wiki:Example4

Examples

Example 4

Makes HERD robot a simple remote controlled robot by a standard TV remote.

example4/main.c:

#include <all.h>

/* command number of the standby button on a tv remote control */
#define RC5_COMMAND_STANDBY     12

int
main(void) 
{
        /* initialize devices */
        init_all();
        
        /* direction we received from */
        uint8_t dir;

        /* remote control command we received
         * see http://en.wikipedia.org/wiki/RC-5
         * for details  */
        uint8_t rc_data;

        /* directions we receive from (for testfunction rc5_has_data_pwm) */
        uint8_t receive_mask = ANYDIR;

        /* endless loop */
        while(1) {

                /* wait until we received a remote control signal.
                 * save the received data in rc_data and the direction we
                 * received from in dir */
                while((dir = rc5_recv_rc_command(&rc_data)) < 0) {
                }
                
                switch(rc_data) {
                        /* the numbers 0-9 corresponds to the number buttons of a remote control */
                        
                        case 2:
                                /* button 2 received 
                                 * move forward until new rc5 data is received*/
                                pwmdrive_go_until(FORWARD, rc5_has_data_pwm, &receive_mask, NULL);
                                break;

                        case 8:
                                /* button 8 received 
                                 * move backward until new rc5 data is received */
                                pwmdrive_go_until(BACKWARD, rc5_has_data_pwm, &receive_mask, NULL);
                                break;

                        case 4:
                                /* button 4 received 
                                 * turn left until new rc5 data is received */
                                pwmdrive_turn_until(LEFT, rc5_has_data_pwm, &receive_mask);
                                break;

                        case 6:
                                /* button 6 received 
                                 * turn right until new rc5 data is received */
                                pwmdrive_turn_until(RIGHT, rc5_has_data_pwm, &receive_mask);
                                break;
                
                        case 7:
                                /* button 7 received 
                                 * turn left 90 degrees */
                                pwmdrive_turn_deg10(LEFT, 9);
                                break;
                        case 9:
                                /* button 9 received 
                                 * turn right 90 degrees */
                                pwmdrive_turn_deg10(RIGHT, 9);
                                break;

                        case 5:
                                /* button 5 received 
                                 * blink pattern */
                                led_on(LEFT);
                                delay_ms(150);
                                led_on(FRONT);
                                delay_ms(150);
                                led_on(RIGHT);
                                delay_ms(150);
                                led_on(BACK);
                                delay_ms(150);
                                led_off(LEFT);
                                delay_ms(150);
                                led_off(FRONT);
                                delay_ms(150);
                                led_off(RIGHT);
                                delay_ms(150);
                                led_off(BACK);
                                delay_ms(150);
                                break;

                        case RC5_COMMAND_STANDBY:
                                /* standby button received 
                                 * turn towards the sender */

                                /* turn on the LED of the sender direction */
                                led_on(dir);
                                delay_ms(1000);
                                switch(dir) {
                                        case LEFT:
                                                pwmdrive_turn_deg10(LEFT, 9);
                                                break;
                                        case RIGHT:
                                                pwmdrive_turn_deg10(RIGHT, 9);
                                                break;
                                        case FRONT:
                                                break;
                                        case BACK:
                                                pwmdrive_turn_deg10(RIGHT, 18);
                                                break;
                                }
                                led_off(dir);
                                break;
                        
                        default:
                                /* something else pressed
                                 * blink with all LEDs */
                                led_on(ANYDIR);
                                delay_ms(1000);
                                led_off(ANYDIR);

                }
        }
        return 0;
}
Last modified 7 years ago Last modified on Nov 23, 2011, 11:24:14 AM