THis a new code I am trying for B2 and pwmpal,
h3n7y
Posts: 11
ok I been trying to get a pwmpal and bs2 to work together and nothing is working I am trying to control an L293d h bridge with 2 dc control with pwmpal and bs2 to make the car steer left and right and i haven't been successful. The documentation for they have on parallax is not good is just does speed and why is not good documentation on pwmpal, this product has been around for 6 years already.
I really found more easy coding in C++ than Pbasic, but this bs2 can't be programmed on C++, yes i have sensors but i will scrap it cuz is getting more diff with pwmpal
Can someone please help me with this codes or send me an email with the codes that works? Please
' {$STAMP BS2}
'( $Pbasic 2.5)
'======Constant===============
ncoder_Variables:
L_cnt VAR Word 'left encoder counter
R_cnt VAR Word 'right encoder counter
X_cnt VAR Word 'left encoder state
y_cnt VAR Word 'left encoder state
L_cnt_dir VAR Word 'left counter direction
R_cnt_dir VAR Word 'right counter direction
L_velocity_cnt VAR Word
R_velocity_cnt VAR Word
prev_Ldir VAR Byte
prev_Rdir VAR Byte
lchk_cntl VAR Byte
rchk_cntl VAR Byte
Chg_dir VAR Byte
sensors VAR Ni
M1ctrl VAR STATUS.BIT0
Main:
INPUT 4 ' left WW-01 ChA
INPUT 5 ' right WW-01 ChA
INPUT 6 ' left WW-01 DIR
INPUT 7 ' right WW-01 DIR
Chg_dir = -1
L_cnt_dir = 1
R_cnt_dir = 1
'‘====================================
Pppin CON 0
pbaud CON 17405
header CON 106
pulse CON 90
GOSUB setup 'these steps initialize the Motors
GOSUB setpulse '
GOSUB setheader '
GOSUB setactive '
DO
GOSUB Chk_sensors
GOSUB Motor_cntl
LOOP
'--- these sensors may be any object detection type
'--- (IR or bumper)
Chk_sensors:
sensors.BIT0 = IN1
sensors.BIT1 = IN2
RETURN
Motor_cntl:
GOSUB Chk_sensors: backward: lefturn: righturn: forward
RETURN
'--These routines check the encoders (left and right)
'--and count up or down depending on if the direction
'--changed.
'---this program is set to check for a falling
'---edge on the encoder ChA line. (in4/5)
'---the velocity_cnt variable may be used to measure
'---the width of the clock pulse but the way it is
'---used here will not be very acurate...
Chk_encoders:
'-- 4 & 5 are connected to the ChA pins of
'-- each encoder.
x_cnt = IN4
y_cnt = IN5
'-- check left encoder
IF x_cnt = 0 THEN main
DEBUG "l_cnt = l_cnt + L_cnt_dir"
END
'-- check right encoder
IF y_cnt = 0 THEN main
DEBUG "r_cnt = r_cnt + R_cnt_dir"
END
GOSUB chk_Ldir
GOSUB chk_Rdir
RETURN
'---begin section = servo setup/contol
' Subroutines used to set up the Motors Controller Chip
'-- substitute any code needed here
setactive:
SEROUT Pppin, pbaud, [noparse][[/noparse]"PWM",(48 + M1CTRL 117]
SEROUT cntl_baud,[noparse][[/noparse]117]'sets pulse
RETURN
setpulse:
SEROUT cntl,baud,[noparse][[/noparse]pulse] 'sets pulse length
RETURN
setup:
SEROUT cntl,baud,[noparse][[/noparse]122] 'sets setup mode
RETURN
setheader:
SEROUT cntl,baud,[noparse][[/noparse]header] 'sets header length
RETURN
'--motion subroutines
lefturn:
SEROUT cntl_baud,[noparse][[/noparse]1,131] 'starts L (no. 1) Motor fwd slowly
SEROUT cntl_baud,[noparse][[/noparse]31,159] 'starts R (no. 2)Motor fwd quickly
PAUSE 2000
RETURN
righturn:
SEROUT cntl_baud,[noparse][[/noparse]0,128] 'starts L (no. 1) Motor fwd quickly
SEROUT cntl_baud,[noparse][[/noparse]16,140] 'starts R (no. 2) Motor fwd slowly
PAUSE 2000
RETURN
backward:
RETURN
forward:
SEROUT cntl_baud,[noparse][[/noparse]0,128] 'L (no. 1) Motor fwd full
SEROUT cntl_baud,[noparse][[/noparse]31,159] 'R (no. 2) Motor fwd full
RETURN
'---====end section = Motor setup/control
I really found more easy coding in C++ than Pbasic, but this bs2 can't be programmed on C++, yes i have sensors but i will scrap it cuz is getting more diff with pwmpal
Can someone please help me with this codes or send me an email with the codes that works? Please
' {$STAMP BS2}
'( $Pbasic 2.5)
'======Constant===============
ncoder_Variables:
L_cnt VAR Word 'left encoder counter
R_cnt VAR Word 'right encoder counter
X_cnt VAR Word 'left encoder state
y_cnt VAR Word 'left encoder state
L_cnt_dir VAR Word 'left counter direction
R_cnt_dir VAR Word 'right counter direction
L_velocity_cnt VAR Word
R_velocity_cnt VAR Word
prev_Ldir VAR Byte
prev_Rdir VAR Byte
lchk_cntl VAR Byte
rchk_cntl VAR Byte
Chg_dir VAR Byte
sensors VAR Ni
M1ctrl VAR STATUS.BIT0
Main:
INPUT 4 ' left WW-01 ChA
INPUT 5 ' right WW-01 ChA
INPUT 6 ' left WW-01 DIR
INPUT 7 ' right WW-01 DIR
Chg_dir = -1
L_cnt_dir = 1
R_cnt_dir = 1
'‘====================================
Pppin CON 0
pbaud CON 17405
header CON 106
pulse CON 90
GOSUB setup 'these steps initialize the Motors
GOSUB setpulse '
GOSUB setheader '
GOSUB setactive '
DO
GOSUB Chk_sensors
GOSUB Motor_cntl
LOOP
'--- these sensors may be any object detection type
'--- (IR or bumper)
Chk_sensors:
sensors.BIT0 = IN1
sensors.BIT1 = IN2
RETURN
Motor_cntl:
GOSUB Chk_sensors: backward: lefturn: righturn: forward
RETURN
'--These routines check the encoders (left and right)
'--and count up or down depending on if the direction
'--changed.
'---this program is set to check for a falling
'---edge on the encoder ChA line. (in4/5)
'---the velocity_cnt variable may be used to measure
'---the width of the clock pulse but the way it is
'---used here will not be very acurate...
Chk_encoders:
'-- 4 & 5 are connected to the ChA pins of
'-- each encoder.
x_cnt = IN4
y_cnt = IN5
'-- check left encoder
IF x_cnt = 0 THEN main
DEBUG "l_cnt = l_cnt + L_cnt_dir"
END
'-- check right encoder
IF y_cnt = 0 THEN main
DEBUG "r_cnt = r_cnt + R_cnt_dir"
END
GOSUB chk_Ldir
GOSUB chk_Rdir
RETURN
'---begin section = servo setup/contol
' Subroutines used to set up the Motors Controller Chip
'-- substitute any code needed here
setactive:
SEROUT Pppin, pbaud, [noparse][[/noparse]"PWM",(48 + M1CTRL 117]
SEROUT cntl_baud,[noparse][[/noparse]117]'sets pulse
RETURN
setpulse:
SEROUT cntl,baud,[noparse][[/noparse]pulse] 'sets pulse length
RETURN
setup:
SEROUT cntl,baud,[noparse][[/noparse]122] 'sets setup mode
RETURN
setheader:
SEROUT cntl,baud,[noparse][[/noparse]header] 'sets header length
RETURN
'--motion subroutines
lefturn:
SEROUT cntl_baud,[noparse][[/noparse]1,131] 'starts L (no. 1) Motor fwd slowly
SEROUT cntl_baud,[noparse][[/noparse]31,159] 'starts R (no. 2)Motor fwd quickly
PAUSE 2000
RETURN
righturn:
SEROUT cntl_baud,[noparse][[/noparse]0,128] 'starts L (no. 1) Motor fwd quickly
SEROUT cntl_baud,[noparse][[/noparse]16,140] 'starts R (no. 2) Motor fwd slowly
PAUSE 2000
RETURN
backward:
RETURN
forward:
SEROUT cntl_baud,[noparse][[/noparse]0,128] 'L (no. 1) Motor fwd full
SEROUT cntl_baud,[noparse][[/noparse]31,159] 'R (no. 2) Motor fwd full
RETURN
'---====end section = Motor setup/control
Comments
your code cannot work because it does not tokenize (compile).
When you·get it to do that include DEBUG commands where appropriate to·help you to localize your error(s).
Cheers Vaclav
There are a few syntax errors in the program; for example Ni I'm guessing should be Nib. Can you successfully compile the program and then attach the program via the attachment manager? That way we can look at the program within the Editor without having to cut and paste.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Respectfully,
Joshua Donelson
www.parallax.com
{$STAMP BS2}
'
[noparse][[/noparse] I/O Definitions ]
RightMotor CON 3 ' Right Servo
LeftMotor CON 1 ' Left Servo
'Ping CON 15 ' PING))) Sensor
'
[noparse][[/noparse] Variables ]
pulseCount VAR Byte ' Used For Measuring Turns
' Conversion constants for room temperature measurements.
Pppin CON 0
CmConstant CON 2260
InConstant CON 890
cmDistance VAR Word
inDistance VAR Word
time VAR Word
task VAR Nib ' Current Task
main
DO
PULSOUT 11, 5
PULSIN 11, 1, time
inDistance = inConstant ** time
'DEBUG HOME, "The distance is ", DEC2 inDistance, " in"
IF (inDistance < 18) THEN main
GOSUB Turn_Right
GOSUB Turn_Right
PAUSE 200
ElSE
GOSUB Go_Forward
PAUSE 200
GOSUB Turn_Left
PAUSE 200
GOSUB Go_Forward
PAUSE 200
GOSUB Turn_Left
PAUSE 200
GOSUB Go_Forward
PAUSE 200
GOSUB Turn_Right
PAUSE 200
GOSUB Go_Forward
GOSUB Go_Forward
ENDIF
'PAUSE 1
LOOP
Turn_Left: ' Left Turn, About 45 Degrees
FOR pulseCount = 0 TO 40 ' Number Of Pulses To Turn
PULSOUT LeftMotor, 650 ' Left motor Left Pulse Value
PULSOUT RightMOtor, 650 ' Right motor Left Pulse Value
PAUSE 20 ' Refresh Delay
NEXT
RETURN
Turn_Right: ' Right Turn, About 45 Degrees
FOR pulseCount = 0 TO 40 ' Number Of Pulses To Turn
PULSOUT LeftMotor, 850 ' Left motor Right Pulse Value
PULSOUT RightMotor, 850 ' Right motor Right Pulse Value
PAUSE 20
NEXT ' Refresh Delay
RETURN
Go_Forward: ' Right Turn, About 45 Degrees
FOR pulseCount = 0 TO 200 ' Number Of Pulses To Turn
PULSOUT LeftMotor, 850 ' Left motor Right Pulse Value
PULSOUT RightMotor, 650 ' Right motor Right Pulse Value
PAUSE 20 ' Refresh Delay
NEXT
RETURN
Go_Backward: ' Right Turn, About 45 Degrees
FOR pulseCount = 0 TO 100 ' Number Of Pulses To Turn
PULSOUT LeftMotor, 650 ' Left Servo Right Pulse Value
PULSOUT RightMotor, 850 ' Right Servo Right Pulse Value
PAUSE 20 ' Refresh Delay
NEXT
RETURN
you are on the right track, however, you are still too far ahead of yourself in troubleshooting this code. Hardware is important, but you need to make you· code "flow" before you get to the hardware.
For example, your code gets stuck in "inDistance = 0 " right from the start.
Use DEBUG and you will make progress.
Cheers Vaclav
PS: Compiler may not care, but the label syntax is "LABEL:" - label text ·then colon. ·As future engineers you need to pay atention to details.
·
·
main:
DEBUG " Main",CR
'STOP
DO
· PULSOUT 11, 5
· PULSIN 11, 1, time
· DEBUG "Test",CR
· 'STOP
· inDistance = inConstant ** time
· DEBUG ? inDistance,CR
· DEBUG ? inConstant,CR
· DEBUG ? time,CR
· STOP
· DEBUG "The distance is ", DEC2 inDistance, " inches",CR
· 'STOP
· IF (inDistance < 18) THEN main
··· GOSUB Turn_Right
··· GOSUB Turn_Right
··· PAUSE 200
· ElSE
I still see a few Syntax Errors; as Valclav_sal pointed, and missing parts in the PING))) code that will overall effect the operation of the PING))) sensor. Is there a reason you are not scaling, or removing the return trip? By doing that the·distance is reading double of what it really is.
The Syntax that Vaclav_sal was explaining can also be referenced with the BASIC Stamp Syntax & Refrence Manual.
BASIC Stamp Syntax & Reference Manual:
http://www.parallax.com/Portals/0/Downloads/docs/prod/stamps/web-BSM-v2.2.pdf
Smart Sensors & Applications:
http://www.parallax.com/Portals/0/Downloads/docs/prod/sic/3rdPrintSmartSensors-v1.0.pdf
I hope these items help,
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Respectfully,
Joshua Donelson
www.parallax.com
If it was the editor would show it as an undefined variable while either downloading or testing it.
Of course for ease of reading etc it should be done correctly.