/* Pico code v0.1 Useage: Drives the pico robot in a simple obstacle avoidance routine. Mild immunity to ambient light conditions. License: Copyright (c) 2007 Zac Wheeler Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include #include #include #include // these control the visible LED #define PORT_LED PORTA #define DDR_LED DDRA #define LED (1<<4) #define _led_on() PORT_LED |= LED; DDR_LED |= LED #define _led_off() PORT_LED &= ~LED; DDR_LED |= LED #define _led_flip() PORT_LED ^= LED; DDR_LED |= LED // these control the motors #define PORT_MOTORS PORTA #define DDR_MOTORS DDRA #define PIN_MOTORS PINA #define MOTOR_IN1 (1<<0) #define MOTOR_IN2 (1<<1) #define MOTOR_IN3 (1<<2) #define MOTOR_IN4 (1<<3) // these just setup the DDRs #define _init_left_motor() \ PORT_MOTORS &= ~(MOTOR_IN1|MOTOR_IN2); \ DDR_MOTORS |= MOTOR_IN1|MOTOR_IN2 #define _init_right_motor() \ PORT_MOTORS &= ~(MOTOR_IN3|MOTOR_IN4); \ DDR_MOTORS |= MOTOR_IN3|MOTOR_IN4 // these set the IO for the 3901 to control motor direction #define _left_motor_forward() \ PORT_MOTORS |= MOTOR_IN1; \ PORT_MOTORS &= ~MOTOR_IN2 #define _left_motor_backward() \ PORT_MOTORS |= MOTOR_IN2; \ PORT_MOTORS &= ~MOTOR_IN1 #define _left_motor_stop() \ PORT_MOTORS |= MOTOR_IN1|MOTOR_IN2 #define _left_motor_roll() \ PORT_MOTORS &= ~(MOTOR_IN1|MOTOR_IN2) #define _right_motor_forward() \ PORT_MOTORS |= MOTOR_IN3; \ PORT_MOTORS &= ~MOTOR_IN4 #define _right_motor_backward() \ PORT_MOTORS |= MOTOR_IN4; \ PORT_MOTORS &= ~MOTOR_IN3 #define _right_motor_stop() \ PORT_MOTORS |= MOTOR_IN3|MOTOR_IN4 #define _right_motor_roll() \ PORT_MOTORS &= ~(MOTOR_IN3|MOTOR_IN4) // these are the different motor states #define BACKWARD MOTOR_IN1 #define FORWARD MOTOR_IN2 #define STOPPED 0xFF // these are the two motors #define LEFT 1 #define RIGHT 2 // reflective sensors #define PORT_SENSORS PORTB #define DDR_SENSORS DDRB #define PIN_SENSORS PINB #define LEFT_LED (1<<2) #define _left_sensor_on() \ DDR_SENSORS |= LEFT_LED; \ PORT_SENSORS |= LEFT_LED #define _left_sensor_off() \ PORT_SENSORS &= ~ LEFT_LED volatile uint8_t g_left_motor_state; volatile uint8_t g_right_motor_state; // return the adc value on a given pin uint16_t get_adc_val(uint8_t pin){ uint16_t result; DIDR0 |= ADC7D; ADMUX = 0x07 & pin; ADCSRA = (1< 50) { _led_off(); go_forward(); } else { _led_on(); backup_and_turn(0); } wdt_reset(); } }