Skip to content

Commit

Permalink
Removed delay on startup of maple. Got rid of ppm_sync global, the rc…
Browse files Browse the repository at this point in the history
… 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)
  • Loading branch information
0xjairo committed Mar 3, 2012
1 parent 7ad3088 commit 8ec379a
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 25 deletions.
3 changes: 3 additions & 0 deletions RC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ void RC::update() {
#endif

if(_sp == SP_INVALID)
{
_throttle=0.0;
return;
}

_yaw = _rcCmd[3];
_pitch = _rcCmd[1];
Expand Down
5 changes: 3 additions & 2 deletions esc-control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
Expand Down Expand Up @@ -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.");
Expand Down Expand Up @@ -115,4 +116,4 @@ void esc_manual_control(void) {
set_rotor_throttle(3, 0.0);

}

#endif
19 changes: 11 additions & 8 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,16 @@ __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
setup();
rc.init();

toggleLED();
delay(2000);
SerialUSB.println("Starting....");

t_prev = 0; t_1Hz = 0;
t_prev = 0;
while (1) {

t = micros();
Expand All @@ -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;
Expand Down
22 changes: 18 additions & 4 deletions main.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

34 changes: 25 additions & 9 deletions ppm-decode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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<TX_NUM_CHANNELS;i++)
{
j = (sync_pulse+1+i)%NUM_TIMERS;
Expand All @@ -58,7 +67,7 @@ int rx_read(int *sp, float *rc)
if(delta > CHANNEL_MAX_TICKS)
{
SerialUSB.println("E!:MAX_TICKS!");
sync_pulse = SP_INVALID;
*sp = SP_INVALID;
return 1;
}

Expand All @@ -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);
Expand All @@ -85,7 +93,7 @@ int rx_read(int *sp, float *rc)
}



#ifdef CLI_UTILS
int rx_read_commands()
{

Expand Down Expand Up @@ -118,8 +126,9 @@ int rx_read_commands()
return 0;

}
#endif

void ppm_sync()
void ppm_sync(int *sp)
{
//#define VERBOSITY 4
int i;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -219,6 +229,7 @@ int printData(){

return 0;
}
#endif

//invoked as configured by the DMA mode flags.
void dma_isr()
Expand Down Expand Up @@ -382,6 +393,7 @@ void init_ppm_dma_transfer()

}

#ifdef CLI_UTILS
void print_ppm_data()
{
//busy wait on the dma_data_captured flag
Expand All @@ -398,6 +410,7 @@ void print_ppm_data()


}
#endif

void ppm_decode_go()
{
Expand All @@ -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");

Expand Down Expand Up @@ -475,3 +489,5 @@ void ppm_decode(void){
SerialUSB.println("Done!");

}
#endif

2 changes: 1 addition & 1 deletion ppm-decode.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion yaw-servo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 8ec379a

Please sign in to comment.