Shop OBEX P1 Docs P2 Docs Learn Events
Basic stamp data comparison logic — Parallax Forums

Basic stamp data comparison logic

w00t_86w00t_86 Posts: 12
edited 2011-01-23 03:49 in General Discussion
Initially i programmed my robot to have 2 DC motors (left and right) to propel it through water. I would have to manually control these 2 motors via a remote control. Turning of the robot would be done by switching off one DC motor and switching on the other one. Example being if i wanted the robot to turn right, i would switch the right DC motor off and switch on the left one.

It worked well in theory, except for the fact that i felt that I could make my task much easier by having another approach. I was thinking that one joystick direction to drive the DC motors in the forward direction and another joystick direction to make the robot turn leftwards or rightwards. The programming part was one part i couldn't fathom before i resigned myself to just driving each DC motors individually and using rotation as switching off one DC motor and switching on the other.

I've wrote my code and i figured that my possible mistake comes from the logic comparison before the turning of motors. However, the BASIC stamp only seemed to be able to make only one directional change (leftwards or rightwards) but not both. Could anyone enlighten me as to what is wrong? It seems pretty straightforward but it simply baffles me! :frown:

My code:
'{$STAMP BS2sx}
'{$PBASIC 2.5}

DIRS  = $f0ff   'Pin I/O. Input = 1, Output = 0.

LOW 4
LOW 5
LOW 6
LOW 7
HIGH 8
HIGH 9

'Motor Driver Input Pins
MotorDriver1 PIN 8
MotorDriver2 PIN 9

'RC Pin
RCForward CON 5                  'Left joystick vertical direction
RCSubmerge CON 6                 'Right joystick vertical direction
RCTurning CON 4                  'Right joystick horizontal direction
RCElectromagnet CON 7            'Left joystick horizontal direction

'RC PULSIN variable [Taking in reading from Remote Control Pins and storing them to a variable]
PWForward VAR Word
PWTurning VAR Word
PWSubmerge VAR Word
PWElectromagnet VAR Word
i VAR Word
j VAR Word

'Remote Control Center Constant Values
JForward CON 1905   'Justify vertical left joystick. Low = 1368, High = 2476.
JSubmerge CON 1920  'Justify vertical right joystick. Low = 1500, High = 2315.
JTurning CON 2022    'Justify horizontal right joystick. Low = 1454, High = 2572
JElectro CON 2042     'Justify horizontal left joystick. Low = 1470, High = 2580

'#Write to EEProm to detect restart
eepromvalue VAR Word
READ 0,Word eepromvalue
WRITE 0,Word eepromvalue +1

'------ Start of Main Program ------'
Main:
PULSIN RCForward,1,PWForward
PULSIN RCSubmerge,1,PWSubmerge
PULSIN RCTurning,1,PWTurning
PULSIN RCElectromagnet,1,PWElectromagnet
PWForward=PWForward & $FFFE
PWSubmerge=PWSubmerge & $FFFE
PWTurning=PWTurning & $FFFE
PWElectromagnet=PWElectromagnet & $FFFE
i=(PWTurning-JTurning)                   'Logic comparison for turning
j=(JTurning-PWTurning)

'DEBUG CR, DEC i
'DEBUG CR, DEC j
'DEBUG CR, DEC PWForward
'DEBUG CR, DEC PWSubmerge
'DEBUG CR, DEC PWTurning
'DEBUG CR, DEC PWElectromagnet

IF (ABS (PWForward-JForward)>400) THEN       'Forward speed control
  SEROUT 8,2, ["2f7",13]          'Left motor
  SEROUT 9,2, ["2f7",13]          'Right motor
ELSE
  SEROUT 8,2, ["2f0",13]
  SEROUT 9,2, ["2f0",13]
ENDIF

IF (ABS (PWSubmerge-JSubmerge)>250) THEN         'Submerge motor forward speed control
  SEROUT 9,2, ["1f9",13]
ELSE
  SEROUT 9,2, ["1f0",13]
ENDIF

IF (i>100) THEN
GOTO RobotTurnRight
ENDIF

IF (j>100) THEN
GOTO RobotTurnLeft
ENDIF

IF (ABS (PWElectromagnet-JElectro)>300) THEN     'Electromagnet is always on unless directed
  LOW 10
ELSE
  HIGH 10
ENDIF


GOTO Main

'Subfunctions
RobotTurnLeft:
DEBUG CR,"Turning Left"
SEROUT 9,2, ["2f5",13]          'Right motor
SEROUT 8,2, ["2f2",13]          'Left motor
GOTO Main

RobotTurnRight:
DEBUG CR,"Turning Right"
SEROUT 8,2, ["2f5",13]          'Left motor
SEROUT 9,2, ["2f2",13]          'Right motor
GOTO Main

P.S. I realized that if i remove the ABS within the IF ELSE conditions the motors also behave erratically. Is the problem linked to the logical comparison problem that i have?

