aboutsummaryrefslogtreecommitdiff
path: root/apps/px4/fmu/fmu.cpp
blob: 3021321daf8bf0acb89addf46b10c2583d6fc2b8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/****************************************************************************
 *
 *   Copyright (C) 2012 PX4 Development Team. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name PX4 nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

/**
 * @file fmu.cpp
 *
 * Driver/configurator for the PX4 FMU multi-purpose port.
 */

#include <nuttx/config.h>

#include <sys/types.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <semaphore.h>
#include <string.h>
#include <fcntl.h>
#include <poll.h>
#include <errno.h>
#include <stdio.h>
#include <math.h>
#include <unistd.h>

#include <nuttx/arch.h>

#include <drivers/device/device.h>
#include <drivers/drv_pwm_output.h>
#include <drivers/drv_gpio.h>
#include <drivers/drv_mixer.h>

#include <uORB/topics/actuator_controls.h>
#include <systemlib/mixer.h>

#include <arch/board/up_pwm_servo.h>

class FMUServo : public device::CDev
{
public:
	enum Mode {
		MODE_2PWM,
		MODE_4PWM,
		MODE_NONE
	};
	FMUServo(Mode mode);
	~FMUServo();

	virtual int	ioctl(struct file *filp, int cmd, unsigned long arg);

	virtual int	init();

private:
	static const unsigned _max_actuators = 4;

	Mode		_mode;
	int		_task;
	int		_t_actuators;
	int		_t_armed;
	unsigned	_num_outputs;

	volatile bool	_task_should_exit;
	bool		_armed;

	mixer_s	*_mixer[_max_actuators];

	static void	task_main_trampoline(int argc, char *argv[]);
	void		task_main();
};

namespace
{

FMUServo	*g_servo;

} // namespace

FMUServo::FMUServo(Mode mode) :
	CDev("fmuservo", PWM_OUTPUT_DEVICE_PATH),
	_mode(mode),
	_task(-1),
	_t_actuators(-1),
	_t_armed(-1),
	_task_should_exit(false),
	_armed(false)
{
	for (unsigned i = 0; i < _max_actuators; i++)
		_mixer[i] = nullptr;
}

FMUServo::~FMUServo()
{
	if (_task != -1) {

		/* task should wake up every 100ms or so at least */
		_task_should_exit = true;

		unsigned i = 0;

		do {
			/* wait 20ms */
			usleep(20000);

			/* if we have given up, kill it */
			if (++i > 10) {
				task_delete(_task);
				break;
			}

		} while (_task != -1);
	}

	g_servo = nullptr;
}

int
FMUServo::init()
{
	int ret;

	ASSERT(_task == -1);

	/* do regular cdev init */
	ret = CDev::init();

	if (ret != OK)
		return ret;

	/* start the IO interface task */
	_task = task_create("fmuservo", SCHED_PRIORITY_DEFAULT, 1024, (main_t)&FMUServo::task_main_trampoline, nullptr);

	if (_task < 0) {
		debug("task start failed: %d", errno);
		return -errno;
	}

	return OK;
}

void
FMUServo::task_main_trampoline(int argc, char *argv[])
{
	g_servo->task_main();
}

