[tuxdroid-svn] r585 - in firmware: tuxcore/trunk tuxdefs
Status: Beta
Brought to you by:
ks156
From: Paul_R <c2m...@c2...> - 2007-10-08 13:47:47
|
Author: Paul_R Date: 2007-10-08 15:47:19 +0200 (Mon, 08 Oct 2007) New Revision: 585 Modified: firmware/tuxcore/trunk/global.h firmware/tuxcore/trunk/main.c firmware/tuxcore/trunk/motors.c firmware/tuxcore/trunk/sensors.c firmware/tuxcore/trunk/sensors.h firmware/tuxdefs/commands.h Log: * Changed sensors.[ch] to include the battery level. The new status command for battery level is 0xC7 - STATUS_BATTERY_CMD The third status byte is to indicate if motors are running : 0 - All the motors are off 1 - One or more motor(s) is (are) on. * Changed motors.c and gStatus to include the motors status. Modified: firmware/tuxcore/trunk/global.h =================================================================== --- firmware/tuxcore/trunk/global.h 2007-10-08 11:48:05 UTC (rev 584) +++ firmware/tuxcore/trunk/global.h 2007-10-08 13:47:19 UTC (rev 585) @@ -99,27 +99,19 @@ * gStatus.mot: Motors Status */ -/* gStatus.mot:[1-0] Spin motor - * xxxxxxx0 === Spin right - * xxxxxxx1 === Spin left */ -#define GSTATUS_MOT_SR 0x01 -#define GSTATUS_MOT_SL 0x02 -/* gStatus.mot:[3-2] Eyes/Mouth motor - * xxxxxxx0 === Eyes - * xxxxxxx1 === Mouth - * xxxxxx0x === Motor stopped - * xxxxxx1x === Motor running */ -#define GSTATUS_MOT_E 0x04 -#define GSTATUS_MOT_M 0x08 -/* gStatus.mot:[5-4] Wings motor - * xxxxxx00 === Wings forward - * xxxxxx01 === Wings backward - * xxxxxx0x === Motor stopped - * xxxxxx1x === Motor running */ -#define GSTATUS_MOT_W0 0x10 -#define GSTATUS_MOT_W1 0x20 -//#define GSTATUS_SW__6 0x40 -//#define GSTATUS_SW__7 0x80 +/* gStatus.mot:[0] Spin motor + * xxxxxxx0 === Spin off + * xxxxxxx1 === Spin on */ +#define GSTATUS_MOT_SPIN 0x01 +/* gStatus.mot:[1] Eyes/Mouth motor + * xxxxxx0x === motor off + * xxxxxx1x === motor on */ +#define GSTATUS_MOT_EYES 0x02 +#define GSTATUS_MOT_MOUTH 0x02 +/* gStatus.mot:[2] Wings motor + * xxxxx0xx === Wings off + * xxxxx1xx === Wings on */ +#define GSTATUS_MOT_WINGS 0x04 /* * gStatus.ir: IR Status @@ -165,8 +157,12 @@ uint8_t lightL; /* Light level low byte */ uint8_t lightH; /* Light level high byte */ uint8_t lightM; /* Light mode */ + uint8_t batteryL; /* Battery level low byte */ + uint8_t batteryH; /* Battery level high byte */ + uint8_t batteryS; /* Battery level status */ uint8_t ir; /* IR RC5 code received from tux's remote */ uint8_t pos; /* Poitionning */ + uint8_t mot; } GSTATUS; Modified: firmware/tuxcore/trunk/main.c =================================================================== --- firmware/tuxcore/trunk/main.c 2007-10-08 11:48:05 UTC (rev 584) +++ firmware/tuxcore/trunk/main.c 2007-10-08 13:47:19 UTC (rev 585) @@ -162,6 +162,8 @@ { t4ms_flag = FALSE; motor_control(); + if (sensorsUpdate) + sensors_control(); if (i2c_pause) i2c_pause--; /* delay after a nack */ @@ -204,8 +206,6 @@ { updateStatusFlag = 0; updateStatus(); /* add sensors states to the status queue */ - - getLight(); /* light measurement */ } //if (commandProcessFlag) /* XXX check if it's quick enough with the if //*/ @@ -261,11 +261,8 @@ turnIrOff(); } -#define STATUS_BYTE_SIZE 20 static void updateStatus(void) { - if (FifoLength(statusFifo) >= (STATUS_BUF_SIZE - STATUS_BYTE_SIZE)) - return; /* if there's not enough place, we dont save. */ statusFifoFlag = 1; FifoPut(statusFifo, STATUS_SENSORS1_CMD); FifoPut(statusFifo, gStatus.sw); @@ -283,14 +280,22 @@ FifoPut(statusFifo, spin_move_counter); FifoPut(statusFifo, gStatus.pos); FifoPut(statusFifo, 0); - if (light_f) /* send light measurement */ + if (sensorsStatus & LIGHT_FLAG) /* send light measurement */ { - light_f = 0; + sensorsStatus &= ~LIGHT_FLAG; FifoPut(statusFifo, STATUS_LIGHT_CMD); FifoPut(statusFifo, gStatus.lightH); FifoPut(statusFifo, gStatus.lightL); FifoPut(statusFifo, gStatus.lightM); } + if (sensorsStatus & BATTERY_FLAG) /* send light measurement */ + { + sensorsStatus &= ~BATTERY_FLAG; + FifoPut(statusFifo, STATUS_BATTERY_CMD); + FifoPut(statusFifo, gStatus.batteryH); + FifoPut(statusFifo, gStatus.batteryL); + FifoPut(statusFifo, gStatus.batteryS); + } if (ir_f) /* send received ir signals */ { ir_f--; @@ -307,7 +312,7 @@ FifoPut(statusFifo, 0); FifoPut(statusFifo, 0); } - + sensorsUpdate |= STATUS_SENT; statusFifoFlag = 0; } Modified: firmware/tuxcore/trunk/motors.c =================================================================== --- firmware/tuxcore/trunk/motors.c 2007-10-08 11:48:05 UTC (rev 584) +++ firmware/tuxcore/trunk/motors.c 2007-10-08 13:47:19 UTC (rev 585) @@ -194,6 +194,7 @@ */ void stop_eyes(void) { + gStatus.mot &= ~GSTATUS_MOT_EYES; stop_eyes_motor(); } @@ -210,6 +211,7 @@ */ void blink_eyes(uint8_t const cnt) { + gStatus.mot |= GSTATUS_MOT_EYES; eyes_move_counter = cnt; eyes_stop_delay = 0; run_eyes_motor(); @@ -352,6 +354,7 @@ */ void stop_mouth(void) { + gStatus.mot &= ~GSTATUS_MOT_MOUTH; stop_mouth_motor(); } @@ -367,6 +370,7 @@ */ void move_mouth(uint8_t const cnt) { + gStatus.mot |= GSTATUS_MOT_MOUTH; mouth_move_counter = cnt; mouth_stop_delay = 0; run_mouth_motor(); @@ -469,6 +473,7 @@ */ void stop_flippers(void) { + gStatus.mot &= ~GSTATUS_MOT_WINGS; stop_flippers_motor(); } @@ -487,6 +492,7 @@ */ void wave_flippers(uint8_t const cnt, uint8_t const pwm) { + gStatus.mot |= GSTATUS_MOT_WINGS; flippers_move_counter = cnt; flippers_PWM = pwm; flippers_stop_delay = 0; @@ -636,6 +642,7 @@ */ void stop_spinning(void) { + gStatus.mot &= ~GSTATUS_MOT_SPIN; stop_spin_motor(); } @@ -647,6 +654,7 @@ */ void spin_left(uint8_t const angle, uint8_t const pwm) { + gStatus.mot |= GSTATUS_MOT_SPIN; spin_direction = LEFT; spin_move_counter = angle; /* If the rotation direction is changing and we are not stopped exactly on @@ -668,6 +676,7 @@ */ void spin_right(uint8_t const angle, uint8_t const pwm) { + gStatus.mot |= GSTATUS_MOT_SPIN; spin_direction = RIGHT; spin_move_counter = angle; /* If the rotation direction is changing and we are not stopped exactly on @@ -762,7 +771,7 @@ flippers_stop_delay--; if (!flippers_stop_delay) { - stop_flippers_motor(); + stop_flippers(); flippers_PWM = 0; } } @@ -773,7 +782,7 @@ eyes_stop_delay--; if (!eyes_stop_delay) { - stop_eyes_motor(); + stop_eyes(); } } @@ -783,17 +792,17 @@ mouth_stop_delay--; if (!mouth_stop_delay) { - stop_mouth_motor(); + stop_mouth(); } } /* spin braking (motor reversed) delay */ if (spin_stop_delay) { - spin_stop_delay--; - if (!spin_stop_delay) - { - stop_spin_motor(); - } + spin_stop_delay--; + if (!spin_stop_delay) + { + stop_spinning(); + } } } Modified: firmware/tuxcore/trunk/sensors.c =================================================================== --- firmware/tuxcore/trunk/sensors.c 2007-10-08 11:48:05 UTC (rev 584) +++ firmware/tuxcore/trunk/sensors.c 2007-10-08 13:47:19 UTC (rev 585) @@ -29,38 +29,55 @@ #include "sensors.h" #include "adc.h" #include "global.h" +#include "sensors.h" #include "hardware.h" +#include "adc.h" +void static light_control(uint16_t light_val); +void static battery_control(uint16_t battery_val); +void static read_flag(void); + +uint8_t static sensorsControlState; +uint8_t static motorsStatus; +uint8_t sensorsStatus; +uint8_t sensorsUpdate; + /** - * \name Light level - * @{ */ -/** \ingroup sensors */ + \brief This function manage the acquisition of all sensors. -void save_light(void) + Two sensors are treaties for now : A light sensor and the battery. + This function is based on a state machine. + + To execute this function, the STATUS_SENT flag OR the ADC_READY flag must be set. The STATUS_SEND flag is set when the status are send to the PC. The ADC_READY flag is set when an interrupt on the ADC module occurs. + + When the state machine is in the last state, it will be resetted and wait while the status are sent. If status are sent before the last state, the cycle continue. + */ +void sensors_control(void) { - union16_t adc_value; - adc_value.w = ADC_read(); - /* Save light mode before changing it below. */ - gStatus.lightM = LIGHT_PU_PORT & LIGHT_PU_MK; - /* Check if it's necessary to change the light mode. */ - if ((adc_value.b[0] <= 0x28) && (adc_value.b[1] == 0x00) && - !(LIGHT_PU_PORT & LIGHT_PU_MK)) + if (sensorsControlState == 0) { - LIGHT_PU_PORT |= LIGHT_PU_MK; - LIGHT_PU_DDR |= LIGHT_PU_MK; + // New cycle started - clear the status_sent flag + sensorsUpdate &= ~STATUS_SENT; + + ADC_start(LIGHT_ADMUX); // XXX Arg : ADMUX + sensorsControlState ++; } - else if ((adc_value.b[0] >= 0xE8) && (adc_value.b[1] == 0x03) && - (LIGHT_PU_PORT & LIGHT_PU_MK)) + else if (sensorsControlState == 1) { - LIGHT_PU_PORT &= ~LIGHT_PU_MK; - LIGHT_PU_DDR &= ~LIGHT_PU_MK; + light_control(ADC_read()); + if (gStatus.mot != 0) + motorsStatus = 1; + + ADC_start(BATTERY_ADMUX); // XXX arg : ADMUX + sensorsControlState ++; } - /* adc_value.b[1] should be read after adc_value.b[0] otherwise - * adc_value.b[0] stays wite protected (c.f. datasheet). */ - gStatus.lightL = adc_value.b[0]; - gStatus.lightH = adc_value.b[1]; - /* There's a new light value. */ - light_f = 1; + else if (sensorsControlState == 2) + { + if (gStatus.mot != 0) + motorsStatus = 1; + battery_control(ADC_read()); + sensorsControlState = 0; + } } /** @@ -68,22 +85,11 @@ */ void sensors_init(void) { - ADC_set_ISR_callback(save_light); + ADC_set_ISR_callback(read_flag); ADC_init(); } /** - \brief Start a light level measurement. - - Start a light level conversion. The result will be saved by the ADC - interrupt. - */ -void getLight(void) -{ - ADC_start(LIGHT_ADMUX); -} - -/** \brief Disable all sensors. This function should minimize power consumption. */ @@ -91,4 +97,73 @@ { ADC_disable(); } + +/** + \brief Callback function of the ADC ISR. + + When an ADC conversion cycle is finished, the ADC_READY flag is set. + */ + +void static read_flag(void) +{ + sensorsUpdate |= ADC_READY; +} + +/** + \brief Light control function. + \param light_val The light level + + This function control the light level. + The light sensor has 2 different values of pull-up resistors (1M and 10k) to increase the range. It's necessary to switch from one to the other resistor depending on the light level. This is done here. + */ + +void static light_control(uint16_t light_val) +{ + union16_t light_value; + light_value.w = light_val; + + /* Save light mode before changing it below. */ + gStatus.lightM = LIGHT_PU_PORT & LIGHT_PU_MK; + + /* Check if it's necessary to change the light mode. */ + if ((light_value.w <= 0x0028) && !(LIGHT_PU_PORT & LIGHT_PU_MK)) + { + LIGHT_PU_PORT |= LIGHT_PU_MK; + LIGHT_PU_DDR |= LIGHT_PU_MK; + } + else if ((light_value.w >= 0x03E8) && (LIGHT_PU_PORT & LIGHT_PU_MK)) + { + LIGHT_PU_PORT &= ~LIGHT_PU_MK; + LIGHT_PU_DDR &= ~LIGHT_PU_MK; + } + + gStatus.lightL = light_value.b[0]; + gStatus.lightH = light_value.b[1]; + /* There's a new light value. */ + sensorsStatus |= LIGHT_FLAG; + sensorsUpdate &= ~ ADC_READY; +} + +/** + \brief Battery control function. + \param battery_val The battery level + + The battery level is just store in gStatus, to be sent to the PC + */ + +void static battery_control(uint16_t battery_val) +{ + union16_t battery_value; + battery_value.w = battery_val; + + gStatus.batteryL = battery_value.b[0]; + gStatus.batteryH = battery_value.b[1]; + if (motorsStatus) + gStatus.batteryS = 1; + else + gStatus.batteryS = 0; + motorsStatus = 0; + sensorsStatus |= BATTERY_FLAG; + sensorsUpdate &= ~ ADC_READY; +} /*! @} */ Modified: firmware/tuxcore/trunk/sensors.h =================================================================== --- firmware/tuxcore/trunk/sensors.h 2007-10-08 11:48:05 UTC (rev 584) +++ firmware/tuxcore/trunk/sensors.h 2007-10-08 13:47:19 UTC (rev 585) @@ -34,8 +34,16 @@ #ifndef _SENSORS_H_ #define _SENSORS_H_ +#define LIGHT_FLAG 1 +#define BATTERY_FLAG 2 +#define STATUS_SENT 1 +#define ADC_READY 2 + +uint8_t extern sensorsStatus; +uint8_t extern sensorsUpdate; + void sensors_init(void); void sensors_disable(void); -void getLight(void); +void sensors_control(void); #endif /* _SENSORS_H_ */ Modified: firmware/tuxdefs/commands.h =================================================================== --- firmware/tuxdefs/commands.h 2007-10-08 11:48:05 UTC (rev 584) +++ firmware/tuxdefs/commands.h 2007-10-08 13:47:19 UTC (rev 585) @@ -324,6 +324,10 @@ /* 1st parameter: Tux ID number (MSB) */ /* 2nd parameter: Tux ID number (LSB) */ +#define STATUS_BATTERY_CMD 0xC7 +/* 1st parameter: battery level high byte */ +/* 2nd parameter: battery level low byte */ +/* 3rd parameter: battery measure status : 0 - motors off; 1 - motors on*/ /* * Special commands */ |