Wednesday, May 23, 2012

Python Submarine Class

A class for controlling the submarine has been completed. Using the functions turnLeft, turnRight, goStraight, ascend, descend, clawopen, clawclose, and stop, a Python program can autonomously control the onboard motors.

Submarine( port, baud, cmd_size ):
This is the class constructor for the Submarine. The port is the serial port to which the motor controller is connected. The baud is the baud rate for the serial port. cmd_size is the length of the commands to be sent to the motor controller. The default for cmd_size is 8.

turnLeft( int ):
Taking a parameter between 0 and 100, the RoboSub will turn left using the appropriate number of thrusters. If the power is between 1 and 50, the left motor will move in "reverse." For power levels greater than 50, the right motor will push forward. At 100, both motors are operating at full power in opposite directions.

turnRight( int ):
Identical to turnLeft, except the motor roles are reversed.

goStraight( int ):
This takes a parameter between -100 and 100. At -100, the left and right motors will be at full power in reverse, and at 100, the motors will be at full power forward.

ascend(  ):
This forces the climb motor to draw the RoboSub toward the surface.

descend(  ):
This brings the RoboSub lower into the water.

clawopen(  ):
This will open the claw and shut off the motor after a brief moment.

clawclose(  ):
This will close the claw and shut off the motor after a brief moment.

stop(  ):
This shuts off all motors and the robot should start floating if it is positively buoyant. Depending on time, this may change to allow the climb motor to counteract the buoyant force and maintain its current depth. ( Ideal )

Usage is as follows:

import submarineclass
import time

RoboSub = submarineclass.Submarine( 9, 2400 )
RoboSub.turnRight( 25 )
time.sleep( 1 )
RoboSub.goStraight( -90 )
time.sleep( 5 )
RoboSub.stop(  )
RoboSub.clawopen(  )
RoboSub.ascend(  )
time.sleep( .4 )
RoboSub.stop(  )
RoboSub.clawclose(  )

This should turn the RoboSub right for 1 second, then move backward at 90% power for 5 seconds, open the claw, move up a bit, then close the claw.

No comments:

Post a Comment