wiki:Example5

Examples

Example 5

Gather all follower bots around one leader bot (in two separate groups).

example5/main.c:

#include <all.h>

/* actual numbers do not matter */
/*
 * If you are unfamiliar with #define:
 * The following line tells the compiler to literally replace the symbol
 * "COMMAND_CALL_1" by the symbol "10".
 * Thus if you want to use another number, you only have to change it in one
 * place.
 */
#define COMMAND_CALL_1  10
#define COMMAND_CALL_2  12

int
main(void) 
{
        /* initialize devices */
        init_all();

        for(;;) {
                uint8_t command;

                /* Robots are separated into two groups depending on user DIP 2 */
                if(get_user_switch2() == 0) {
                        command = COMMAND_CALL_1;
                } else {
                        command = COMMAND_CALL_2;

                }

                /*
                 * Each robot is either a follower (tries to drive to leader)
                 * or a leader (tries to get every follower in the same group gathered around it)
                 * depending on user DIP 1
                 */
                if(get_user_switch1() ==  0) {
                        led_set(ANYDIR);
                        rc5_send_command(ANYDIR, ANY_DEST, command, 0);
                        delay_ms(500);
                        pwmdrive_turn_deg10(RIGHT, 1);
                        led_set(OFF);
                        delay_ms(500);
                } else {
                        led_set(ANYDIR);
                        uint16_t data; // not used
                        
                        int8_t dir = rc5_recv_specific_command(ANYDIR, ANY_SOURCE, command, &data, NULL);
                        
                        led_set(dir);
                        rc5_send_command(ANYDIR & ~dir, ANY_DEST, command, 0);
                        delay_ms(100);
                        
                        if(dir == FRONT) {
                                pwmdrive_go_cm(FORWARD, 10, NULL);
                        } else if(dir == BACK) {
                                pwmdrive_turn_deg10(RIGHT, 18);
                        } else if(dir == RIGHT) {
                                pwmdrive_turn_deg10(RIGHT, 3);
                        } else if(dir == LEFT) {
                                pwmdrive_turn_deg10(LEFT, 3);
                        }
                        rc5_recv_clear_all();
                        led_set(OFF);
                }
        }

        return 0;
}
Last modified 6 years ago Last modified on May 8, 2012, 12:22:04 AM