PDA

View Full Version : Need Help with Basic Stamp 2



viper550
05-11-2006, 09:20 PM
Hi everyone.Im new to this forum and i need some help with a project i have.I have a robot that is equiped with basic stamp 2 and i have some problems programming it.I will try to explain what i want it to do with as much details as i can so i can help you understand my problem.

I want the robot to have 2 modes.The first mode will be manual control where i can control the robot with the keyboard.The second mode is automatic movement where the robot will be a ble to move by itself.I want to be able to switch between the 2 modes whenever i want.The code i give was written by me for this purpose but i have some problems.The program starts and it does nothing because it await me to press m for manual control or a for automatic movement.When i press m then i control the robot with the 2,4,6,8 buttons.Here comes my first problem.When i press a to switch to automatic control it doesn't do anything.I have to press it the second time in order to start.When it enters automatic mode it does what it should do but here is my second big problem.When i press m to go to manual mode again it doesn't do anything.No matter how many times i press the m button it still stays in automatic mode.I have tried everything i could think of.If anyone can help me i would be very thankful.

Here i give the code i use:

'{$STAMP BS2}
'{$PBASIC 2.5}

SERVO_SX CON 12
SERVO_DX CON 13
buffer1 VAR Byte

checkAM:
SERIN 16,84,10000,checkAM,[buffer1]
IF (buffer1=97) THEN Auto
IF (buffer1=109) THEN manual
GOTO checkAM

manual:
SERIN 16,84,10000,manual,[buffer1]
IF (buffer1=56) THEN Forward
IF (buffer1=50) THEN Backward
IF (buffer1=54) THEN Right
IF (buffer1=52) THEN Left
IF (buffer1=53) THEN stopm

GOTO checkAM

Auto:
BaffoSX VAR Bit
BaffoDX VAR Bit
BaffoSX=IN6
BaffoDX=IN4
IF (BaffoSX=1 AND BaffoDX=1) THEN Forward1
IF (BaffoSX=1 AND BaffoDX=0) THEN Right_contact
IF (BaffoSX=0 AND BaffoDX=1) THEN Left_contact

GOTO checkAM


Right_contact:
sx VAR Byte
sx=1000
PULSOUT SERVO_SX,750
PULSOUT SERVO_DX,750
PAUSE 1500
T1:
PULSOUT SERVO_SX,800
PULSOUT SERVO_DX,700
sx =sx-1
IF sx=0 THEN T2
GOTO T1
T2:
PULSOUT SERVO_SX,700
PULSOUT SERVO_DX,700
sx = sx-1
IF sx=0 THEN Forward1
GOTO T2
GOTO Auto

Left_contact:
dx VAR Byte
dx=1000
PULSOUT SERVO_SX,750
PULSOUT SERVO_DX,750
PAUSE 1500
T3:
PULSOUT SERVO_SX,800
PULSOUT SERVO_DX,700
dx =dx-1
IF dx=0 THEN T4
GOTO T3
T4:
PULSOUT SERVO_SX,800
PULSOUT SERVO_DX,800
dx = dx-1
IF dx=0 THEN Forward1
GOTO T4
GOTO Auto

Forward1:
PULSOUT SERVO_SX,700
PULSOUT SERVO_DX,800
GOTO Auto

Forward:
PULSOUT SERVO_SX,500
PULSOUT SERVO_DX,1000
GOTO manual

Backward:
PULSOUT SERVO_SX,1000
PULSOUT SERVO_DX,500
GOTO manual

Right:
PULSOUT SERVO_SX,1000
PULSOUT SERVO_DX,1000
GOTO manual

Left:
PULSOUT SERVO_SX,500
PULSOUT SERVO_DX,500
GOTO manual

stopm:
PULSOUT SERVO_SX,750
PULSOUT SERVO_DX,750
GOTO manual

PJ Allen
05-11-2006, 10:11 PM
· I think the trouble is in your "manual" subroutine, because it checks the buffers and·then you GOTO CheckAM.·


manual:
SERIN 16,84,10000,manual,[buffer1]
IF (buffer1=56) THEN Forward
IF (buffer1=50) THEN Backward
IF (buffer1=54) THEN Right
IF (buffer1=52) THEN Left
IF (buffer1=53) THEN stopm

GOTO checkAM



· I think that you should have it check the buffer for an M whereupon it should exit to CheckAM otherwise it should re-cycle to the top of "manual:".


manual:
SERIN 16,84,10000,manual,[buffer1]
IF (buffer1=56) THEN Forward
IF (buffer1=50) THEN Backward
IF (buffer1=54) THEN Right
IF (buffer1=52) THEN Left
IF (buffer1=53) THEN stopm
IF (buffer1 = 109) THEN checkAM ' look for 'a' for auto
GOTO manual


Post Edited (PJ Allen) : 5/11/2006 8:14:39 PM GMT

viper550
05-12-2006, 03:46 AM
I cant understand why i have to press 2 times the "a" button to enter the auto movement.

