Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //walker start code
- //uses servo.h included with arduino 1.0 package
- #include <Servo.h>
- #include <IRremote.h> //non standard IRremote library from: https://github.com/shirriff/Arduino-IRremote
- //servo naming
- //Front or Back
- //Right or Left
- //Upper or Lower
- //pins choosen here
- //in the event of conflicts in numbering, use these
- #define FRU 11
- #define FRL 12
- #define FLU 9
- #define FLL 10
- #define BRU 4
- #define BRL 5
- #define BLU 6
- #define BLL 7
- //actual names for direct control
- //if you direct control, record the positions too
- Servo FRUs; //pin D11
- Servo FRLs; //pin D12
- Servo FLUs; //pin D9
- Servo FLLs; //pin D10
- Servo BRUs; //pin 4
- Servo BRLs; //pin 5
- Servo BLUs; //pin 6
- Servo BLLs; //pin 7
- //calibration parmaters
- //angle offset from each other for balancing
- const int FRUoff = 0-25;
- const int FRLoff = 0;
- const int FLUoff = 0-25;
- const int FLLoff = 0;
- const int BRUoff = 0+25;
- const int BRLoff = 0;
- const int BLUoff = 0+25;
- const int BLLoff = 0;
- //-1 for reversal, 1 for standard
- const int FRUrev = -1;
- const int FRLrev = -1;
- const int FLUrev = 1;
- const int FLLrev = 1;
- const int BRUrev = -1;
- const int BRLrev = 1;
- const int BLUrev = 1;
- const int BLLrev = -1;
- //this changes the speed of the bot
- //Don't plan on this being the actual speed since all servos move concurrently to reach destination at the same time
- //Only uses integar math still
- float degreesPerStep = 4;
- int stepTime = 25;
- //holds the servo positions for smoothing movement
- int servoPos[8]; //order [FRU, FRL, FLU, FLL, BRU, BRL, BLU, BLL]
- //commands go in this
- //TODO list commands
- //main command: ['g',FRU, FRL, FLU, FLL, BRU, BRL, BLU, BLL] sets all servos to the positions specified
- //ints passed as chars over serial, offset by 90 for unsigned
- int inByte = 0; // incoming serial byte default
- //IR stuff
- int RECV_PIN = 2; //IR decoder on pin D2!!
- IRrecv irrecv(RECV_PIN);
- decode_results results;
- void setup()
- {
- FRUs.attach(FRU);
- FRLs.attach(FRL);
- FLUs.attach(FLU);
- FLLs.attach(FLL);
- BRUs.attach(BRU);
- BRLs.attach(BRL);
- BLUs.attach(BLU);
- BLLs.attach(BLL);
- //open serial comm
- Serial.begin(9600);
- stand(); //put it in a known position
- irrecv.enableIRIn(); // Start the IR receiver
- Serial.println("Finished Startup");
- }
- //takes name of servo and position and sets to that
- //approx -90 to 90 range
- //modified by calibration parameters
- //records the current servo position when modified
- int servoSet(int name, int pos)
- {
- switch (name){
- case FRU:
- FRUs.write(90+(pos+FRUoff)*FRUrev);
- servoPos[0] = pos;
- break;
- case FRL:
- FRLs.write(90+(pos+FRLoff)*FRLrev);
- servoPos[1] = pos;
- break;
- case FLU:
- FLUs.write(90+(pos+FLUoff)*FLUrev);
- servoPos[2] = pos;
- break;
- case FLL:
- FLLs.write(90+(pos+FLLoff)*FLLrev);
- servoPos[3] = pos;
- break;
- case BRU:
- BRUs.write(90+(pos+BRUoff)*BRUrev);
- servoPos[4] = pos;
- break;
- case BRL:
- BRLs.write(90+(pos+BRLoff)*BRLrev);
- servoPos[5] = pos;
- break;
- case BLU:
- BLUs.write(90+(pos+BLUoff)*BLUrev);
- servoPos[6] = pos;
- break;
- case BLL:
- BLLs.write(90+(pos+BLLoff)*BLLrev);
- servoPos[7] = pos;
- break;
- default:
- return 0;
- break;
- }
- return 1;
- }
- //IR decoding
- //These are captured codes from my IR transmittrer
- //hexbug spider channel A
- //this will output unrecognized codes over serial to capture your own if needed
- //hold button and disregard all values that aren't repearing
- //we're not aiming for perfect capture so some dropping is fine
- char getIR(){
- char result = '0';
- if (irrecv.decode(&results)) {
- int value = results.value;
- switch (value){
- case 0xED5A0B2B:
- Serial.println("forward");
- result = 'F';
- break;
- case 0x324FBF0A:
- Serial.println("back");
- result = 'B';
- break;
- case 0x23B42182:
- Serial.println("right");
- result = 'R';
- break;
- case 0x4AAFE047:
- Serial.println("left");
- result = 'L';
- break;
- default:
- Serial.print("Bad code: ");
- Serial.println(results.value, HEX);
- break;
- }
- irrecv.resume(); // Receive the next value
- }
- return result;
- }
- //These step commands are copy overs from my python test script
- int forwardStep(){
- Serial.println("I'll try my best....");
- int stepAngle = -30; //this is the only change over walk, changes the direction of swivel
- int liftAngle = 25;
- int stepPos[8] = {0,40,0,40,0,40,0,40}; //start position
- goTo(stepPos, 10, 30);
- stepPos[1] = stepPos[1]-liftAngle; //lift FR
- stepPos[7] = stepPos[7]-liftAngle; //lift BL
- goTo(stepPos, 10, 30);
- stepPos[0] = stepPos[0]+stepAngle; //swivel FR forward
- stepPos[6] = stepPos[6]+stepAngle; //swivel BL forward
- stepPos[2] = stepPos[2]-stepAngle; //swivel FL back
- stepPos[4] = stepPos[4]-stepAngle;// swivel BR back
- goTo(stepPos, 10, 30);
- stepPos[1] = stepPos[1]+liftAngle; //drop FR
- stepPos[7] = stepPos[7]+liftAngle;// #drop BL
- stepPos[3] = stepPos[3]-liftAngle;// #lift FL
- stepPos[5] = stepPos[5]-liftAngle;// #lift BR
- goTo(stepPos, 10, 30);
- stepPos[0] = stepPos[0]-2*stepAngle;// #swivel FR back x2
- stepPos[6] = stepPos[6]-2*stepAngle;// #swivel BL back x2
- stepPos[2] = stepPos[2]+2*stepAngle;// #swivel FL forward x2
- stepPos[4] = stepPos[4]+2*stepAngle;// #swivel BR forward x2
- goTo(stepPos, 10, 30);
- stepPos[3] = stepPos[3]+liftAngle;// #drop FL
- stepPos[5] = stepPos[5]+liftAngle;// #drop BR
- goTo(stepPos, 10, 30);
- }
- int backwardStep(){
- Serial.println("I'll try my best....");
- int stepAngle = 30; //this is the only change over walk, changes the direction of swivel
- int liftAngle = 25;
- int stepPos[8] = {0,40,0,40,0,40,0,40}; //start position
- goTo(stepPos, 10, 30);
- stepPos[1] = stepPos[1]-liftAngle; //lift FR
- stepPos[7] = stepPos[7]-liftAngle; //lift BL
- goTo(stepPos, 10, 30);
- stepPos[0] = stepPos[0]+stepAngle; //swivel FR forward
- stepPos[6] = stepPos[6]+stepAngle; //swivel BL forward
- stepPos[2] = stepPos[2]-stepAngle; //swivel FL back
- stepPos[4] = stepPos[4]-stepAngle;// swivel BR back
- goTo(stepPos, 10, 30);
- stepPos[1] = stepPos[1]+liftAngle; //drop FR
- stepPos[7] = stepPos[7]+liftAngle;// #drop BL
- stepPos[3] = stepPos[3]-liftAngle;// #lift FL
- stepPos[5] = stepPos[5]-liftAngle;// #lift BR
- goTo(stepPos, 10, 30);
- stepPos[0] = stepPos[0]-2*stepAngle;// #swivel FR back x2
- stepPos[6] = stepPos[6]-2*stepAngle;// #swivel BL back x2
- stepPos[2] = stepPos[2]+2*stepAngle;// #swivel FL forward x2
- stepPos[4] = stepPos[4]+2*stepAngle;// #swivel BR forward x2
- goTo(stepPos, 10, 30);
- stepPos[3] = stepPos[3]+liftAngle;// #drop FL
- stepPos[5] = stepPos[5]+liftAngle;// #drop BR
- goTo(stepPos, 10, 30);
- }
- int turnRight(){
- int stepAngle = -30; //only changed here
- int liftAngle = 25;
- int stepPos[8] = {0,40,0,40,0,40,0,40};// #goto stand
- goTo(stepPos, 10, 30);
- stepPos[3] = stepPos[3]-liftAngle;// #lift FL
- stepPos[5] = stepPos[5]-liftAngle;// #lift BR
- goTo(stepPos, 10, 30);
- stepPos[2] = stepPos[2]+stepAngle;// #swivel FL forward
- stepPos[6] = stepPos[6]-stepAngle;// #swivel BL back
- stepPos[4] = stepPos[4]-stepAngle;// #swivel BR back
- stepPos[0] = stepPos[0]+stepAngle;// #swivel FR forward
- goTo(stepPos, 10, 30);
- stepPos[3] = stepPos[3]+liftAngle;// #drop FL
- stepPos[7] = stepPos[7]-liftAngle;// #lift BL
- stepPos[5] = stepPos[5]+liftAngle;// #drop BR
- stepPos[1] = stepPos[1]-liftAngle;// #lift FR
- goTo(stepPos, 10, 30);
- stepPos[2] = stepPos[2]-stepAngle;// #swivel FL back //oldx2
- stepPos[6] = stepPos[6]+stepAngle;// #swivel BL forward //oldx2
- stepPos[4] = stepPos[4]+stepAngle;// #swivel BR forward //oldx2
- stepPos[0] = stepPos[0]-stepAngle;// #swivel FR back //oldx2
- goTo(stepPos, 10, 30);
- stepPos[7] = stepPos[7]+liftAngle;// #drop BL
- stepPos[1] = stepPos[1]+liftAngle;// #drop FR
- goTo(stepPos, 10, 30);
- }
- int turnLeft(){
- int stepAngle = 30; //only changed here
- int liftAngle = 25;
- int stepPos[8] = {0,40,0,40,0,40,0,40};// #goto stand
- goTo(stepPos, 10, 30);
- stepPos[3] = stepPos[3]-liftAngle;// #lift FL
- stepPos[5] = stepPos[5]-liftAngle;// #lift BR
- goTo(stepPos, 10, 30);
- stepPos[2] = stepPos[2]+stepAngle;// #swivel FL forward
- stepPos[6] = stepPos[6]-stepAngle;// #swivel BL back
- stepPos[4] = stepPos[4]-stepAngle;// #swivel BR back
- stepPos[0] = stepPos[0]+stepAngle;// #swivel FR forward
- goTo(stepPos, 10, 30);
- stepPos[3] = stepPos[3]+liftAngle;// #drop FL
- stepPos[7] = stepPos[7]-liftAngle;// #lift BL
- stepPos[5] = stepPos[5]+liftAngle;// #drop BR
- stepPos[1] = stepPos[1]-liftAngle;// #lift FR
- goTo(stepPos, 10, 30);
- stepPos[2] = stepPos[2]-stepAngle;// #swivel FL back //oldx2
- stepPos[6] = stepPos[6]+stepAngle;// #swivel BL forward //oldx2
- stepPos[4] = stepPos[4]+stepAngle;// #swivel BR forward //oldx2
- stepPos[0] = stepPos[0]-stepAngle;// #swivel FR back //oldx2
- goTo(stepPos, 10, 30);
- stepPos[7] = stepPos[7]+liftAngle;// #drop BL
- stepPos[1] = stepPos[1]+liftAngle;// #drop FR
- goTo(stepPos, 10, 30);
- }
- //listens to the serial port for a array of locations and then sets the servos to that postion
- //this uses a default speed that may need to be changed
- //note that there is a 90 degree offset in the angle as sent over serial
- int go(){
- delay(100); //wait to make sure the rest of the command arrives
- int readDest[8];
- if (Serial.available() >= 8) { //read the array
- for (int i = 0; i<8; i++){
- readDest[i]=Serial.read()-90; //90 degree shift since unsigned over serial
- }
- }
- else{
- Serial.println("Go command FUBAR -- NO DEAL!");
- return 0;
- }
- //space reserved for sanity checking...
- goTo(readDest, 10, 30);
- }
- //this was the test command
- //depreciated
- int flex(){
- for (int i = 15-45; i < 15+45; i+=5){
- int stepPos[8] = {i,40,i,40,i,40,i,40}; //start position
- goTo(stepPos, 10, 30);
- }
- }
- //reports the current servo positions over the serial
- int reportPos(){
- Serial.println("Current positions as follows");
- for (int i = 0; i < 8; i++){
- Serial.println(servoPos[i]);
- }
- }
- //idiot proof error state condition
- //snaps direct to position
- //--may be rough on the bot
- int standOld(){
- servoSet(FLU, 0);
- servoSet(FRU, 0);
- servoSet(FLL, 40);
- servoSet(FRL, 40);
- servoSet(BLU, 0);
- servoSet(BRU, 0);
- servoSet(BLL, 40);
- servoSet(BRL, 40);
- }
- //goes to a desired position in a smooth action
- //disregards the legacy parameters -> users global variables and calculates
- int goTo(int desired[], int stepNumOld, int waitTimeOld){
- //we will now be ignoring the legacy stepNum and calculating our own
- int maxDelta = 0;
- for (int i = 0; i<8; i++){
- if (abs(servoPos[i]-desired[i]) >= maxDelta){
- maxDelta = abs(servoPos[i]-desired[i]);
- }
- }
- //disregard the old waitTime
- int waitTime = stepTime;
- int stepNum = maxDelta/degreesPerStep;
- //calculate step size
- int steps[8];
- steps[0] = (servoPos[0]-(desired[0]))/-stepNum;
- steps[1] = (servoPos[1]-(desired[1]))/-stepNum;
- steps[2] = (servoPos[2]-(desired[2]))/-stepNum;
- steps[3] = (servoPos[3]-(desired[3]))/-stepNum;
- steps[4] = (servoPos[4]-(desired[4]))/-stepNum;
- steps[5] = (servoPos[5]-(desired[5]))/-stepNum;
- steps[6] = (servoPos[6]-(desired[6]))/-stepNum;
- steps[7] = (servoPos[7]-(desired[7]))/-stepNum;
- //do the stepping
- for (int j = 0; j < stepNum; j++){
- servoSet(FRU, servoPos[0]+steps[0]);
- servoSet(FRL, servoPos[1]+steps[1]);
- servoSet(FLU, servoPos[2]+steps[2]);
- servoSet(FLL, servoPos[3]+steps[3]);
- servoSet(BRU, servoPos[4]+steps[4]);
- servoSet(BRL, servoPos[5]+steps[5]);
- servoSet(BLU, servoPos[6]+steps[6]);
- servoSet(BLL, servoPos[7]+steps[7]);
- delay(waitTime);
- }
- //set direct to account for error
- servoSet(FRU, desired[0]);
- servoSet(FRL, desired[1]);
- servoSet(FLU, desired[2]);
- servoSet(FLL, desired[3]);
- servoSet(BRU, desired[4]);
- servoSet(BRL, desired[5]);
- servoSet(BLU, desired[6]);
- servoSet(BLL, desired[7]);
- }
- //puts the servos all in a safe standing position
- int stand(){
- int standPos[8] = {0,40,0,40,0,40,0,40};
- goTo(standPos, 10, 30);
- }
- //sets the bot down on it's belly
- int down(){
- int downPos[8] = {0,-45,0,-45,0,-45,0,-45};
- goTo(downPos, 10, 30);
- }
- //the ascii commands are converted to ints on the serial
- //comparison is done vs their equivalent number
- int takeCommand(int input){
- switch (input){
- case 115: //ascii 's' for stand
- Serial.println("standing up");
- stand();
- break;
- case 100: //ascii 'd' for sit Down
- Serial.println("belly flop");
- down();
- break;
- case 114: //ascii 'r' for report
- Serial.println("report");
- reportPos();
- break;
- case 101: //ascii 'e' for error state
- Serial.println("idiot proofing");
- standOld();
- break;
- case 102: //'f' for forward step
- forwardStep();
- break;
- case 116: //'t' for test pose
- flex();
- break;
- case 103: //'g' for go - it is anticiated this will be the main command
- go();
- break;
- //for the IR stuff
- //hard coded steps for remote control
- case 70:
- forwardStep();
- break;
- case 66:
- backwardStep();
- break;
- case 82:
- turnRight();
- break;
- case 76:
- turnLeft();
- break;
- default: //this will tell you the code of what you entered if you listen on the serial
- //Serial.print("I don't know what this means:");
- // Serial.println(input);
- return 0;
- }
- }
- void loop()
- {
- //This uses IR control
- //takeCommand(getIR());
- //getIR();
- // this uses serial control
- if (Serial.available() > 0) {
- // get incoming byte:
- inByte = Serial.read();
- takeCommand(inByte);
- Serial.println("got it"); //an ack that includes a 'g' is needed for the simple interface
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement