Timing the controls to be automated is creating issues because the batteries and being drained, and therefore slowing down the motors, and changing the timing.
To combat this instead of Butlerbot controlling himself, he can be controlled over the USB cable via a computer. This requires two codes: one that runs on the arduino board and operates the motors based on what it receives, and another that runs through Processing on the computer to send the inputs.
In terms of a power system, we should plan on plugging it into the wall with our 12V 850mA adapter, instead of the batteries, simply because the extra boost allows it to climb more smoothly.
Keyboard control layout is:
WASD keys control the general movement, with QE to provide minor adjustments to it alignment.
YUI make the robot lift on the racks, HJK make the robot lower, and NM"," releases the stepper motors.
It has three travel speeds: 10, 20, and 30 that are keyed in by the number pad.
Full Arduino Code:
***********************************************
//Includes libraries
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
//Assigns motor shield addresses
Adafruit_MotorShield AFMSb(0x60);
Adafruit_MotorShield AFMSt(0x61);
//Motors on bottom board:
// Stepper Motor on M1 and M2
Adafruit_StepperMotor *FMotor = AFMSb.getStepper(200, 1);
// Stepper Motor on M3 and M4
Adafruit_StepperMotor *BMotor = AFMSb.getStepper(200, 2);
//Motors on top board:
//DC Motor on M4
Adafruit_DCMotor *LMotor = AFMSt.getMotor(4);
//DC Motor on M1
Adafruit_DCMotor *RMotor = AFMSt.getMotor(1);
//Sets lift motor directions
int up = FORWARD;
int down = BACKWARD;
//Sets motor speeds
int liftSpeed = 50;
int moveSpeed = 30;
//Serial input
int input=0;
void setup() {
//Starts
communication with computer
Serial.begin(9600);
//Starts motor
shields
AFMSb.begin();
AFMSt.begin();
}
void loop(){
if
(Serial.available()){
input =
Serial.read();
switch (input){
//Move forward, W
case 'W':
setMoveSpeed();
LMotor->run(BACKWARD);
RMotor->run(BACKWARD);
break;
//Turn left, A
case 'A':
setMoveSpeed();
LMotor->run(BACKWARD);
RMotor->run(FORWARD);
break;
//Turn backward,
S
case 'S':
setMoveSpeed();
LMotor->run(FORWARD);
RMotor->run(FORWARD);
break;
//Turn right, D
case 'D':
setMoveSpeed();
LMotor->run(FORWARD);
RMotor->run(BACKWARD);
break;
//Slow turn
left, Q
case 'Q':
RMotor->setSpeed(10);
RMotor->run(FORWARD);
break;
//Slow turn
right, E
case 'E':
LMotor->setSpeed(10);
LMotor->run(FORWARD);
break;
//Lift front
rack, H
case 'H':
setLiftSpeed();
FMotor->step(1,down,DOUBLE);
break;
//Lower front
rack, Y
case 'Y':
setLiftSpeed();
FMotor->step(1,up,DOUBLE);
break;
//Lift back
rack, J
case 'J':
setLiftSpeed();
BMotor->step(1,down,DOUBLE);
break;
//Lower back
rack, U
case 'U':
setLiftSpeed();
BMotor->step(1,up,DOUBLE);
break;
//Lift both
racks, K
case 'K':
setLiftSpeed();
BMotor->step(1,down,DOUBLE);
FMotor->step(1,down,DOUBLE);
break;
//Lower both
racks, I
case 'I':
setLiftSpeed();
BMotor->step(1,up,DOUBLE);
FMotor->step(1,up,DOUBLE);
break;
//Release both
racks, N
case 'N':
FMotor->release();
break;
//Release back
rack, M
case 'M':
BMotor->release();
break;
//Release both
racks, ,
case ',':
FMotor->release();
BMotor->release();
break;
//Speed 10
case '1':
moveSpeed =
10;
break;
//Speed 20
case '2':
moveSpeed =
20;
break;
//Speed 30
case '3':
moveSpeed =
30;
break;
//Default,
releases DC motors and holds steppers
case '0':
FMotor->setSpeed(0);
BMotor->setSpeed(0);
LMotor->run(RELEASE);
RMotor->run(RELEASE);
break;
}
}
}
void setMoveSpeed(){
LMotor->setSpeed(moveSpeed);
RMotor->setSpeed(moveSpeed);
}
void setLiftSpeed(){
BMotor->setSpeed(liftSpeed);
FMotor->setSpeed(liftSpeed);
}
Processing Code:
***********************************************
import processing.serial.*;
Serial port;
PImage img;
void setup(){
println(Serial.list());
port = new
Serial(this, Serial.list()[0], 9600);
size (1563, 450);
img =
loadImage("Control Keys.PNG");
}
void draw() {
image(img, 0, 0);
if (keyPressed){
switch (key){
case 'w': case
'W': port.write('W');break;
case 's': case
'S': port.write('S');break;
case 'a': case
'A': port.write('A');break;
case 'd': case
'D': port.write('D');break;
case 'q': case
'Q': port.write('Q');break;
case 'e': case
'E': port.write('E');break;
case 'y': case
'Y': port.write('Y');break;
case 'h': case
'H': port.write('H');break;
case 'u': case
'U': port.write('U');break;
case 'j': case
'J': port.write('J');break;
case 'i': case
'I': port.write('I');break;
case 'k': case
'K': port.write('K');break;
case 'n': case 'N':
port.write('N');break;
case 'm': case
'M': port.write('M');break;
case ',': case
'<':port.write(',');break;
case '1':
port.write('1');break;
case '2':
port.write('2');break;
case '3':
port.write('3');break;
default:
port.write('0');break;
}
}
else
port.write('0');
}
No comments:
Post a Comment