Skip to content

Instantly share code, notes, and snippets.

@prafulfillment
Last active August 29, 2015 13:56
Show Gist options
  • Save prafulfillment/8809624 to your computer and use it in GitHub Desktop.
Save prafulfillment/8809624 to your computer and use it in GitHub Desktop.
import serial
ser = serial.Serial('<USB tty>', 9600)
"""
`Forward` is constant 85 to `Acceleration Servo` value
`Backward` is constant 103 to `Acceleration Servo` value
`Stop` is constant 90 to `Acceleration Servo` value
Before moving `Forward` or `Backward`, the `Acceleration Servo` is `Stop`ped for 1 second
to reduce the problems with the car locking up.
Right is +5 to the `Steering Servo` value
Left is -5 to `Steering Servo` value
"""
ser.write('F') # Forward
ser.write('L') # Left
ser.write('B') # Backward
ser.write('S') # Stop
ser.write('R') # Right
# these can be combined
ser.write('FRRR') # forward with 3 rights
#include <Servo.h>
Servo steering_servo;
Servo acceleration_servo;
// PIN CONSTANTS:
int TURNING_PIN = 8;
int ACCELERATION_PIN = 9;
// STEERING CONSTANTS:
// Steering servo has range of MAX_LEFT - MAX_RIGHT
int MAX_RIGHT = 110;
int MAX_LEFT = 40;
int TURN_RADIUS = 5;
// ACCELERATION CONSTANTS:
int ACCELERATION = 83;
// INIT VARS:
int turning_angle = 75; // steering mid-point
int message;
int pos = 0;
void setup()
{
Serial.begin(9600);
steering_servo.attach(TURNING_PIN);
steering_servo.write(turning_angle);
acceleration_servo.attach(ACCELERATION_PIN);
acceleration_servo.write(ACCELERATION);
}
int turn(int dir){
int curr_angle = turning_angle;
int curr_angle_prime = curr_angle + (TURN_RADIUS * dir);
if(curr_angle_prime > MAX_LEFT && curr_angle_prime < MAX_RIGHT){
int pos = curr_angle;
while(pos!=curr_angle_prime){
steering_servo.write(pos);
pos+=dir;
delay(5);
}
curr_angle = curr_angle_prime;
}
return curr_angle;
}
int accel(int a){
acceleration_servo.write(a);
}
void loop()
{
if( Serial.available() > 0 ) {
message = Serial.read();
Serial.flush();
Serial.println( "ACK" );
Serial.flush();
// Left turn
if( message == 'L' ) {
turning_angle = turn(-1);
}
// Right turn
if( message == 'R' ) {
turning_angle = turn(1);
}
// Forwards
if( message == 'F' ) {
accel(90); // stop or the car brakes and must be reset
delay(1000);
accel(85);
}
// Backwards
if( message == 'B' ) {
accel(90);
delay(1000);
accel(103);
}
// Stop
if( message == 'S' ) {
accel(90);
}
// Explore
if( message == 'E' ) {
// NOTHING;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment