/* * The MIT License * * Copyright (c) 2007 MIT 6.270 Robotics Competition * * 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 #define GYRO_PORT 11 #define MOTOR_LEFT 0 #define MOTOR_RIGHT 1 #define VELOCITY 150 #define MOTOR_MAX_VEL 220 #define MOTOR_MIN_VEL 80 int16_t motor_left_vel, motor_right_vel; int sign(float number) { if(number > 0) return 1; else if(number < 0) return -1; else return 0; } //bound velocity by MAX_VEL and MIN_VEL int16_t limitVelocity(int16_t velocity) { int16_t speed = abs(velocity); if(speed > MOTOR_MAX_VEL) return velocity * sign(velocity); else if (speed < MOTOR_MIN_VEL) return -velocity * sign(velocity); else return velocity; } void setVelocity(int left, int right) { motor_set_vel(MOTOR_LEFT, limitVelocity(left)); motor_set_vel(MOTOR_RIGHT, limitVelocity(right)); } float testFactor() { return 100.0 / frob_read_range(1, 1000); } void moveStraight(float desired_heading) { motor_left_vel = VELOCITY; motor_right_vel = VELOCITY; while(1) { float delta_heading = desired_heading - gyro_get_degrees(); motor_left_vel = VELOCITY - delta_heading * testFactor(); motor_right_vel = VELOCITY + delta_heading * testFactor(); setVelocity(motor_left_vel, motor_right_vel); } } int umain (void) { moveStraight(gyro_get_degrees()); return 0; } void usetup (void) { printf("\n Place robot. Press go to calibrate."); go_click(); pause(500); gyro_init(GYRO_PORT, 1400000 , 800L); }