void
FMUServo::task_main()
{
	/* configure for PWM output */
	switch (_mode) {
	case MODE_2PWM:
		/* multi-port with flow control lines as PWM */
		/* XXX magic numbers */
		up_pwm_servo_init(0x3);
		break;

	case MODE_4PWM:
		/* multi-port as 4 PWM outs */
		/* XXX magic numbers */
		up_pwm_servo_init(0xf);
		break;

	case MODE_NONE:
		/* we should never get here... */
		break;
	}

	/* subscribe to objects that we are interested in watching */
	_t_actuators = orb_subscribe(ORB_ID_VEHICLE_ATTITUDE_CONTROLS);
	orb_set_interval(_t_actuators, 20);		/* 50Hz update rate */

	_t_armed = orb_subscribe(ORB_ID(actuator_armed));
	orb_set_interval(_t_armed, 100);		/* 10Hz update rate */

	struct pollfd fds[2];
	fds[0].fd = _t_actuators;
	fds[0].events = POLLIN;
	fds[1].fd = _t_armed;
	fds[1].events = POLLIN;

	unsigned num_outputs = (_mode == MODE_2PWM) ? 2 : 4;

	log("starting");

	/* loop until killed */
	while (!_task_should_exit) {

		/* sleep waiting for data, but no more than 100ms */
		int ret = ::poll(&fds[0], 2, 100);

		/* this would be bad... */
		if (ret < 0) {
			log("poll error %d", errno);
			usleep(1000000);
			continue;
		}

		/* do we have a control update? */
		if (fds[0].revents & POLLIN) {
			struct actuator_controls_s ac;
			float *controls[1] = { &ac.control[0] };

			/* get controls */
			orb_copy(ORB_ID_VEHICLE_ATTITUDE_CONTROLS, _t_actuators, &ac);

			/* iterate actuators */
			for (unsigned i = 0; i < num_outputs; i++) {

				/* if the actuator is configured */
				if (_mixer[i] != nullptr) {
					/* mix controls to the actuator */
					float output = mixer_mix(_mixer[i], &controls[0]);

					/* scale for PWM output 900 - 2100us */
					up_pwm_servo_set(i, 1500 + (600 * output));
				}
			}
		}

		/* how about an arming update? */
		if (fds[1].revents & POLLIN) {
			struct actuator_armed_s aa;

			/* get new value */
			orb_copy(ORB_ID(actuator_armed), _t_armed, &aa);

			/* update PMW servo armed status */
			up_pwm_servo_arm(aa.armed);
		}
	}

	::close(_t_actuators);
	::close(_t_armed);

	/* make sure servos are off */
	up_pwm_servo_deinit();

	log("stopping");

	/* note - someone else is responsible for restoring the GPIO config */

	/* tell the dtor that we are exiting */
	_task = -1;
	_exit(0);
}

