/******************************************************************************
 * pitch and roll prototypes for Spray2008 SPI control
 * CS assignment = CS_PITCH, CS_ROLL is done in spi_cmd.h
 * created Oct2008
 * updated: 24jun09 for brake control
 *****************************************************************************/

short       exercise_motor  ( short cs );
short       spi_motor       ( short cs, short rel,  short cnts  );
short       send_p_r_brake  ( short cs, short on    );
short       send_p_r_run    ( short cs, short rel,  short cnts  );
short       run_p_r_wait    ( short cs, short rel,  short cnts  );
short       wait_p_r        ( short cs, short tmax );

void        motor_menu      ( short cs );
void        pr_flash_menu   ( struct spi_param *ss );
void        display_prflash ( void     );
short       init_pr_iparam  ( short cs );
short       last_ops        ( short cs );
void        parse_last_ops  ( struct spi_param *ss );
short       clear_ops       ( short cs );

void        display_pr_pwr   ( struct spi_param *ss );
void        display_pr_ad12  ( struct spi_param *ss );
void        display_motor    ( struct spi_param *ss );
void        display_pr_query ( short r, struct spi_param *ss );  // display the response
void        pr_set_default   ( struct spi_param *ss );

short       get_p_r_pot      ( short cs );
void        control_pitch    ( float now, short desired, float g );
void        control_roll     ( float now, short desired );
void        control_head     ( short down,float dh, float ravg  );

short       roll_wing        ( short wing_now, short seek);
void        zero_roll_angle  ( void );
void        zero_pitch_angle ( void );
short       RF_Wing_Setup    ( short unit );


void        test_pitch_roll  ( void );

#define DCDT_R  15 // = # of counts change per second for roll
// DCDT_R is a default minimum for roll, in case EEPROM was never set
// 17 cnts/sec ~right for the MicroMO 2224 motor+1526:1 gearhead.
#define DCDT_P  15 // = # of counts change per second for pitch
// DCDT_P is a default minimum for pitch, in case EEPROM was never set
// 17 cnts/sec ~right for the MicroMO 2224 motor+1526:1 gearhead.


//****   specific constants to the pitch/roll SPI module **********************
//****   These values must agree with the compiled msp430 code !!!

// define the a/d channels
#define AD_AMPS 0 // chan0 = amps
#define AD_VOLT 1 // chan1 = volts
#define AD_POT  2 // chan2 = potentiometer


// sub-commands for controlling pitch/roll aux power **************************
#define PR_ON_AD         1  // turn on needed circuitry to sample a/d value
#define PR_14V_ON        2  // turn on bit for 14V on
#define PR_5V_ON         3  // turn on bit for the 5V power





// flash addr locations in msp430
// !!!WARNING!!! THESE MUST AGREE WITH MSP430 VALUES !!!!!
//#define F_SER_NO       0    // serial # of this peripheral
//Firmware version is compiled into msp430 code, NOT user-settable at addr[2]:
//#define F_VERSION      2    // flash version
#define F_DCDT         4    // #counts change per second for normal motor
#define F_MIN_POT      6    // minimum inbounds potentiometer reading 
#define F_MAX_POT      8    // maximum inbounds potentiometer reading 
#define F_C2U_I       10    // convert from counts to amps*1000
#define F_C2U_V       12    // convert from counts to volts*1000
#define F_MAX_I       14    // max current, mA

// presently #16..48 =20 valus are undefined


/** OLD had these pot values in the module's FLASH
//** NEW, the Spray keeps track of these
#define F_POT_SET     50    // start array of potentiometer setting values

#define F_zero  2 // +2  level pitch or zero roll
#define F_port  4 // +4  xmit pitch or port wing
#define F_stbd  6 // +6  pitch_up or stbd wing
// +8  optional
// +10 = optional
// +12 = optional
 /**/

// status of run_motor function
#define MOTOR_OK      1  // ran normally, stopped due to pot value
#define MOTOR_DT      2  // ran on time alone: small increment
#define MOTOR_BAD_POT 3  // pot out-of-range, ran on time alone
#define MOTOR_NO_MOVE 4  // pot out-of-range, did NOT run
#define MOTOR_NONE    5  // requested d-pot to small to try to move
#define MOTOR_SLOW    6  // 2s change in pot was too small..aborted
#define MOTOR_MAX     7  // exceeded the max pot value
#define MOTOR_MIN     8  // exceeded the min pot value
#define MOTOR_TMO     9  // timed-out (slow motor overall)
#define MOTOR_AMPS   10  // motor amps exceeded max allowed
#define MOTOR_LIMIT  11  // limit switch is CLOSED..at the limit!!!
#define MOTOR_STOP   12  // stopped from external command
#define MOTOR_CLEAR  13  // last op cleared: no other update yet



