Programmera i Pbasic (för Basic Stamp 2)

PIC, AVR, Arduino, Raspberry Pi, Basic Stamp, PLC mm.
elev
Inlägg: 12
Blev medlem: 23 augusti 2004, 11:19:32

Programmera i Pbasic (för Basic Stamp 2)

Inlägg av elev »

Är du bra på programmering?

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.

Kod: Markera allt

 
' {$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 
Senast redigerad av elev 15 mars 2005, 12:41:42, redigerad totalt 1 gång.
Användarvisningsbild
Hedis
Inlägg: 2493
Blev medlem: 8 december 2003, 15:10:44
Ort: Vänersborg
Kontakt:

Inlägg av Hedis »

Ett tips är att använda code taggarna. Börja med [code]och avsluta med [/code] så blir det lättare att läsa.

Men HALLÅ! räcker dte inte med ett inlägg?
In o ta bort det andra nu innan någons skriver i det.
Senast redigerad av Hedis 14 mars 2005, 21:57:47, redigerad totalt 1 gång.
Användarvisningsbild
strombom
Inlägg: 3305
Blev medlem: 27 maj 2003, 10:50:20
Ort: Västra Götaland
Kontakt:

Inlägg av strombom »

Börja med att trassla ur och snygga till koden så att det blir läsbart

/Johan
elev
Inlägg: 12
Blev medlem: 23 augusti 2004, 11:19:32

Inlägg av elev »

bättre nu?
Användarvisningsbild
Icecap
Inlägg: 26647
Blev medlem: 10 januari 2005, 14:52:15
Ort: Starup (Haderslev), Danmark

Inlägg av Icecap »

Näpp!

Av någon outgrundelig anledning vill du ha en massa tomma rader i koden, jag antar att det är för att det ska fylla hela skärmen och vara omöjligt att läsa.

När detta är så struntar iaf. jag i att svara.
Användarvisningsbild
Jine
Inlägg: 1795
Blev medlem: 21 juli 2004, 20:25:56
Skype: Jim.Nelin
Ort: Trångsund, Stockholm
Kontakt:

Inlägg av Jine »

Kod: Markera allt

' {$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 

VARSÅGOD :P

Kan säga till och börja med att "debug" delen saknar massor av kod lr hur?
Användarvisningsbild
Icecap
Inlägg: 26647
Blev medlem: 10 januari 2005, 14:52:15
Ort: Starup (Haderslev), Danmark

Inlägg av Icecap »

Du kunne ju faktisk ha redigerat det första inlägg eller hur?

Kod: Markera allt

noTable:
      DEBUG "No table", CR 'if no table
      PAUSE 500
      flag=0
      GOSUB RobotSend
      ' Här har den stoppat och det är dax att lägga i backen och svänga. Det kan göras som en sekvens skulle jag tro.

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 '
Edit: Sen har BASIC inget med att vara bra på att programmera att göra!
danei
EF Sponsor
Inlägg: 27423
Blev medlem: 2 juni 2003, 14:21:34
Ort: Östergötland
Kontakt:

Inlägg av danei »

Det är lite svårt får någon anna annan arr redigera första inlägget. Eller vad menar du?
Skriv svar