int
FMUServo::ioctl(struct file *filp, int cmd, unsigned long arg)
{
	int ret = OK;
	int channel;
	struct MixInfo *mi;
	struct mixer_s *mm, *tmm;

	switch (cmd) {
	case PWM_SERVO_ARM:
		up_pwm_servo_arm(true);
		break;

	case PWM_SERVO_DISARM:
		up_pwm_servo_arm(false);
		break;

	case PWM_SERVO_SET(2):
	case PWM_SERVO_SET(3):
		if (_mode != MODE_4PWM) {
			ret = -EINVAL;
			break;
		}

		/* FALLTHROUGH */
	case PWM_SERVO_SET(0):
	case PWM_SERVO_SET(1):
		if (arg < 2100) {
			channel = cmd - PWM_SERVO_SET(0);
			up_pwm_servo_set(channel, arg);

		} else {
			ret = -EINVAL;
		}

		break;

	case PWM_SERVO_GET(2):
	case PWM_SERVO_GET(3):
		if (_mode != MODE_4PWM) {
			ret = -EINVAL;
			break;
		}

		/* FALLTHROUGH */
	case PWM_SERVO_GET(0):
	case PWM_SERVO_GET(1): {
			channel = cmd - PWM_SERVO_SET(0);
			*(servo_position_t *)arg = up_pwm_servo_get(channel);
			break;
		}

	case MIXERIOCGETMIXERCOUNT:
		if (_mode == MODE_4PWM) {
			*(unsigned *)arg = 4;

		} else {
			*(unsigned *)arg = 2;
		}

		break;

	case MIXERIOCGETMIXER(3):
	case MIXERIOCGETMIXER(2):
		if (_mode != MODE_4PWM) {
			ret = -EINVAL;
			break;
		}

		/* FALLTHROUGH */
	case MIXERIOCGETMIXER(1):
	case MIXERIOCGETMIXER(0):
		channel = cmd - MIXERIOCGETMIXER(0);

		/* if no mixer is assigned, we return ENOENT */
		if (_mixer[channel] == nullptr) {
			ret = -ENOENT;
			break;
		}

		/* caller's MixInfo */
		mi = (struct MixInfo *)arg;

		/* if MixInfo claims to be big enough, copy mixer info */
		if (mi->num_controls >= _mixer[channel]->control_count) {
			memcpy(&mi->mixer, _mixer[channel], MIXER_SIZE(_mixer[channel]->control_count));

		} else {
			/* just update MixInfo with actual size of the mixer */
			mi->mixer.control_count = _mixer[channel]->control_count;
		}

		break;

	case MIXERIOCSETMIXER(3):
	case MIXERIOCSETMIXER(2):
		if (_mode != MODE_4PWM) {
			ret = -EINVAL;
			break;
		}

		/* FALLTHROUGH */
	case MIXERIOCSETMIXER(1):
	case MIXERIOCSETMIXER(0):
		channel = cmd - MIXERIOCSETMIXER(0);

		/* get the caller-supplied mixer and check */
		mm = (struct mixer_s *)arg;

		/* allocate local storage and copy from the caller*/
		if (mm != nullptr) {

			if (mixer_check(mm, 1, NUM_ACTUATOR_CONTROLS)) {
				/* only the attitude group is supported */
				ret = -EINVAL;
				break;
			}

			/* allocate a new mixer struct */
			tmm = (struct mixer_s *)malloc(MIXER_SIZE(mm->control_count));
			memcpy(tmm, mm, MIXER_SIZE(mm->control_count));

		} else {
			tmm = nullptr;
		}

		/* swap in new mixer for old */
		mm = _mixer[channel];
		_mixer[channel] = tmm;

		/* if there was an old mixer, free it */
		if (mm != nullptr)
			free(mm);

		break;

	default:
		ret = -ENOTTY;
		break;
	}

	return ret;
}

