Sunday, April 8, 2012

[ROBOMAG2012] Motor Control Code Usage

The motor control has been rewritten to make it easier to use across multiple files. The public member functions are as follows:

set_attr( arr_attributes ):
This function allows us to set the pwm center and PWM half range for the Maestro ports.
Parameters: arr_attributes is an array of ordered pairs [c,r], where the order is determined by the ports to which each servo or motor is connected. The array index corresponds to each port. c is the pwm center, r is the half range, which is the difference between the max and pwm center.
self.pwm_attr[n][0] + self.pwm_attr[n][1] = pwm max for port n
self.pwm_attr[n][0] - self.pwm_attr[n][1] = pwm min for port n

turn( amount = 0 ):
The turn function takes a percentage and convert to appropriate turning angle
-100. to < 0. turns left, > 0. to +100. turns right
Any values outside of this range will place the servos back to neutral position

drive( speed = 0 ):
The move command takes a power percentage and converts to appropriate power
-100. to < 0. moves backward, > 0. to +100. moves forward
Any values outside of this range will turn the motors off

honk( on ):
if on = 1, Turns horn on HIGH pwmout = 3000
else turns horn on LOW pwmout = 100

reset( ):
brings all the pwm to default settings. Centers steering servos and turns off motor control

To use these functions, an object must first be constructed. By default, the only parameter required is the COM Port number. On this particular machine, the servo controller is on port 7. The code would look like:

motors = control( 7 )
motors.set_attr( [[1500, 406], [1500, 406], [1496, 504]] ) 50 )
time.sleep( 3 )
motors.honk( 1 )
motors.turn( 68 )
time.sleep( 2 )
motors.honk( 0 )
motors.turn( ) 100 )
time.sleep( 5 ) )

This creates the control object named motors and sets the pwm centers and half ranges for the front servo, the rear servo, and the speed controller on ports 0, 1, 2, respectively. It then starts moving at 50% speed, turns 68% to the right and honks its horn while moving, straightens out while moving, and speeds up to 100% speed for 5 more seconds before slowing down to a stop.

No comments:

Post a Comment