Hi Guys… let me preface with I am a Arduino noob, and a serial communications noob… I am just hacking my way thru things.
I work in Maya software from autodesk, and have been working on an interface between it and my robots. I know there is already “ServoTools for Maya” but that one is limited by servos being hard coded into the arduino sketch.
I have written a node in maya/python that is a lot more open than servoTools. I have it working with my Robotis USB2Dynamixel now, and I am trying to add the same kind of functionality to an arduino controlled robot.
Basically what I need is to be able to send a start byte to the arduino, then a servo ID (pin number for arduino) then the value that the servo needs to be.
This is what I have so far… I ripped some of the SerialLCD stuff because I have a matrix orbital LK204-24 connected to my arduino mega for debugging readout.
arduino code
int mode = 0; // where we are in the frame
unsigned char id = 0; // id of servo
int pwmSend;
void setup()
{
Serial.begin(38400);
Serial1.begin(19200);
clearLCD();
// Turn OFF the block cursor
// Note that setting both block and underline
// cursors may give unpredictable results.
Serial1.print(254, BYTE);
Serial1.print(84, BYTE);
// Turn ON the underline cursor
Serial1.print(254, BYTE);
Serial1.print(74, BYTE);
}
// MAIN CODE
void loop()
{
cursorHome();
Serial1.print("WFSB");//waiting for start byte
//not using cursorSet cause it doesnt work, just resets position to 1,1
cursorRight();cursorRight();cursorRight();cursorRight();
Serial1.print("ID is ");
newLine();
while(Serial.available() > 0){
// We need to 0xFF at start of packet
if(mode == 0){ // start of new packet
if(Serial.read() == 0xFF){
mode = 2;
newLine();
Serial1.print("GSB");//Got Start Byte
cursorRight();cursorRight();cursorRight();cursorRight();cursorRight();
}
}else if(mode == 2){ // next is index of servo
id = Serial.read();
if(id != 0xFF)
mode = 3;
Serial1.print(id);
newLine();
}
else if(mode == 3){ // next byte is length
if(Serial.read() == 0xFF){
pwmSend = Serial.read();
mode = 4;
Serial1.print("got pwm ");
Serial1.print(pwmSend);
}
}
// print text to the current cursor position
// start a new line
}
}
// LCD FUNCTIONS-- keep the ones you need.
// clear the LCD
void clearLCD(){
Serial1.print(12, BYTE);
}
// start a new line
void newLine() {
Serial1.print(10, BYTE);
}
// move the cursor to the home position
void cursorHome(){
Serial1.print(254, BYTE);
Serial1.print(72, BYTE);
}
// move the cursor to a specific place
// e.g.: cursorSet(3,2) sets the cursor to x = 3 and y = 2
void cursorSet(int xpos, int ypos){
Serial1.print(254, BYTE);
Serial1.print(71, BYTE);
Serial1.print(xpos); //Column position
Serial1.print(ypos); //Row position
}
// backspace and erase previous character
void backSpace() {
Serial1.print(8, BYTE);
}
// move cursor left
void cursorLeft(){
Serial1.print(254, BYTE);
Serial1.print(76, BYTE);
}
// move cursor right
void cursorRight(){
Serial1.print(254, BYTE);
Serial1.print(77, BYTE);
}
// set LCD contrast
void setContrast(int contrast){
Serial1.print(254, BYTE);
Serial1.print(80, BYTE);
Serial1.print(contrast);
}
// turn on backlight
void backlightOn(int minutes){
Serial1.print(254, BYTE);
Serial1.print(66, BYTE);
Serial1.print(minutes); // use 0 minutes to turn the backlight on indefinitely
}
// turn off backlight
void backlightOff(){
Serial1.print(254, BYTE);
Serial1.print(70, BYTE);
}
here is the python code that sends the serial data to the arduino
def moveServo(servo, angle):
''' A function for setting the servo number and its angle.
Example:
>>> servo.move(1,180)'''
if (0 <= angle <= 180):
if angle != lastAngle[servo -1]:
ser.write(0xFF)
ser.write(chr(servo))
ser.write(0xFF)
ser.write(chr(angle))
ser.write(chr(13))
lastAngle[servo -1] = angle
#print "running serial write"
#print "servo " + str(servo) + " " + str(angle)
else:
print "Servo angle must be an integer between 0 and 180.\n"
return lastAngle[servo -1]
the ser.write(chr(13)) was a test to see if sending a different character would be easier
anyone have any insight on how I can get this to work?
I thought I was close but am getting stuck