Subversion Repositories FlightCtrl

Rev

Rev 1910 | Rev 2103 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#include <inttypes.h>
/*
 * An attempt to define a generic control. That could be an R/C receiver, an external control
 * (serial over Bluetooth, Wi232, XBee, whatever) or the NaviCtrl.
 * This resembles somewhat an object-oriented class definition (except that there are no virtuals).
 * The idea is that the combination of different control inputs, of the way they superimpose upon
 * each other, the priorities between them and the behavior in case that one fails is simplified,
 * and all in one place.
 */


/*
 * Signal qualities, used to determine the availability of a control.
 * NO_SIGNAL means there was never a signal. SIGNAL_LOST that there was a signal, but it was lost.
 * SIGNAL_BAD is too bad for flight. This is the hysteresis range for deciding whether to engage
 * or disengage emergency landing.
 * SIGNAL_OK means the signal is usable for flight.
 * SIGNAL_GOOD means the signal can also be used for setting parameters.
 */

#define NO_SIGNAL   0
#define SIGNAL_LOST 1
#define SIGNAL_BAD  2
#define SIGNAL_OK   3
#define SIGNAL_GOOD 4

/*
 * The PRTY arrays
 */

#define CONTROL_ELEVATOR 0
#define CONTROL_AILERONS 1
#define CONTROL_THROTTLE 2
#define CONTROL_RUDDER   3

/*
 * Looping flags.
 * LOOPING_UP || LOOPING_DOWN <=> LOOPING_PITCH_AXIS
 * LOOPING_LEFT || LOOPING_RIGHT <=> LOOPING_ROLL_AXIS
 */

#define LOOPING_UP         (1<<0)
#define LOOPING_DOWN       (1<<1)
#define LOOPING_LEFT       (1<<2)
#define LOOPING_RIGHT      (1<<3)
#define LOOPING_PITCH_AXIS (1<<4)
#define LOOPING_ROLL_AXIS  (1<<5)

/*
 * This is only relevant for "abstract controls" ie. all control sources have the
 * same interface. This struct of code pointers is used like an abstract class
 * definition from object-oriented languages, and all control input implementations
 * will declare an instance of the stuct (=implementation of the abstract class).
 */

typedef struct {
        /* Get the pitch input in the nominal range [-STICK_RANGE, STICK_RANGE]. */
        int16_t(*getPitch)(void);

        /* Get the roll input in the nominal range [-STICK_RANGE, STICK_RANGE]. */
        int16_t(*getRoll)(void);

        /* Get the yaw input in the nominal range [-STICK_RANGE, STICK_RANGE]. */
        int16_t(*getYaw)(void);

        /* Get the throttle input in the nominal range [0, THROTTLE_RANGE]. */
        uint16_t(*getThrottle)(void);

        /* Signal quality, by the above SIGNAL_... definitions. */
        uint8_t (*getSignalQuality)(void);

        /* Calibrate sticks to their center positions (only relevant for R/C, really) */
        void (*calibrate)(void);
} t_control;

/*
 * Our output.
 */

extern int16_t controls[4];
extern uint16_t controlActivity;
//extern uint16_t maxControl[2];

void controlMixer_initVariables(void);
//void controlMixer_updateVariables(void);

void controlMixer_setNeutral(void);

/*
 * Update the exported variables. Called at every flight control cycle.
 */

void controlMixer_periodicTask(void);

/*
 * Get the current command. See the COMMAND_.... define's
 */

uint8_t controlMixer_getCommand(void);

void controlMixer_performCalibrationCommands(uint8_t command);

uint8_t controlMixer_getSignalQuality(void);
extern uint8_t controlMixer_didReceiveSignal;

/*
 * The controls operate in [-1024, 1024] just about.
 * Throttle is [0..255] just about.
 */

// Scale controls to 1 byte:
#define CONTROL_SCALING (1024/256)

// Scale throttle levels to byte:
#define MOTOR_SCALING (1024/256)

/*
 * Gets the argument for the current command (a number).
 *
 * Stick position to argument values (for stick control):
 * 2--3--4
 * |     |  +
 * 1  9  5  ^ 0
 * |     |  |  
 * 8--7--6
 *    
 * + <--
 *    0
 *
 * Not in any of these positions: 0
 */

uint8_t controlMixer_getArgument(void);
uint8_t controlMixer_isCommandRepeated(void);