Subversion Repositories FlightCtrl

Rev

Rev 2102 | 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 EATR arrays
 */

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

/*
 * 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 control[4];
extern int32_t controlIntegrals[4];
extern uint16_t controlActivity;
extern uint16_t maxControl[2];

extern volatile uint8_t MKFlags;
extern uint16_t isFlying;

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

void controlMixer_setNeutral(void);

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

void controlMixer_update(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);

/*
 * 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
 */

// void controlMixer_handleCommands(void);
uint8_t controlMixer_getArgument(void);
uint8_t controlMixer_isCommandRepeated(void);
// TODO: Abstract away if possible.
uint8_t controlMixer_testCompassCalState(void);