wiki:Example2.2

Examples

Example 2.2

Wait until user DIP switches (Pin 1 and 2) changed, then drive 20 cm forward. Afterwards examine return value in order to decide what happened.

example2_2/main.c:

#include <all.h>

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

        /* endless loop */
        while(1) {
                /* get current switch state */
                uint8_t switch_state = get_user_switches();
                /* wait (and blink) until user switches changed */
                while(get_user_switches() == switch_state) {
                        /* blink with front LED ... */
                        led_on(FRONT);
                        delay_ms(500);
                        led_off(FRONT);
                        delay_ms(500);
                }

                /* wait for 2 seconds */
                delay_ms(2000);

                /* move 20 cm forward and save return value and driven distance*/
                uint8_t driven_dist;
                uint8_t retval = pwmdrive_go_cm(FORWARD, 20, &driven_dist);
                
                /* check if pwmdrive_go_cm succeeded */
                switch(retval) {
                        case PWM_RETURN_SUCCESS:
                                /* pwmdrive_go_cm succeeded 
                                 * (driven_dist should be ~ 20) */
                                
                                /* turn on only front LED */
                                led_set(FRONT);
                                break;
                        case PWM_RETURN_INVALID_DIRECTION:
                                /* pwmdrive_go_cm failed because 
                                 * the direction argument was wrong (not FORWARD or BACKWARD) 
                                 * (driven_dist should be 0) */
                                 
                                /* turn on only left LED */
                                led_set(LEFT);
                                break;
                        case PWM_RETURN_NO_GROUND:
                                /* pwmdrive_go_cm failed because no ground was detected 
                                 * actually driven distance available in driven_dist */

                                /* turn on only right LED */
                                led_set(RIGHT);
                                break;
                        case PWM_RETURN_COLLISION:
                                /* pwmdrive_go_cm failed because a collision was
                                 * detected by the obstacle sensor
                                 * actually driven distance available in driven_dist */

                                /* turn on only back LED */
                                led_set(BACK);
                                break;
                        case PWM_RETURN_STUCK:
                                /* pwmdrive_go_cm failed because robot got stuck 
                                 * actually driven distance available in driven_dist */

                                /* turn on left and right LEDs */
                                led_set(LEFT | RIGHT);
                }

                /* wait for one second */
                delay_ms(2000);
                /* turn of all LEDs */
                led_off(ANYDIR);
        }

        return 0;
}
Last modified 6 years ago Last modified on Nov 23, 2011, 11:25:13 AM