'basic competition program' for sumobot modified for use with step genie/stepper moto
circuitbob
Posts: 5
Hello, I teach a high school level robotics class. We use the sumobot and the basic competition program for a lot of robots. Now I have some students that want to use stepper motors instead of servos, controlling them with the step genie or UCN- 5804 (which require three inputs: enable, direction and step pulse) has anyone out there modified the basic competition program to use a motor control like this instead of servos, or have any advice on how to do so? im not an expert stamp programmer yet and am a little foggy on where exactly to start.
Comments
[ Title]
' Mini Sumo 5.1 : Basic Competition Program
' {$STAMP BS2}
'
[ I/O Definitions]
LMotor CON 13 ' left servo motor
RMotor CON 12 ' right servo motor
LLineSnsPwr CON 10 ' left line sensor power
LLineSnsIn CON 9 ' left line sensor input
RLineSnsPwr CON 7 ' right line sensor power
RLineSnsIn CON 8 ' right line sensor input
LfIrOut CON 4 ' left IR LED output
LfIrIn VAR IN11 ' left IR sensor input
RtIrOut CON 15 ' right IR LED output
RtIrIn VAR IN14 ' right IR sensor input
Speaker CON 1 ' piezo speaker
StartLED CON 0 ' display start delay
'
[ Constants]
LFwdFast CON 1000 ' left motor forward; fast
LFwdSlow CON 800 ' left motor forward; slow
LStop CON 750 ' left motor stop
LRevSlow CON 700 ' left motor reverse; slow
LRevFast CON 500 ' left motor reverse; fast
RFwdFast CON 500 ' right motor forward; fast
RFwdSlow CON 700 ' right motor forward; slow
RStop CON 750 ' right motor stop
RRevSlow CON 800 ' right motor reverse; slow
RRevFast CON 1000 ' right motor reverse; fast
'
[ Variables]
leftSense VAR Word ' left sensor raw reading
rightSense VAR Word ' right sensor raw reading
blackThresh VAR Word ' QTI black thresholdsetting
lineBits VAR Nib ' decoded sensors value
lineLeft VAR lineBits.BIT1
lineRight VAR lineBits.BIT0
irBits VAR Nib ' storage for IR targetdata
irLeft VAR irBits.BIT1
irRight VAR irBits.BIT0
lastIr VAR Nib ' info from last reading
pulses VAR Byte ' counter for motor control
temp VAR Byte
'
[ EEPROM Data]
RunStatus DATA $00 ' runstatus
'
[ Initialization]
Run_Check: ' userReset button as On-Off
READ RunStatus,temp ' read current status
temp = ~temp ' invertstatus
WRITE RunStatus,temp ' savestatus for next reset
IF (temp = 0) THENSet_Threshold ' run now?
END ' -- no ... next time
' Sets black threshold to 1/4 the average of the two sensorreadings.
' SumoBot must be placed over black playing surface beforethis code runs.
Set_Threshold: ' set QTIblack threshold
GOSUBRead_Line_Sensors
blackThresh =(leftSense / 8) + (rightSense / 8)
Start_Delay: ' mandatoryfive second delay
FOR temp = 1 TO 5
HIGH StartLED ' show active
PAUSE 900
INPUTStartLED 'blink each second
FREQOUT Speaker,100, 2500, 3000 ' beep eachsecond
NEXT
GOTO Lunge ' startaggressive!
'
[ Main Code ]
Main:
GOSUBRead_Line_Sensors
' If not on theShikiri line (border), continue to look for opponent,
' otherwise, spinback toward center and resume search
BRANCH lineBits,[Search_For_Opponent, Spin_Left, Spin_Right, About_Face]
' --[ Border Avoidance ]--
Spin_Left: ' rightsensor was active
FOR pulses = 1 TO 20
PULSOUT LMotor,LRevFast
PULSOUT RMotor,RFwdFast
PAUSE 20
NEXT
lastIr = %00 ' clear scan direction
GOTO Lunge
Spin_Right: ' leftsensor was active
FOR pulses = 1 TO 20
PULSOUT LMotor,LFwdFast
PULSOUT RMotor,RRevFast
PAUSE 20
NEXT
lastIr = %00
GOTO Lunge
About_Face: ' both sensors onShikiri line
FOR pulses = 1 TO10 ' back upfrom edge
PULSOUT LMotor,LRevFast
PULSOUT RMotor,RRevFast
PAUSE 20
NEXT
FOR pulses = 1 TO30 ' turn around
PULSOUT LMotor,LFwdFast
PULSOUT RMotor,RRevFast
PAUSE 20
NEXT
lastIr = %00
GOTO Lunge
' --[ IR Processing ]--
Search_For_Opponent:
GOSUBRead_IR_Sensors
' If opponent is notin view, scan last known direction. Turntoward
' opponent if seenby one "eye" -- if both, lunge forward
BRANCH irBits,[Scan, Follow_Right, Follow_Left, Lunge]
Scan:
BRANCH lastIR,[Move_Fwd, Scan_Right, Scan_Left]
Move_Fwd:
GOSUB Creep_Forward
GOTO Main
Scan_Right: ' spinright, slow
FOR pulses = 1 TO 5
PULSOUT LMotor,LFwdSlow
PULSOUT RMotor,RRevSlow
PAUSE 20
NEXT
GOSUBCreep_Forward 'keep moving
GOTO Main
Scan_Left: ' spinleft, slow
FOR pulses = 1 TO 5
PULSOUT LMotor,LRevSlow
PULSOUT RMotor,RFwdSlow
PAUSE 20
NEXT
GOSUB Creep_Forward
GOTO Main
Follow_Right: ' spin right, fast
PULSOUT LMotor,LFwdFast
PULSOUT RMotor,RRevSlow
lastIR = irBits ' save lastdirection found
GOTO Main
Follow_Left: ' spinleft, fast
PULSOUT LMotor, LRevSlow
PULSOUT RMotor,RFwdFast
lastIR = irBits
GOTO Main
Lunge: 'locked on -- go get him!
FOR pulses = 1 TO 15
PULSOUT LMotor,LFwdFast
PULSOUT RMotor,RFwdFast
GOSUBRead_Line_Sensors
IF (lineBits =%11) THEN Match_Over ' in sightand we're on the line
NEXT
GOTO Main
' If SumoBot can see the opponent with both "eyes"and both QTIs are
' detecting the border, we must have pushed the opponentout.
Match_Over:
FOR pulses = 1 TO10 ' stop motors
PULSOUT LMotor,LStop
PULSOUT RMotor,RStop
PAUSE 20
NEXT
INPUT LMotor
INPUT RMotor
FOR temp = 1 TO10 ' make somenoise
HIGH StartLED
FREQOUT Speaker,100, 2500, 3000 ' beep
INPUTStartLED 'blink LED
PAUSE 100
NEXT
DIRS = $0000 ' disable alloutputs
GOTO Run_Check ' reset fornext round
'
[ Subroutines]
Read_Line_Sensors:
HIGHLLineSnsPwr ' activate sensors
HIGH RLineSnsPwr
HIGH LLineSnsIn ' discharge QTIcaps
HIGH RLineSnsIn
PAUSE 1
RCTIME LLineSnsIn,1, leftSense ' read leftsensor
RCTIME RLineSnsIn,1, rightSense ' read rightsensor
LOW LLineSnsPwr ' deactivatesensors
LOW RLineSnsPwr
' convert readingsto bits
lineBits = %00
LOOKDOWN leftSense,>=[blackThresh, 0], lineLeft
LOOKDOWN rightSense,>=[blackThresh, 0], lineRight
RETURN
Read_IR_Sensors:
FREQOUT LfIrOut, 1,38500 ' modulate leftIR LED
irLeft = ~LfIrIn ' read input (1 =target)
FREQOUT RtIrOut, 1,38500 ' modulateright IR LED
irRight =~RtIrIn 'read input (1 = target)
RETURN
Creep_Forward:
FOR pulses = 1 TO 10
PULSOUT LMotor,LFwdSlow
PULSOUT RMotor,RFwdSlow
PAUSE 20
NEXT
RETURN
I imagine enable could probably be tied high. Direction is held high when the motor is to turn 'forward"(we'll call it for now) and the direction pin is held low to reverse the motor. Instead of having to output the individual sequence of bits the stepper needs the step pin just needs to pulsed and the driver takes care of the rest.
Now the problem I see with trying to use a stepper with a BS2 is the stepper, even with the added controller, will require a lot of step pulses to get the motor to move. Instead of a pulse once every 20ms, you'll need to send however many pulses you want the stepper motor to take steps. You'll also need to send the pulses at the correct intervals. I think this soon becomes too much for the BS2 (though I'm not positive about this).
In general one doesn't want to use a stepper with a BS2 if you want the BS2 to do anything other than pulse the stepper.
So the short answer is I really doubt you'll be able to use steppers instead of servos in the sumobots.
You can certainly check a few sensors while moving. You might have to slow down the pulse rate to the stepper a bit, but as I'm wont to say, you can do ANYTHING with a BS2.
I don't think Chris Savage ever believed the quad encoder speeds I achieved on the BS2, but Tracy Allen did.
Since the "PAUSE 20" command is likely taking most of the time in a loop, this time could be spent pulsing the stepper. So I maybe the steppers wouldn't be so bad after all. I'm not sure how the bot will behave with interruptions to the steps as sensors are checked and decisions are made.
Steppers are certainly not a drop in replacement for CR servos.
@erco, I'm trying to remember. Was that quad encoder video before or after the video showing how you can unlock your car with a tennis ball?
Actually, my prior video was Bigfoot riding a unicycle while juggling Roswell aliens.
And thus beginneth my quest for 2014!
Let's face it, driving servos and pausing 20 in nested loops is nearly as much of a PITA as driving stepper motors. So Servopal saved the day. Seems like "Stepperpal" should be on the docket as well. IIRC there was Motormind or Microstepper(?) in the past, but there's nothing available now, at least not from Mother Parallax.
Since I like robotics and general motion control so much, maybe this is my call to action! Perfect app for a serial-input PIC daughterboard, maybe with an onboard ULN2803 for two bipolar steppers. Could double as a 8-servo driver. Maybe I'll make my first PCB and get filthy stinking rich!
Nah...
Well maybe...
Heck, a ULN5804 dedicated IC driver IC is just $2.59: http://www.ebay.com/itm/1PCS-UCN5804B-UCN5804-Stepper-Motor-Transl-EQ13-/360718744837
I'm sure there are examples of controlling a stepper with a BS2 but I'm not so sure if there are examples of BS2/stepper controlled robots.
In some ways this could be easier than using a servo but it would only be easier it you didn't need to control the speed of the robot.
The motor could be turned on with a "HIGH" command to the pin or pins controlling the h-bridge/motor. One the motor is turned on the program can continue with other tasks. However if you want to control the speed of the motor you'd need to send a PWM output to the h-bridge. The program can't send a PWM signal and do anything else at the same time. I also think the PWM would be limited to one motor at a time.
Another concern with using PWM and a h-bridge to control a DC motor is the BS2's PWM frequency is very high (IIRC). I'm not sure how well the PWM output from a BS2 would work with many (most?) h-bridge chips/circuits. I think the BS2 really needs some sort of smart driver to use DC motors, hence the use of CR servos.
The BS2 can only do one thing at a time. IMO, this imposes a severe limitation when using it to drive multiple motors.
Pololu sells a variety of smart motor controllers. These take care of driving the motors and the BS2 would just send it commands to indicate how fast to drive which motor. I haven't used these myself but I believe this is one way of overcoming the limitations of the BS2.
And later I retrofitted those steppers into a BS2-based Boebot with a ULN2803 driver: http://forums.parallax.com/showthread.php/144427-Stepper-BoeBot
Using nothing but a BS2/2803 might require stopping the steppers briefly occasionally to monitor various sensors for a Sumobot.
Some modification and or machining is in order to mount the motors to the chassis, and more to mount the wheels to the motors. You would need better batteries to run stepper motors, for sure. Probably li-po or li-ion. Certainly more time and $$$, is all of this in the budget? If so, I have some interesting thoughts for you.