Monday, June 1, 2009

Robot programs

Here is the wheelchair program, complete with steering check...
It goes with this robot

Wheelchair Robot Program, controls above pictured robot     Copyright (C) 2009  Jordan Lange      This program is free software: you can redistribute it and/or modify     it under the terms of the GNU General Public License as published by     the Free Software Foundation, either version 3 of the License, or     (at your option) any later version.      This program is distributed in the hope that it will be useful,     but WITHOUT ANY WARRANTY; without even the implied warranty of     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     GNU General Public License for more details.      You should have received a copy of the GNU General Public License     along with this program.  If not, see 
#define engine_main OUT_A;
#define engine_steer OUT_B;
#define sensor_left SENSOR_1;
#define sensor_right SENSOR_3;
#define sensor_steering SENSOR_2;

int turning = 0;

task main() {
SetSensor(SENSOR_1, SENSOR_TOUCH);
SetSensor(SENSOR_2, SENSOR_TOUCH);
SetSensor(SENSOR_3, SENSOR_TOUCH);
SetSensorMode(SENSOR_1, SENSOR_MODE_RAW);
SetSensorMode(SENSOR_2, SENSOR_MODE_RAW);
SetSensorMode(SENSOR_3, SENSOR_MODE_RAW);
SetPower(OUT_A, 99); // Main power at max
SetPower(OUT_C, 99); // Main power at max
SetPower(OUT_B, 3); // Steering power at 3

OnFwd(OUT_A); // Turn on engines
OnFwd(OUT_C);
// Check for bumping into the walls...
start steering_check;
// Navigate the course
start navigate_course;
}

task steering_check() {
while (true) {
if (turning != 1) {
// Make sure the steering is straight
if (SENSOR_2 > 45) {
// Turn power off while correcting the steering
int x = OutputStatus(0);
int y = OutputStatus(2);
if (x > 0) {
Off(OUT_A);
Off(OUT_C);
}
// Steering is off, set it all the way to the left
SetPower(OUT_B, 7);
OnRev(OUT_B);
Wait(20);
Off(OUT_B);
SetPower(OUT_B, 3);
// Bring the steering back to center
int timeout = Timer(0);
int timeoutCounter = 0;
int power = 3;
while (true) {
if (Timer(0) > timeout + 30) {
// Try again...
// set steering all the way to the left again
SetPower(OUT_B, 7);
OnRev(OUT_B);
Wait(20);
Off(OUT_B);
if (timeoutCounter > 4) {
// Failsafe, wheels are STUCK
power++;
}
SetPower(OUT_B, power);
// Reset timeout
timeout = Timer(0);
timeoutCounter++;
}
if (SENSOR_2 <>
break;
OnFwd(OUT_B);
Wait(1);
Off(OUT_B);
}
// Turn the power back on
if (x > 0) {
OnFwd(OUT_A);
OnFwd(OUT_C);
}
// Steering is aligned, yay!
repeat (5) { PlaySound(SOUND_CLICK); }
}
}
}
}

task navigate_course() {
OnFwd(OUT_A);
OnFwd(OUT_C);
Wait(1150);
Off(OUT_A);
Off(OUT_C);
repeat(10) {
SendMessage(1);
}
ClearMessage();
until (Message() == 2);
OnFwd(OUT_A);
OnFwd(OUT_C);
// start banking to the right a little bit so it hits the walls
turning = 1;
turn(5);
Wait(500);
turn(-4);
until (SENSOR_1 <= 150 && SENSOR_3 <= 150);
// Hit the far wall, reverse and then turn left
OnRev(OUT_A);
OnRev(OUT_C);
Wait(700);
OnFwd(OUT_A);
OnFwd(OUT_C);
// hard left to cross stairs
turn(-12);
Wait(720);
turn(8); // straighten steering up a bit
Wait(100);
turning = 0;
// The first wall it hits, turn to the left
// Should be in a position where that's useful
until (SENSOR_1 <= 150 || SENSOR_3 <= 150);
reverse_turn(13, 180);
repeat (5) { PlaySound(SOUND_CLICK); }
// Defender mode (default mode)
while (true) {
if (SENSOR_1 <= 200) {
reverse_turn(-13, 180);
}
if (SENSOR_3 <= 200) {
reverse_turn(13, 180);
}
}
}

void turn(int time) {
if (time <>
OnFwd(OUT_B);
Wait(-1 * time);
Off(OUT_B);
} else if (time > 0) {
OnRev(OUT_B);
Wait(time);
Off(OUT_B);
}
}
void reverse_turn(int time, int back_delay) {
turning = 1;
if (time <>
OnRev(OUT_A);
OnRev(OUT_C);
OnFwd(OUT_B);
Wait(-1 * time);
Off(OUT_B);
Wait(back_delay);
Off(OUT_A);
Off(OUT_C);
} else if (time > 0) {
OnRev(OUT_A);
OnRev(OUT_C);
OnRev(OUT_B);
Wait(time);
Off(OUT_B);
Wait(back_delay);
Off(OUT_A);
Off(OUT_C);
}
OnFwd(OUT_A);
OnFwd(OUT_C);
turning = 0; // Let the steering check take care of setting the steering back to straight
}

Next up is the Car program...
It is this robot, but the extending arm will be removed and the brick turned to the side to allow the wheelchair coming over the ramp to communicate with it
Car Robot Program, controls above pictured robot     Copyright (C) 2009  Jordan Lange      This program is free software: you can redistribute it and/or modify     it under the terms of the GNU General Public License as published by     the Free Software Foundation, either version 3 of the License, or     (at your option) any later version.      This program is distributed in the hope that it will be useful,     but WITHOUT ANY WARRANTY; without even the implied warranty of     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     GNU General Public License for more details.      You should have received a copy of the GNU General Public License     along with this program.  If not, see 
task main() {
SetPower(OUT_A, 7);
SetSensor(SENSOR_3, SENSOR_LIGHT);
ClearMessage();
while (Message() != 1) {
OnFwd(OUT_A);
Wait(500);
OnRev(OUT_A);
Wait(500);
}
if (SENSOR_3 <= 38) {
Toggle(OUT_A);
until (SENSOR_3 >= 45);
Wait(200);
Off(OUT_A);
}
Off(OUT_A);
}

And last is our defender program...

Defender Robot Program, controls car robot pictured above, but with a different purpose     Copyright (C) 2009  Jordan Lange      This program is free software: you can redistribute it and/or modify     it under the terms of the GNU General Public License as published by     the Free Software Foundation, either version 3 of the License, or     (at your option) any later version.      This program is distributed in the hope that it will be useful,     but WITHOUT ANY WARRANTY; without even the implied warranty of     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     GNU General Public License for more details.      You should have received a copy of the GNU General Public License     along with this program.  If not, see 
task main() {
SetPower(OUT_A, 7);
OnRev(OUT_A);
Wait(500);
Off(OUT_A);
SetPower(OUT_C, 5);
OnFwd(OUT_C);
Wait(200);
Off(OUT_C);
}

This program extends this arm (not Chun's arm):


No comments:

Post a Comment