All pastes #899279 Raw Edit

Anonymous

public text v1 · immutable
·published 2008-02-11 00:43 UTC
rendered paste body
-> cat ssc32.py
#!/usr/bin/python
# Import the Serial module
import serial, time;

EOL = "\r";
command = "";

# Set the SSC-32 port - "COM1:" for Windows, "/dev/ttyS0" for Linux
SSC32_Port = "/dev/ttyS0";

LeftMChan = 0;
LeftMStop = 1530;

RightMChan = 1;
RightMStop = 1535;

PanChan = 2;
PanHome = 1530;
PanREnd = 2150;
PanLEnd = 900;

TiltChan = 3;
TiltHome = 1430;
TiltUEnd = 500;
TiltDEnd = 1550;

# Reads single characters until a CR is read
def Response (port):
        ich = "";
        resp = "";

        while (ich <> '\r'):
                ich = port.read(1);

                if (ich <> '\r'):
                        resp = resp + ich;
        return resp;


# Converts a servo position in degrees to uS for the SSC-32
def To_Degrees (uS):
        result = 0;

        result = (uS - 1500) / 10;

        return result;

# Converts an SSC-32 servo position in uS to degrees
def To_uS (degrees):
        result = 0;

        result = (degrees * 10) + 1500;

        return result;

# Wait for a servo move to be completed
def Wait_for_Servos (port):

        ich = "";

        while (ich <> "."):
                Send_Command (port, "Q", True);

                time.sleep(5);                          # Wait 5ms for a response to be ready

                ich = port.read(1);

                time.sleep(1000);                       # Wait one second
        return;

# Send EOL to the SSC-32 to start a command executing
def Send_EOL (port):

        result = port.write(EOL);

        return result;

# Send a command to the SSC-32 with or without an EOL
def Send_Command (port, cmd, sendeol):

        result = port.write (cmd);

        if (sendeol):
                Send_EOL (port);

        return result;

#
# Start of mainline code
#

#       Open the port at 115200 Bps - defaults to 8N1
ssc32 = serial.Serial(SSC32_Port, 115200);

#       Motors All Stop - Left = Ch0, Right = Ch1
command = "#" + str(LeftMChan) + " P" + str(LeftMStop) + " #" + str(RightMChan) + " P" + str(RightMStop);
Send_Command (ssc32, command, True);

Wait_for_Servos (ssc32);

#       Home the PING Pan/Tilt Base
command = "S25 #" + str(PanChan) + " P" + str(PanHome) + " #" + str(TiltChan) + " P" + str(TiltHome) +  "T6000";
Send_Command (ssc32, command, True);

Wait_for_Servos (ssc32);

#       Send the version command
command = "ver";
Send_Command (ssc32, command, True);

#       Read the response
inp = Response(ssc32);

#       Show what we got back
print inp;

command = "S25 #" + str(PanChan) + "P1900" + "#" + str(TiltChan) + "P500 T6000"
Send_Command (ssc32, command, True);

Wait_for_Servos (ssc32);

#       Home the PING Pan/Tilt Base
command = "S25 #" + str(PanChan) + " P" + str(PanHome) + " #" + str(TiltChan) + " P" + str(TiltHome) +  "T6000";
Send_Command (ssc32, command, True);

Wait_for_Servos (ssc32);

#       Close the port
ssc32.close();