namespace
{

enum PortMode {
	PORT_MODE_UNSET = 0,
	PORT_FULL_GPIO,
	PORT_FULL_SERIAL,
	PORT_FULL_PWM,
	PORT_GPIO_AND_SERIAL,
	PORT_PWM_AND_SERIAL,
	PORT_PWM_AND_GPIO,
};

PortMode g_port_mode;

int
fmu_new_mode(PortMode new_mode)
{
	int fd;
	int ret = OK;
	uint32_t gpio_bits;
	FMUServo::Mode servo_mode;

	/* get hold of the GPIO configuration descriptor */
	fd = open(GPIO_DEVICE_PATH, 0);

	if (fd < 0)
		return -errno;

	/* start by tearing down any existing state and revert to all-GPIO-inputs */
	if (g_servo != nullptr) {
		delete g_servo;
		g_servo = nullptr;
	}

	/* reset to all-inputs */
	ioctl(fd, GPIO_RESET, 0);

	gpio_bits = 0;
	servo_mode = FMUServo::MODE_NONE;

	switch (new_mode) {
	case PORT_FULL_GPIO:
	case PORT_MODE_UNSET:
		/* nothing more to do here */
		break;

	case PORT_FULL_SERIAL:
		/* set all multi-GPIOs to serial mode */
		gpio_bits = GPIO_MULTI_1 | GPIO_MULTI_2 | GPIO_MULTI_3 | GPIO_MULTI_4;
		break;

	case PORT_FULL_PWM:
		/* select 4-pin PWM mode */
		servo_mode = FMUServo::MODE_4PWM;
		break;

	case PORT_GPIO_AND_SERIAL:
		/* set RX/TX multi-GPIOs to serial mode */
		gpio_bits = GPIO_MULTI_3 | GPIO_MULTI_4;
		break;

	case PORT_PWM_AND_SERIAL:
		/* select 2-pin PWM mode */
		servo_mode = FMUServo::MODE_2PWM;
		/* set RX/TX multi-GPIOs to serial mode */
		gpio_bits = GPIO_MULTI_3 | GPIO_MULTI_4;
		break;

	case PORT_PWM_AND_GPIO:
		/* select 2-pin PWM mode */
		servo_mode = FMUServo::MODE_2PWM;
		break;
	}

	/* adjust GPIO config for serial mode(s) */
	if (gpio_bits != 0)
		ioctl(fd, GPIO_SET_ALT_1, gpio_bits);

	close(fd);

	/* create new PWM driver if required */
	if (servo_mode != FMUServo::MODE_NONE) {
		g_servo = new FMUServo(servo_mode);

		if (g_servo == nullptr) {
			ret = -ENOMEM;

		} else {
			ret = g_servo->init();

			if (ret != OK) {
				delete g_servo;
				g_servo = nullptr;
			}
		}
	}

	return ret;
}

void
test(void)
{
	int	fd;

	fd = open(PWM_OUTPUT_DEVICE_PATH, 0);

	if (fd < 0) {
		puts("open fail");
		exit(1);
	}

	ioctl(fd, PWM_SERVO_ARM, 0);
	ioctl(fd, PWM_SERVO_SET(0), 1000);

	close(fd);

	exit(0);
}

void
fake(int argc, char *argv[])
{
	if (argc < 5) {
		puts("fmu fake <roll> <pitch> <yaw> <thrust> (values -100 - 100)");
		exit(1);
	}

	struct actuator_controls_s ac;

	ac.control[0] = strtol(argv[1], 0, 0) / 100.0f;

	ac.control[1] = strtol(argv[2], 0, 0) / 100.0f;

	ac.control[2] = strtol(argv[3], 0, 0) / 100.0f;

	ac.control[3] = strtol(argv[4], 0, 0) / 100.0f;

	int handle = orb_advertise(ORB_ID_VEHICLE_ATTITUDE_CONTROLS, &ac);

	if (handle < 0) {
		puts("advertise failed");
		exit(1);
	}

	close(handle);

	exit(0);
}

} // namespace

extern "C" __EXPORT int fmu_main(int argc, char *argv[]);

int
fmu_main(int argc, char *argv[])
{
	PortMode new_mode = PORT_MODE_UNSET;

	if (!strcmp(argv[1], "test"))
		test();

	if (!strcmp(argv[1], "fake"))
		fake(argc - 1, argv + 1);

	/*
	 * Mode switches.
	 *
	 * XXX use getopt?
	 */
	if (!strcmp(argv[1], "mode_gpio")) {
		new_mode = PORT_FULL_GPIO;

	} else if (!strcmp(argv[1], "mode_serial")) {
		new_mode = PORT_FULL_SERIAL;

	} else if (!strcmp(argv[1], "mode_pwm")) {
		new_mode = PORT_FULL_PWM;

	} else if (!strcmp(argv[1], "mode_gpio_serial")) {
		new_mode = PORT_GPIO_AND_SERIAL;

	} else if (!strcmp(argv[1], "mode_pwm_serial")) {
		new_mode = PORT_PWM_AND_SERIAL;

	} else if (!strcmp(argv[1], "mode_pwm_gpio")) {
		new_mode = PORT_PWM_AND_GPIO;
	}

	/* was a new mode set? */
	if (new_mode != PORT_MODE_UNSET) {

		/* yes but it's the same mode */
		if (new_mode == g_port_mode)
			return OK;

		/* switch modes */
		return fmu_new_mode(new_mode);
	}

	/* test, etc. here */

	fprintf(stderr, "FMU: unrecognised command, try:\n");
	fprintf(stderr, "  mode_gpio, mode_serial, mode_pwm, mode_gpio_serial, mode_pwm_serial, mode_pwm_gpio\n");
	return -EINVAL;
}