Comments

  • Clive WakehamClive Wakeham Posts: 152
    edited 2011-01-22 14:03
    w00t_86 wrote: »
    Initially i programmed my robot to have 2 DC motors (left and right) to propel it through water. I would have to manually control these 2 motors via a remote control. Turning of the robot would be done by switching off one DC motor and switching on the other one. Example being if i wanted the robot to turn right, i would switch the right DC motor off and switch on the left one.

    It worked well in theory, except for the fact that i felt that I could make my task much easier by having another approach. I was thinking that one joystick direction to drive the DC motors in the forward direction and another joystick direction to make the robot turn leftwards or rightwards. The programming part was one part i couldn't fathom before i resigned myself to just driving each DC motors individually and using rotation as switching off one DC motor and switching on the other.

    I've wrote my code and i figured that my possible mistake comes from the logic comparison before the turning of motors. However, the BASIC stamp only seemed to be able to make only one directional change (leftwards or rightwards) but not both. Could anyone enlighten me as to what is wrong? It seems pretty straightforward but it simply baffles me! :frown:

    P.S. I realized that if i remove the ABS within the IF ELSE conditions the motors also behave erratically. Is the problem linked to the logical comparison problem that i have?

    The Basic Stamp can only do one thing at a time, unless you off-load it to another chip.
    I had a similar issue, I had built a small robot that had a rotating ultrasonic detector which took readings from three directions and compared it to readings it took previously and then determined if it needed to turn into a doorway, turn around if it was at the end of the hall, adjust its heading slighly if it was drifting towards a wall while advancing etc etc.
    Of course the Basic stamp could not keep the rotating head going, getting the results of the ultrasonic sensor, and control the motors at the same time. It looked like it was having a fit or something.
    I actually bought a SERVOPAL to offload the controlling of the motors but I didn't get around to re-progamming it. I decided to upgrade to a Propeller chip instead.

    Question. Are you sure of the values coming from the joysticks?
    I notice that you have a few debug statements in your code to check values, but the use of DEC might not show the correct figures. Change them to SDEC and see what the results are, and compare them to your original results.
  • w00t_86w00t_86 Posts: 12
    edited 2011-01-22 22:03
    The Basic Stamp can only do one thing at a time, unless you off-load it to another chip.
    I had a similar issue, I had built a small robot that had a rotating ultrasonic detector which took readings from three directions and compared it to readings it took previously and then determined if it needed to turn into a doorway, turn around if it was at the end of the hall, adjust its heading slighly if it was drifting towards a wall while advancing etc etc.
    Of course the Basic stamp could not keep the rotating head going, getting the results of the ultrasonic sensor, and control the motors at the same time. It looked like it was having a fit or something.
    I actually bought a SERVOPAL to offload the controlling of the motors but I didn't get around to re-progamming it. I decided to upgrade to a Propeller chip instead.

    Question. Are you sure of the values coming from the joysticks?
    I notice that you have a few debug statements in your code to check values, but the use of DEC might not show the correct figures. Change them to SDEC and see what the results are, and compare them to your original results.

    In that case, would using 2 BASIC stamps at the same time with communication via SEROUT solve the problem of 1 BASIC Stamp having too much to do?
  • Clive WakehamClive Wakeham Posts: 152
    edited 2011-01-23 00:49
    You could.
    You would need to allocate a couple of pins to allow them to handshake first before they tried communicating with get other, then you are still trying to get them to do a few things at once otherwise.
  • bsnutbsnut Posts: 521
    edited 2011-01-23 01:39
    What are you trying to do with the Basic Stamps? One Stamp can handle the autopilot with no problem, which can run the drive motors. This means, you can have the RC receiver control motors manually, which gives better control over your motors while not under the control of autopilot. When it's under the control of autopilot, the servopal can handle the motor control with the stamp sending pulses to the direction and speed.

    This gives you some idea how you can do it. By the way, I will have to read your code later on, because I am post this on my phone.
  • w00t_86w00t_86 Posts: 12
    edited 2011-01-23 03:49
    bsnut wrote: »
    What are you trying to do with the Basic Stamps? One Stamp can handle the autopilot with no problem, which can run the drive motors. This means, you can have the RC receiver control motors manually, which gives better control over your motors while not under the control of autopilot. When it's under the control of autopilot, the servopal can handle the motor control with the stamp sending pulses to the direction and speed.

    This gives you some idea how you can do it. By the way, I will have to read your code later on, because I am post this on my phone.

    Basically i'm getting my robot to be underwater and picking up some small metal nuts. Forward and turning movement has been taken control by a left and right motor, the submerging of the robot (very very slightly buoyant) will help to reach depths required.

    The BASIC stamp will read in data input from a remote control via a wireless receiver. Then, the BASIC stamp will output data that is required to run the indicated directions to 2 motor drivers. The motor drivers are capable of driving 2 DC motors each.
Sign In or Register to comment.