[tuxdroid-svn] r521 - firmware/tuxcore/trunk
Status: Beta
Brought to you by:
ks156
From: jaguarondi <c2m...@c2...> - 2007-09-13 07:30:11
|
Author: jaguarondi Date: 2007-09-13 09:30:10 +0200 (Thu, 13 Sep 2007) New Revision: 521 Modified: firmware/tuxcore/trunk/main.c firmware/tuxcore/trunk/motors.c firmware/tuxcore/trunk/motors.h firmware/tuxcore/trunk/parser.c firmware/tuxcore/trunk/standalone.c Log: * Reorganized the spin functions. * This is the last cleanup of this module for now. Modified: firmware/tuxcore/trunk/main.c =================================================================== --- firmware/tuxcore/trunk/main.c 2007-09-13 06:32:43 UTC (rev 520) +++ firmware/tuxcore/trunk/main.c 2007-09-13 07:30:10 UTC (rev 521) @@ -338,7 +338,7 @@ led_bak = readLeds(); closeIO(); powersave_movements(); - stopSpin(); + stop_spinning(); stop_mouth(); stop_flippers(); PRR_bak = PRR; Modified: firmware/tuxcore/trunk/motors.c =================================================================== --- firmware/tuxcore/trunk/motors.c 2007-09-13 06:32:43 UTC (rev 520) +++ firmware/tuxcore/trunk/motors.c 2007-09-13 07:30:10 UTC (rev 521) @@ -88,11 +88,20 @@ uint8_t spin_move_counter; /** PWM applied on the spinning motor. */ uint8_t spin_PWM = 5; +/** Direction type for spinning. */ +enum spin_direction +{ + LEFT, + RIGHT, +}; /** Spinning direction */ static uint8_t spin_direction; -/** PWM mask register applied on the port on a regular basis. */ +/** PWM mask register applied on the motor port on a regular basis to drive the + * flippers and spinning. */ uint8_t portB_PWM_mask; +#define flippers_PWMMask portB_PWM_mask +#define spin_PWM_mask portB_PWM_mask /** * \name Module configuration @@ -125,13 +134,14 @@ /* Enable 'Pin change interrupt 2' for the eyes position switches */ PCMSK2 = PSW_EYES_C_MK; PCICR |= _BV(PCIE2); - /* Enable 'INT1 interrupt' for the spin position switch in falling edge mode */ + /* Enable 'INT1 interrupt' for the spin position switch in falling edge + * mode */ EICRA |= _BV(ISC11); EIMSK |= _BV(INT1); } /** - \brief Initializes the position switches for minimal power consumption. + \brief Initialize the position switches for minimal power consumption. In normal mode, all pins have internal pull-up and each switch shorts the pin low. So in power save mode, we must set all pins as strong low. */ @@ -215,18 +225,18 @@ behavior in all states: - opened - no movement is required; - closed - just one movement is required to open the eyes; - - unknown - two movements are required as the eyes can only be stopped when - they are closing; a spring is used to open them. + - unknown - two movements are required as the eyes can only be stopped + when they are closing; a spring is used to open them. - We need to differentiate when the eyes are opened and stopped, and when + We need to differentiate when the eyes are opened and stopped, and when they are opened but blinking. In the first case, we don't need to do anything. But in the second, we need to stop the eyes in the open position. To stop the eyes in the open state by sending the open_eyes_cmd when they're blinking, eyes_move_counter is initalised but the blink_eyes function isn't called. - If the motor is running, it will execute two movements before stopping. If the - motor isn't switched on, nothing happens but eyes_move_counter is set to a - non zero value though they are no movements happening. + If the motor is running, it will execute two movements before stopping. If + the motor isn't switched on, nothing happens but eyes_move_counter is set to + a non zero value though they are no movements happening. \note The computer side (API, daemon) must be aware that the eyes_move_counter can be non zero with the motor switched off. @@ -464,8 +474,8 @@ /** \brief Start waving the flippers up and down - \ingroup flippers - \param cnt number of movements before the flippers will stop + \ingroup flippers + \param cnt number of movements before the flippers will stop \param pwm pwm value between 1 (slow) and 5 (fast) The flippers will start waving until 'cnt' movements have been executed. A @@ -502,9 +512,8 @@ wave_flippers(2, 5); } - /** - \brief Raise flippers + \brief Raise flippers \ingroup flippers The condition to raise the flippers is that they aren't already in the upper @@ -529,7 +538,6 @@ wave_flippers(1,5); } - /** \brief Lower flippers \ingroup flippers @@ -613,42 +621,22 @@ /*! @} */ /** - \brief Spin position interrupt - \ingroup spin - - This interrupt stops the spinning motor when the desired number of movements - have been executed. - - The switch is pushed each 90 degrees. We set the interrupt in falling edge - mode so there's no interrupt generated when the switch is released. - */ -ISR(SIG_INTERRUPT1) + * \name Spinning functions + * @{ */ +/** \brief Low level access to stop the spinning motor. */ +static inline void stop_spin_motor(void) __attribute__ ((always_inline)); +void stop_spin_motor(void) { - if (spin_move_counter) - { - spin_move_counter--; - - if (!spin_move_counter) - { - spin_PWM_mask &= ~MOT_SPIN_MK; - stopSpinMot(); - spin_stop_delay = SPIN_BRAKING_DLY; - if (spin_direction == LEFT) - spin_PWM_mask |= MOT_SPIN_R_MK; - else - spin_PWM_mask |= MOT_SPIN_L_MK; - } - } + spin_PWM_mask &= ~MOT_SPIN_MK; + MOT_SPIN_PT &= ~MOT_SPIN_MK; } - /** - \brief Stop the spinning motor - \ingroup primitives + \brief Stop spinning immediately. + \ingroup spin */ -void stopSpin(void) +void stop_spinning(void) { - spin_PWM_mask &= ~MOT_SPIN_MK; - stopSpinMot(); + stop_spin_motor(); } /** @@ -656,14 +644,14 @@ \param angle Angle to turn, in 90° unit \ingroup spin */ -void spinLeft(uint8_t angle, uint8_t pwm) +void spin_left(uint8_t angle, uint8_t pwm) { - /* Check if direction is changing and if pos. switch isn't push. Then, - * increment angle value to prevent too short rotation */ - if (spin_direction == RIGHT && (PSW_SPIN_PIN & PSW_SPIN_MK)) - { - angle += 1; - } + /* If the rotation direction is changing and we are not stopped exactly on + * the switch (position switch not pressed), we need to increment the angle + * value to prevent counting the first switch detection that will happen as + * soon as the rotation starts. */ + if ((spin_direction == RIGHT) && (PSW_SPIN_PIN & PSW_SPIN_MK)) + angle++; spin_direction = LEFT; spin_move_counter = angle; spin_PWM = pwm; @@ -676,13 +664,13 @@ \param angle Angle to turn, in 90° unit \ingroup spin */ -void spinRight(uint8_t angle, uint8_t pwm) +void spin_right(uint8_t angle, uint8_t pwm) { /* If the rotation direction is changing and we are not stopped exactly on * the switch (position switch not pressed), we need to increment the angle * value to prevent counting the first switch detection that will happen as * soon as the rotation starts. */ - if (spin_direction == LEFT && (PSW_SPIN_PIN & PSW_SPIN_MK)) + if ((spin_direction == LEFT) && (PSW_SPIN_PIN & PSW_SPIN_MK)) angle++; spin_direction = RIGHT; spin_move_counter = angle; @@ -690,10 +678,38 @@ spin_PWM_mask &= ~MOT_SPIN_L_MK; spin_PWM_mask |= MOT_SPIN_R_MK; } +/** + \brief Spin position interrupt + This interrupt stops the spinning motor when the desired number of movements + have been executed. + The switch is pushed each 90 degrees. We set the interrupt in falling edge + mode so there's no interrupt generated when the switch is released. + */ +ISR(SIG_INTERRUPT1) +{ + if (spin_move_counter) + { + spin_move_counter--; + + if (!spin_move_counter) + { + spin_PWM_mask &= ~MOT_SPIN_MK; + stop_spin_motor(); + spin_stop_delay = SPIN_BRAKING_DLY; + if (spin_direction == LEFT) + spin_PWM_mask |= MOT_SPIN_R_MK; + else + spin_PWM_mask |= MOT_SPIN_L_MK; + } + } +} +/*! @} */ + /** - \brief Control the PWM of the spinning & flippers and all motors braking. + \brief Periodic routine that controls the PWM of the spinning, flippers and + all motors braking. \fn motor_control \ingroup movements @@ -725,7 +741,7 @@ if (pwm_tim++ == PWM_PERIOD) { pwm_tim = 0; - PORTB |= portB_PWM_mask; /* spin and flippers */ + PORTB |= portB_PWM_mask; /* spin and flippers */ } /* Flippers timer to stop the flippers in any position */ @@ -775,7 +791,7 @@ spin_stop_delay--; if (!spin_stop_delay) { - stopSpinMot(); + stop_spin_motor(); } } } Modified: firmware/tuxcore/trunk/motors.h =================================================================== --- firmware/tuxcore/trunk/motors.h 2007-09-13 06:32:43 UTC (rev 520) +++ firmware/tuxcore/trunk/motors.h 2007-09-13 07:30:10 UTC (rev 521) @@ -59,14 +59,6 @@ #include "hardware.h" -#define flippers_PWMMask portB_PWM_mask -#define spin_PWM_mask portB_PWM_mask - -enum mouth_pos_s -{ OPEN, CLOSE, UNDEFINED }; -enum spin_direction -{ LEFT, RIGHT }; - /** \ingroup eyes */ extern uint8_t eyes_move_counter; /** \ingroup mouth */ @@ -76,9 +68,6 @@ /** \ingroup spin */ extern uint8_t spin_move_counter, spin_PWM; -/* pwm mask registers */ -extern uint8_t portB_PWM_mask; - /* * Module configuration */ @@ -102,38 +91,13 @@ extern void wave_flippers(uint8_t cnt, uint8_t pwm); extern void lower_flippers(void); extern void raise_flippers(void); -void stopSpin(void); -void spinLeft(uint8_t angle, uint8_t pwm); -void spinRight(uint8_t angle, uint8_t pwm); +extern void stop_spinning(void); +extern void spin_left(uint8_t angle, uint8_t pwm); +extern void spin_right(uint8_t angle, uint8_t pwm); /* - * Macro and inline functions definition + * Control */ - -/** - \brief Stop the spinning motor - \ingroup primitives - */ -static inline void stopSpinMot(void) __attribute__ ((always_inline)); -void stopSpinMot(void) -{ - spin_PWM_mask &= ~MOT_SPIN_MK; - MOT_SPIN_PT &= ~MOT_SPIN_MK; -} -/** - \brief Start spinning to the left - \ingroup primitives - */ -#define runSpinLeft() (MOT_SPIN_R_PT &= ~MOT_SPIN_R_MK); \ - (MOT_SPIN_L_PT |= MOT_SPIN_L_MK) - -/** - \brief Start spinning to the right - \ingroup primitives - */ -#define runSpinRight() (MOT_SPIN_L_PT &= ~MOT_SPIN_L_MK); \ - (MOT_SPIN_R_PT |= MOT_SPIN_R_MK) - extern void motor_control(void); #endif /* _MOTORS_H_ */ Modified: firmware/tuxcore/trunk/parser.c =================================================================== --- firmware/tuxcore/trunk/parser.c 2007-09-13 06:32:43 UTC (rev 520) +++ firmware/tuxcore/trunk/parser.c 2007-09-13 07:30:10 UTC (rev 521) @@ -122,15 +122,15 @@ } else if (command[0] == SPIN_LEFT_CMD) { - spinLeft(command[1], command[2]); + spin_left(command[1], command[2]); } else if (command[0] == SPIN_RIGHT_CMD) { - spinRight(command[1], command[2]); + spin_right(command[1], command[2]); } else if (command[0] == STOP_SPIN_CMD) { - stopSpin(); + stop_spinning(); } /* Leds */ Modified: firmware/tuxcore/trunk/standalone.c =================================================================== --- firmware/tuxcore/trunk/standalone.c 2007-09-13 06:32:43 UTC (rev 520) +++ firmware/tuxcore/trunk/standalone.c 2007-09-13 07:30:10 UTC (rev 521) @@ -208,7 +208,7 @@ /* Disable spinning when plugged */ if ((gStatus.sw & GSTATUS_POWERPLUGSW_MK)) { - stopSpin(); /* flush the spinning commands */ + stop_spinning(); /* flush the spinning commands */ spin_move_counter = 0; } @@ -288,13 +288,13 @@ wave_flippers(mov_nbr, 5); break; case K_LEFT: - spinLeft(mov_nbr, spin_PWM); + spin_left(mov_nbr, spin_PWM); break; case K_RIGHT: - spinRight(mov_nbr, spin_PWM); + spin_right(mov_nbr, spin_PWM); break; case K_STOP: - stopSpin(); + stop_spinning(); stop_flippers(); stop_mouth(); break; |