If i do what you told me i will have to press the "m" button first and then press "a" to enter the auto mode.Can i do something so i dont have to press "m" first and "a" last?

But to tell you the truth this is a minor problem.My biggest problem is that when it enters auto mode i cant get to to return to manual control no matter what.Any ideas?

PJ Allen
05-12-2006, 04:12 AM
· I guess from inside of "manual:" that it should look for an 'a' input/key, not an 'm' [sorry?].
· In 'automatic', you need to test for a 'm' input/key, sometime.··
· Right?


Auto:
BaffoSX VAR Bit
BaffoDX VAR Bit
BaffoSX=IN6
BaffoDX=IN4
IF (BaffoSX=1 AND BaffoDX=1) THEN Forward1
IF (BaffoSX=1 AND BaffoDX=0) THEN Right_contact
IF (BaffoSX=0 AND BaffoDX=1) THEN Left_contact
GOTO checkAM


· I see the GOTO checkAM, but if it ever has a condition for Forward1, Right_contact or Left_conact -- I don't see that it ever goes back from those subroutines to checkAM.· It only gets to checkAM, so far as I can see, if none of those conditions are met.··Am I missing something?

*** Post Edit ***

Bottom line: from inside of "Auto" you need to arrange to test for 'm' and from inside of "Manual" you need to test for 'a'.

Post Edited (PJ Allen) : 5/11/2006 8:30:32 PM GMT

viper550
05-12-2006, 05:19 AM
First of all thanks for all the help you are offering.As for your observations you are correct on both of them.

For my first problem all i had to do is add· "IF (buffer1=97) THEN Auto" into manual.That solved my first problem.Now it enters auto mode with the first press of "a" button.

For the second problem now.You made it very clear why it doesn't work.The program always find one of these conditions so it never goes back to checkAM.I have to enter a command to check when the buffer will be 109 which means i have pressed the m button.But in order to do that dont i have to put the SERIN command in order to get the ascii code?If i do that the program will be interupted and it will stop.One idea i had was to make all three subroutines that are being executed by the conditions to·goto checkAM.But it seems that when it goes to checkAM it waits for me to press a button again.Shouldn't it hold 97 in buffer1 so it wouldnt stop?

PJ Allen
05-12-2006, 08:25 AM
· Since you're SERVOing, you'll have to wedge the looking for an 'm' (within AUTOmatic mode) between the servo refresh intervals, I guess.
··This may be possible (sooner with 19200 than at 9600.)· I'm figuring to get it to bail, you might have to jog the 'm' key, but then again, if your keyboard is set for auto-repeat you might just be able to hold it down.
· To help figure it out a bit, you could turn an LED on/off during the time it's given to accepting an 'm'.· Does that make sense?
·

viper550
05-12-2006, 04:51 PM
Ummm...sorry but i didnt get it.Could you please explain it a little better.

PJ Allen
05-12-2006, 07:42 PM
· You're going to have to implement a break in the Automatic action to go and see if there is·a serial-comm 'm' sent, since STAMPs don't multi-task.
· I guess AUTo is just F-R-L, F-R-L,..., so maybe at the top of AUTO you could put in a SERIN for just the 'm' (but don't leave it hang·for 10000 as your other routines.)· No 'm', no comm, go on with AUTO action.
· [ I was figuring that you're using your PC, maybe hyperterm or something, as the keyboard/serial-signal source.· Yes/No? ]

viper550
05-12-2006, 08:50 PM
Yes Auto is F-R-L,...

If i understand correctly what you are saying is to do something like this?

Auto:
SERIN 16,84,100,Auto,[buffer1]
IF (buffer1=109) THEN manual
BaffoSX VAR Bit
BaffoDX VAR Bit
BaffoSX=IN6
BaffoDX=IN4
IF (BaffoSX=1 AND BaffoDX=1) THEN Forward1
IF (BaffoSX=1 AND BaffoDX=0) THEN Right_contact
IF (BaffoSX=0 AND BaffoDX=1) THEN Left_contact
GOTO checkAM


I am using my PC Basic stamp editor

viper550
05-12-2006, 09:13 PM
ok i solved the problem.All i had to do is this

Auto:
SERIN 16,84,30,Auto1,[buffer1]
IF (buffer1=109) THEN manual
·
Auto1:
BaffoSX VAR Bit
BaffoDX VAR Bit
BaffoSX=IN6
BaffoDX=IN4
·
IF (BaffoSX=1 AND BaffoDX=1) THEN Forward1
IF (BaffoSX=1 AND BaffoDX=0) THEN Right_contact
IF (BaffoSX=0 AND BaffoDX=1) THEN Left_contact
·
GOTO checkAM



Now it works as it should be.Thanks for all the help mate.I wouldn't have solved it without your help.http://forums.parallax.com/images/smilies/hop.gif

PJ Allen
05-12-2006, 09:53 PM
Good on ya'!· Keep on keeping on.