Biped robot code,
Example python code to talk to the Arduino code.
#!/usr/bin/python
import time, sys, os, serial
#set up the serial port for action (0==COM1==ttyS0)
ser=serial.Serial('/dev/ttyUSB0')
ser.baudrate=9600
packet=0
def setpos(n,angle):
packet=chr(255)+chr(n)+chr(angle)
ser.write(packet)
print "Please, give me a direction F for forward,L for left turn,R for right turn"
print "Capitol F for run forward"
readkey = None
while readkey != "x":
readkey=input('Need servo number 0-8:')
print "Result:"+str(readkey)
if (readkey != "x"):
pos=input('Need position:' )
setpos(readkey,pos)
if readkey == "x":
print "End Of Line!"
Arduino servo controller code, thanks to principallabs for the baseline..
/* * MultipleSerialServoControl * -------------- * * Uses the Arduino Serial library * (http://arduino.cc/en/Reference/Serial) * and the Arduino Servo library * (http://arduino.cc/en/Reference/Servo) * to control multiple servos from a PC using a USB cable. * * Designed for use with the Python servo.py module * (http://principialabs.com/arduino-python-4-axis-servo-control/) * * REQUIRED: Arduino 0017 or higher * Control up to 48 servos with Arduino Mega! * See the "TO ADD SERVOS:" comments below. * * Created 23 December 2009 * copyleft 2009 Brian D. Wendt * http://principialabs.com/ * */ // Import the Arduino Servo library #include#define NBR_SERVOS 8 #define FIRST_SERVO_PIN 2 // Create a Servo object for each servo // TO ADD SERVOS: // Servo servo5; // etc... // Common servo setup values int minPulse = 700; // minimum servo position, us (microseconds) int maxPulse = 2300; // maximum servo position, us // // User input for servo and position int userInput[3]; // raw input from serial buffer, 3 bytes int startbyte; // start byte, begin reading input int pos = 0; // servo angle 0-180 int servo; int i; // iterator int v; Servo Servos[NBR_SERVOS]; int lpos[NBR_SERVOS]; void setup() { for( int i =0; i < NBR_SERVOS; i++) { Servos[i].attach( FIRST_SERVO_PIN +i); } // Open the serial connection, 9600 baud Serial.begin(9600); //Sweep test Serial.println ("Sweep test"); for( int i=0; i { Serial.print("Servo,"); Serial.print(i); Serial.print(","); Serial.println(0); Servos[i].write(0); // tell servo to go to position in variable 'pos' delay(1000); lpos[i]=pos; Serial.print("Servo,"); Serial.print(i); Serial.print(","); Serial.println(45); Servos[i].write(45); // tell servo to go to position in variable 'pos' delay(1000); lpos[i]=pos; Serial.print("Servo,"); Serial.print(i); Serial.print(","); Serial.println(90); Servos[i].write(90); // tell servo to go to position in variable 'pos' delay(1000); lpos[i]=pos; Serial.print("Servo,"); Serial.print(i); Serial.print(","); Serial.println(135); Servos[i].write(135); // tell servo to go to position in variable 'pos' delay(1000); lpos[i]=pos; Serial.print("Servo,"); Serial.print(i); Serial.print(","); Serial.println(180); Servos[i].write(180); // tell servo to go to position in variable 'pos' delay(1000); lpos[i]=pos; Serial.print("Servo,"); Serial.print(i); Serial.print(","); Serial.println(90); Servos[i].write(90); // tell servo to go to position in variable 'pos' delay(1000); lpos[i]=pos; } int i = 0; } void loop() { // Wait for serial input (min 3 bytes in buffer) if (Serial.available() > 2) { // Read the first byte startbyte = Serial.read(); // If it's really the startbyte (255) ... Serial.println ("checking serial"); if (startbyte == 255) { Serial.print ("Got servo cmd"); // ... then get the next two bytes for (i=0;i<2;i++) { userInput[i] = Serial.read(); } // First byte = servo to move? servo = userInput[0]; // Second byte = which position? pos = userInput[1]; // Packet error checking and recovery if (pos == 255) { servo = 255; } // Assign new position to appropriate servo if (servo <= NBR_SERVOS) { Servos[servo].write(pos); delay (15); lpos[servo]=pos; Serial.print (startbyte); Serial.print (","); Serial.print (servo); Serial.print (","); Serial.print (pos); Serial.println ("Sent"); } } if (startbyte == 254) { // ... state stub } } } ##Blog Glitch##
0 comments:
Post a Comment