Hejsan!
Jag håller på med ett litet projekt som går ut på att programmera en robot i PBASIC. Det är tänkt att
den ska köra rakt fram ända tills den kommer fram till kanten på ett bord. Då ska den backa och
svänga (annars faller den ju ner från bordet). Hittills har jag lyckats få den att stanna när den kommer
ut till bordskanten, men det här programmet som jag klistrar in vill inte låta roboten göra ngt mer än
att just stanna när den kört fram till bordskanten (och blinka medan den kör, för att sluta när den
stannar).
Kommandot för att backa är robotdata=robotreverse, och för att svänga åt vänster skriver man
robotdata=robotleft, men jag har svårt för att komma på VAR jag ska placera de här instruktionerna.
Finns det ngn vänlig själ där ute som skulle kunna tänka sig att ge mig en ledtråd? Tacksam för svar.
' {$STAMP BS2}
' Mainline
Flag VAR Bit
HIGH SC ' Set the I/O Bits As O/P
HIGH SD ' and High
PAUSE 100 ' Stop the Robot from Running
RobotData = RobotStop
PAUSE 100
robotdata=RobotForward ' Move Forward for 200 msecs
GOSUB RobotSend
noTable:
DEBUG "No table", CR 'if no table
PAUSE 500
flag=0
GOSUB RobotSend
Loop: ' Repeat Here Forever
PAUSE 500 ' Poll Once Every 1/2 Second
RobotData = RobotWhiskers ' Read the Robot Whiskers
GOSUB RobotSendReceive
IF (RobotData = 0) THEN noTable 'whiskers detect nothing (there's nothing underneath the robot)
'else
flag=1
'otherwise - when the robot is on the table
DEBUG "table", CR
'ENDIF
' flash LED
RobotData = RobotLEDOn
GOSUB RobotSend
PAUSE 300
RobotData = RobotLEDOff
GOSUB RobotSend '
robotdata=RobotForward ' Move Forward for 200 msecs
GOSUB RobotSend
GOTO Loop
' Robot Interface Code Follows:
'
' Myke Predko
'
' Copyright (C) 2001 McGraw-Hill
'
' Robot Commands
RobotStop CON 0 ' Stop the Robot
Behavior1 CON 1 ' Random Movement
Behavior2 CON 2 ' Photovore
Behavior3 CON 3 ' Photophobe
Behavior4 CON 4 ' Wall Hugger/Maze Solver
RobotForward CON 5 ' Move Forward for 200 msecs
RobotReverse CON 6 ' Move Reverse for 200 msecs
RobotLeft CON 7 ' Turn Left for 200 msecs
RobotRight CON 8 ' Turn Right for 200 msecs
RobotLEDOn CON 9 ' Turn on the Robot's LED
RobotLEDOff CON 10 ' Turn off the Robot's LED
RobotPWM0 CON 11 ' PWM = 0% Duty Cycle
RobotPWM1 CON 12 ' PWM = 1st "Notch"
RobotPWM2 CON 13 ' PWM = 2nd "Notch"
RobotPWM3 CON 14 ' PWM = 3rd "Notch"
RobotPWM4 CON 15 ' PWM = 100% Duty Cycle
RobotPWM CON 16 ' Return the Current PWM Value
RobotState CON 17 ' Return the Executing State
RobotWhiskers CON 18 ' Return State of the "Whiskers"
' Bit 0 - Left "Whisker"
' Bit 1 - Right "Whisker"
RobotCDSL CON 19 ' Return Value of Left CDS Cell
RobotCDSR CON 20 ' Return Value of Right CDS Cell
RobotButton CON 21 ' Return the Last Remote Button Press
' 0 - No Buttons Pressed
' 1 - Leftmost Button Pressed
' 2 - Middle Button Pressed
' 3 - Rightmost Button Pressed
' After "RobotButton" Operation,
' Button Save is Cleared
' Robot Interface Pins
SC CON 14 ' Define the I/O Pins
SD CON 15
' Robot Interface Variables
RobotData VAR Byte ' Data Byte to Send to/Receive
' from Robot
' Robot Operation Subroutines
RobotSend ' Send the Byte in "RobotData"
LOW SC ' Hold Low for 1 msec before
PAUSE 1 ' Shifting in Data
SHIFTOUT SD, SC, LSBFIRST, [RobotData]
HIGH SC
RETURN
RobotSendReceive ' Send the Byte in "RobotData"
LOW SC ' Hold Low for 1 msec before
PAUSE 1 ' Shifting in Data
SHIFTOUT SD, SC, LSBFIRST, [RobotData]
PAUSE 1 ' Wait for Operation to Complete
SHIFTIN SD, SC, LSBPOST, [RobotData]
HIGH SC
RETURN