From 8ec379a8d206cff865eadcceb4c1c0cce4daf4a1 Mon Sep 17 00:00:00 2001 From: Jairo Date: Sat, 3 Mar 2012 12:53:18 -0600 Subject: [PATCH] Removed delay on startup of maple. Got rid of ppm_sync global, the rc class has a sp member filling the same function. Changed the timer scale factor to 22 (for a 20ms period on the PWM outputs to the servo and esc's) --- RC.cpp | 3 +++ esc-control.cpp | 5 +++-- main.cpp | 19 +++++++++++-------- main.h | 22 ++++++++++++++++++---- ppm-decode.cpp | 34 +++++++++++++++++++++++++--------- ppm-decode.h | 2 +- yaw-servo.cpp | 2 +- 7 files changed, 62 insertions(+), 25 deletions(-) diff --git a/RC.cpp b/RC.cpp index c709f86..abd478d 100644 --- a/RC.cpp +++ b/RC.cpp @@ -42,7 +42,10 @@ void RC::update() { #endif if(_sp == SP_INVALID) + { + _throttle=0.0; return; + } _yaw = _rcCmd[3]; _pitch = _rcCmd[1]; diff --git a/esc-control.cpp b/esc-control.cpp index 2fd3550..e64d652 100644 --- a/esc-control.cpp +++ b/esc-control.cpp @@ -18,7 +18,7 @@ void esc_init() timer1.setMode(TIMER_CH1, TIMER_PWM); timer1.setMode(TIMER_CH2, TIMER_PWM); timer1.setMode(TIMER_CH3, TIMER_PWM); - timer1.setPrescaleFactor(21); + timer1.setPrescaleFactor(SERVO_PPM_TIMER_PRESCALE_FACTOR); // Set throttle to minimum for(int i=0;i<3;i++) @@ -71,6 +71,7 @@ void set_rotor_throttle(int rotor, float rate) } +#ifdef CLI_UTILS void esc_manual_control(void) { SerialUSB.println("Press \'j\' to lower speed."); SerialUSB.println("Press \'k\' to increase speed."); @@ -115,4 +116,4 @@ void esc_manual_control(void) { set_rotor_throttle(3, 0.0); } - +#endif diff --git a/main.cpp b/main.cpp index 4c07bc4..110b7a1 100644 --- a/main.cpp +++ b/main.cpp @@ -58,7 +58,7 @@ __attribute__((constructor)) void premain() { int main(void) { int fault=0; uint32 t, t_prev, t_delta; - uint32 t_1Hz; + uint32 tick50hz=0; RC rc; // init @@ -66,10 +66,8 @@ int main(void) { rc.init(); toggleLED(); - delay(2000); - SerialUSB.println("Starting...."); - t_prev = 0; t_1Hz = 0; + t_prev = 0; while (1) { t = micros(); @@ -85,19 +83,24 @@ int main(void) { set_rotor_throttle(2, rc.get_channel( CH_THROTTLE )); set_rotor_throttle(3, rc.get_channel( CH_THROTTLE )); + // 10Hz loop + if(tick50hz % 10 == 0) + { + toggleLED(); + } + // 1Hz loop - if(t_1Hz % 50 == 0) + if(tick50hz % 50 == 0) { - toggleLED(); - //printData(); SerialUSB.print("throttle:"); SerialUSB.println(rc.get_channel( CH_THROTTLE )); SerialUSB.println(""); } - t_1Hz++; + + tick50hz++; } } return 0; diff --git a/main.h b/main.h index f6b7036..0e6993b 100644 --- a/main.h +++ b/main.h @@ -28,9 +28,23 @@ /* 3_RX (pin 0 on Maple Mini) */ #define IMU_RX_PIN BOARD_USART3_RX_PIN +/* + * The PPM signal frequency is 50 Hz. + * The max count in the timer's register + * is 65536 (16-bit register). + * The MCU's clock is 72 MHz. + * Therefore the prescale factor is: + * 1/(50 Hz) * (72 MHz) / (65536) = 21.97 + */ +#define SERVO_PPM_TIMER_PRESCALE_FACTOR 22 + +/* min: 1ms + * (1 ms * 72 Mhz)/ (PRESCALE_FACTOR) + * = (1 ms * 72 Mhz)/ 22 + * = 3272.72 ticks + */ +#define PPM_MIN 3273 -/* min: 1ms in tick counts */ -#define PPM_MIN 3430 -/* max: 2ms in tick counts */ -#define PPM_MAX 6855 +/* max: 2ms = 6545.45 */ +#define PPM_MAX 6545 diff --git a/ppm-decode.cpp b/ppm-decode.cpp index da36bdd..33cf4ba 100644 --- a/ppm-decode.cpp +++ b/ppm-decode.cpp @@ -23,7 +23,7 @@ float rx_channels[TX_NUM_CHANNELS]; uint16 delta=0; uint16 ppm_timeout=0; int do_print_timeout_once=1; -int sync_pulse = -1; +//int sync_pulse = -1; //rcCmd_t rcCmd; int rx_read(int *sp, float *rc) @@ -32,22 +32,31 @@ int rx_read(int *sp, float *rc) float duty; int i,j; uint16 delta; + int sync_pulse; + sync_pulse = *sp; if(ppm_timeout == 1) { //TODO: On timeout, zero out commands - SerialUSB.println("ppm_timeout!"); - sync_pulse == SP_INVALID; + if(do_print_timeout_once == 1) + { + SerialUSB.println("ppm_timeout!"); + do_print_timeout_once=0; + } + *sp = SP_INVALID; return 1; } if(sync_pulse == SP_INVALID) { - ppm_sync(); + ppm_sync(sp); return 1; } + // reset timeout print flag + do_print_timeout_once=1; + for(i=0;i CHANNEL_MAX_TICKS) { SerialUSB.println("E!:MAX_TICKS!"); - sync_pulse = SP_INVALID; + *sp = SP_INVALID; return 1; } @@ -68,7 +77,6 @@ int rx_read(int *sp, float *rc) duty = (duty-0.06)/(0.88-0.06); if(duty < 0) duty = 0; if(duty > 1) duty = 1; -// *(float *)(&rcCmd+i) = duty; *(float *)(rc+i) = (float)duty; #ifdef VERBOSITY>3 SerialUSB.print("duty:"); SerialUSB.print(duty); @@ -85,7 +93,7 @@ int rx_read(int *sp, float *rc) } - +#ifdef CLI_UTILS int rx_read_commands() { @@ -118,8 +126,9 @@ int rx_read_commands() return 0; } +#endif -void ppm_sync() +void ppm_sync(int *sp) { //#define VERBOSITY 4 int i; @@ -188,10 +197,11 @@ void ppm_sync() // save temp sp to sync_pulse if(sp_confidence >= SP_CONFIDENCE_MINIMUM) - sync_pulse = temp_sync_pulse; + *sp = temp_sync_pulse; } +#ifdef CLI_UTILS //dump routine to show content of captured data. int printData(){ int i; @@ -219,6 +229,7 @@ int printData(){ return 0; } +#endif //invoked as configured by the DMA mode flags. void dma_isr() @@ -382,6 +393,7 @@ void init_ppm_dma_transfer() } +#ifdef CLI_UTILS void print_ppm_data() { //busy wait on the dma_data_captured flag @@ -398,6 +410,7 @@ void print_ppm_data() } +#endif void ppm_decode_go() { @@ -413,6 +426,7 @@ void ppm_decode_go() } +#ifdef CLI_UTILS void ppm_decode(void){ SerialUSB.println("Decoding DIY Drones PPM encoder on pin D27"); @@ -475,3 +489,5 @@ void ppm_decode(void){ SerialUSB.println("Done!"); } +#endif + diff --git a/ppm-decode.h b/ppm-decode.h index 68362bd..116de9b 100644 --- a/ppm-decode.h +++ b/ppm-decode.h @@ -45,7 +45,7 @@ int printData(); int rx_read(int *sync_pulse, float *rc); int rx_read_commands(); -void ppm_sync(); +void ppm_sync(int *sp); void dma_isr(); void ppm_timeout_isr(); diff --git a/yaw-servo.cpp b/yaw-servo.cpp index 09ef4da..efe6fe7 100644 --- a/yaw-servo.cpp +++ b/yaw-servo.cpp @@ -14,7 +14,7 @@ HardwareTimer timer2(2); void yaw_servo_init() { timer2.setMode(TIMER_CH4, TIMER_PWM); - timer2.setPrescaleFactor(21); + timer2.setPrescaleFactor(SERVO_PPM_TIMER_PRESCALE_FACTOR); set_servo_angle(0.0);