arduino stuffs
works are in progress
| -rw-r--r-- | .DS_Store | bin | 8196 -> 8196 bytes | |||
| -rw-r--r-- | car_driver/car_driver.ino | 129 | ||||
| -rw-r--r-- | car_driver/move.ino | 241 | ||||
| -rw-r--r-- | libraries/.DS_Store | bin | 6148 -> 6148 bytes | |||
| -rwxr-xr-x | libraries/RobotMotor/RobotMotor2wd/RobotMotor.cpp | 1 |
5 files changed, 371 insertions, 0 deletions
| Binary files differ diff --git a/car_driver/car_driver.ino b/car_driver/car_driver.ino new file mode 100644 index 0000000..b6b24d2 --- /dev/null +++ b/car_driver/car_driver.ino @@ -0,0 +1,129 @@ + +#include <AFMotor.h> // adafruit motor shield library +#include "RobotMotor.h" // 2wd or 4wd motor library + +#define HEADER 'H' + +#define TOTAL_BYTES 8// hmmm +#define number_of_keys 4 + +enum {MOV_LEFT, MOV_RIGHT, MOV_FORWARD, MOV_BACK, MOV_ROTATE, MOV_STOP}; +const char tags[] = {'w', 'a', 's', 'd'}; +const char* states[] = {"Left", "Right", "Forward", "Back", "Rotate", "Stop"}; +const int DIR_LEFT = 0; +const int DIR_RIGHT = 1; +const int DIR_CENTER = 2; + +const char* locationString[] = {"Left", "Right", "Center"}; // labels for debug + +const int LED_PIN = 13; + +int commandState = MOV_STOP; // what robot is told to do + + +const char MOVE_FORWARD = 'w'; // move forward +const char MOVE_LEFT = 'a'; // move left +const char MOVE_RIGHT = 'd'; // move right +// const char PIVOT_CCW = '?'; // rotate 90 degrees CCW +// const char PIVOT_CW = '?'; // rotate 90 degrees CW +// const char PIVOT = '?'; // rotation angle (minus rotates CCW) + +// not used in this example +// const char MOVE_SPEED = '?'; +// const char MOVE_SLOWER = '?'; // reduce speed +// const char MOVE_FASTER = '?'; // increase speed + +void setup() { + Serial.begin(9600); + Serial.println("initialized"); + moveBegin(); + moveSetSpeed(MIN_SPEED + 40); +} + +void loop() { + if ( Serial.available() >= TOTAL_BYTES) { + // sample format: Hw0a0s1d0 + if (Serial.read() == HEADER) { + for(int i = 0; i <= number_of_keys * 2; i++) { + char tag = Serial.read(); // read the first tag + if (tags[i / 2] == tag){ // perhaps works + char key = Serial.read(); + Serial.print(tag); Serial.print(": "); Serial.println(key); // debug + processCommand(tags[i], key); + } + else { + Serial.write(tag); + Serial.println( ": unknown tag"); + } + } + } + } + else { + Serial.print("missing header: "); + Serial.println(HEADER); + } + } +} + +void changeCmdState(int newState) +{ + if(newState != commandState) + { + // Serial.print("Changing Cmd state from "); Serial.print( states[commandState]); // dont need this logged + // Serial.print(" to "); Serial.println(states[newState]); + commandState = newState; + } +} + + +void processCommand(int cmd, int pressed) +{ + moveStop(); + changeCmdState(MOV_STOP); + // byte speed; + //Serial.write(cmd); // uncomment to echo + + switch(cmd) + { + case MOVE_LEFT : { + if(pressed == 1) { + changeCmdState(MOV_ROTATE); + moveRotate(-10); + break; + } + } + case MOVE_RIGHT : { + if(pressed == 1) { + changeCmdState(MOV_ROTATE); + moveRotate(10); + break; + } + } + case MOVE_FORWARD : {if(pressed == 1) {changeCmdState(MOV_FORWARD); moveForward(); break;}} + case 's' : { // s is complicated, cause im trying to do this without 40 buttons + if (pressed == 1){ + if (commandState != MOV_STOP) { + changeCmdState(MOV_STOP); + moveStop(); + break; + } + else if (commandState == MOV_STOP) { + changeCmdState(MOV_BACK); + moveBackward(); + break; + } + } + } + + // case PIVOT_CCW : changeCmdState(MOV_ROTATE); moveRotate(-90); break; + // case PIVOT_CW : changeCmdState(MOV_ROTATE); moveRotate(90); break; +// case PIVOT : changeCmdState(MOV_ROTATE); moveRotate(val); break; +// case MOVE_SPEED : speed = val; moveSetSpeed(speed); break; +// case SLOWER : moveSlower(speedIncrement); break; +// case FASTER : moveFaster(speedIncrement); break; + case '\r' : case '\n': break; // ignore cr and lf + default : Serial.print('['); Serial.write(cmd); Serial.println("] Ignored"); break; + } +} + + diff --git a/car_driver/move.ino b/car_driver/move.ino new file mode 100644 index 0000000..e407420 --- /dev/null +++ b/car_driver/move.ino @@ -0,0 +1,241 @@ +/************************************* + Drive: mid level movement functions +*************************************/ + +int moveState = MOV_STOP; // what robot is doing + +int moveSpeed = 0; // move speed stored here (0-100%) +int speedIncrement = 10; // percent to increase or decrease speed + +void moveBegin() +{ + motorBegin(MOTOR_LEFT); + motorBegin(MOTOR_RIGHT); + moveStop(); +} + +void moveLeft() +{ + changeMoveState(MOV_LEFT); + motorForward(MOTOR_LEFT, 0); + motorForward(MOTOR_RIGHT, moveSpeed); + Serial.println("were going left... maybe????"); +} + +void moveRight() +{ + changeMoveState(MOV_RIGHT); + motorForward(MOTOR_LEFT, moveSpeed); + motorForward(MOTOR_RIGHT, 0); + Serial.println("were going right... maybe????"); +} + +void moveForward() +{ + changeMoveState(MOV_FORWARD); + motorForward(MOTOR_LEFT, moveSpeed); + motorForward(MOTOR_RIGHT, moveSpeed); +} + +void moveBackward() +{ + changeMoveState(MOV_BACK); + motorReverse(MOTOR_LEFT, moveSpeed); + motorReverse(MOTOR_RIGHT, moveSpeed); +} + +void moveRotate(int angle) +{ + changeMoveState(MOV_ROTATE); + Serial.print("Rotating "); Serial.print(angle); + if(angle < 0) + { + Serial.println(" (left)"); + motorReverse(MOTOR_LEFT, moveSpeed); + motorForward(MOTOR_RIGHT, moveSpeed); + angle = -angle; + } + else if(angle > 0) + { + Serial.println(" (right)"); + motorForward(MOTOR_LEFT, moveSpeed); + motorReverse(MOTOR_RIGHT, moveSpeed); + } + int ms = rotationAngleToTime(angle, moveSpeed); + movingDelay(ms); + moveBrake(); +} + +void moveStop() +{ + changeMoveState(MOV_STOP); + motorStop(MOTOR_LEFT); + motorStop(MOTOR_RIGHT); + Serial.println("stop lmao"); +} + +void moveBrake() +{ + changeMoveState(MOV_STOP); + motorBrake(MOTOR_LEFT); + motorBrake(MOTOR_RIGHT); +} + +void moveSetSpeed(int speed) +{ + motorSetSpeed(MOTOR_LEFT, speed) ; + motorSetSpeed(MOTOR_RIGHT, speed) ; + moveSpeed = speed; // save the value +} + +void moveSlower(int decrement) +{ + Serial.print(" Slower: "); + if( moveSpeed >= speedIncrement + MIN_SPEED) + moveSpeed -= speedIncrement; + else moveSpeed = MIN_SPEED; + moveSetSpeed(moveSpeed); +} + + +void moveFaster(int increment) +{ + Serial.print(" Faster: "); + moveSpeed += speedIncrement; + if(moveSpeed > 100) + moveSpeed = 100; + moveSetSpeed(moveSpeed); +} + +int moveGetState() +{ + return moveState; +} + +/************************************* + functions to rotate the robot +*************************************/ + +// return the time in milliseconds to turn the given angle at the given speed +long rotationAngleToTime( int angle, int speed) +{ +int fullRotationTime; // time to rotate 360 degrees at given speed + + if(speed < MIN_SPEED) + return 0; // ignore speeds slower then the first table entry + + angle = abs(angle); + + if(speed >= 100) + fullRotationTime = rotationTime[NBR_SPEEDS-1]; // the last entry is 100% + else + { + int index = (speed - MIN_SPEED) / SPEED_TABLE_INTERVAL ; // index into speed and time tables + int t0 = rotationTime[index]; + int t1 = rotationTime[index+1]; // time of the next higher speed + fullRotationTime = map(speed, speedTable[index], speedTable[index+1], t0, t1); + // Serial.print("index= "); Serial.print(index); Serial.print(", t0 = "); Serial.print(t0); Serial.print(", t1 = "); Serial.print(t1); + } + // Serial.print(" full rotation time = "); Serial.println(fullRotationTime); + long result = map(angle, 0,360, 0, fullRotationTime); + return result; +} + + +// rotate the robot from MIN_SPEED to 100% increasing by SPEED_TABLE_INTERVAL +void calibrateRotationRate(int direction, int angle) +{ + Serial.print(locationString[direction]); + Serial.println(" calibration" ); + for(int speed = MIN_SPEED; speed <= 100; speed += SPEED_TABLE_INTERVAL) + { + + delay(1000); + //blinkNumber(speed/10); + + if( direction == DIR_LEFT) + { // rotate left + motorReverse(MOTOR_LEFT, speed); + motorForward(MOTOR_RIGHT, speed); + } + else if( direction == DIR_RIGHT) + { // rotate right + motorForward(MOTOR_LEFT, speed); + motorReverse(MOTOR_RIGHT, speed); + } + else + Serial.println("Invalid direction"); + + + int time = rotationAngleToTime(angle, speed); + + Serial.print(locationString[direction]); + Serial.print(": rotate "); + Serial.print(angle); + Serial.print(" degrees at speed "); + Serial.print(speed); + Serial.print(" for "); + Serial.print(time); + Serial.println("ms"); + delay(time); + motorStop(MOTOR_LEFT); + motorStop(MOTOR_RIGHT); + delay(2000); // two second delay between speeds + } +} + +// this is the low level movement state. +// it will differ from the command state when the robot is avoiding obstacles +void changeMoveState(int newState) +{ + if(newState != moveState) + { + Serial.print("Changing move state from "); Serial.print( states[moveState]); + Serial.print(" to "); Serial.println(states[newState]); + moveState = newState; + } +} + +/************* high level movement functions ****************/ + +//moves in the given direction at the current speed for the given duration in milliseconds +void timedMove(int direction, int duration) +{ + Serial.print("Timed move "); + if(direction == MOV_FORWARD) { + Serial.println("forward"); + moveForward(); + } + else if(direction == MOV_BACK) { + Serial.println("back"); + moveBackward(); + } + else + Serial.println("?"); + + movingDelay(duration); + moveStop(); +} + +// check for obstacles while delaying the given duration in ms + +void movingDelay(long duration) +{ + long startTime = millis(); + while(millis() - startTime < duration) { + // function in Look module checks for obstacle in direction of movement + if(checkMovement() == false) { + if( moveState != MOV_ROTATE) // rotate is only valid movement + { + Serial.println("Stopping in moving Delay()"); + moveBrake(); + } + } + } +} + +boolean checkMovement() { + return true; +} + + diff --git a/libraries/.DS_Store b/libraries/.DS_Store Binary files differindex aa223dc..d3d41a7 100644 --- a/libraries/.DS_Store +++ b/libraries/.DS_Store diff --git a/libraries/RobotMotor/RobotMotor2wd/RobotMotor.cpp b/libraries/RobotMotor/RobotMotor2wd/RobotMotor.cpp index caa9939..6c8ab03 100755 --- a/libraries/RobotMotor/RobotMotor2wd/RobotMotor.cpp +++ b/libraries/RobotMotor/RobotMotor2wd/RobotMotor.cpp @@ -66,6 +66,7 @@ void motorStop(int motor) { // todo set speed to 0 ??? motors[motor].run(RELEASE); // stopped + motorSetSpeed(0) // todo done!? } void motorBrake(int motor) |