Shop OBEX P1 Docs P2 Docs Learn Events
code problem with accelerometer — Parallax Forums

code problem with accelerometer

joy1joy1 Posts: 32
edited 2012-12-09 19:01 in Accessories
i'm working on a project for our robot to self-level on it's x-axis only. The robot has two front wheel drive wheels and there will be one motor on each side to raise/lower the appropriate wheel to keep the base level as one wheel goes over a 3/4 inch piece of plywood. I can make it work with one side such that it raises and lowers as the robot goes over the plywood. Problem is, I don't know how to create the code for the second side so that it also will raise/lower when the other wheel is on the plywood.

Any help would be much appreciated...

Here is my code
'
[ Title ]
' X-axis two motors.bs2
' Robot has two linear actuators..one for left drive one for right drive.
'self leveling attempt when driving on 3/4 plywood

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

'
[ Variables ]

x VAR Word ' Left/right tilt

pulseLeft VAR Word ' left pulse value

pulseRight VAR Word ' right pulse value

'
[ Initialization ]

pulseLeft = 750 ' Start with all pulses centered
pulseRight = 750

DEBUG "Beep!!!" ' Test piezospeaker
FREQOUT 4, 2000, 3000
DEBUG CLS, " x ", CR ' Axis headings

'
[ Main Routine ]

DO ' Begin main routine.

PULSIN 6, 1, x ' Get tilt pulses.

x = (x MIN 1875 MAX 3125) - 2500 ' -625 to 625 with 0 = no tilt
DEBUG CRSRX, 0, "(", SDEC3 x, ")", CLREOL
' Navigation decisions
IF ABS(x) > 100 THEN ' x > 40...right wheel on plywood then left motor
'moves up UNTIL ~0(level)
pulseLeft = 750 - x

' IF the robot moves with the left wheel of the robot ON the plywood this
'has to occur in reverse...I do not have any code for it

ELSE ' Stay still
pulseLeft = 750
pulseRight = 750
ENDIF
pulseLeft = pulseLeft MIN 650 MAX 850 ' Keep 650 <= durations <= 850
pulseRight = pulseRight MIN 650 MAX 850

PAUSE 20
PULSOUT 13, pulseLeft ' Send control pulses to servos
PULSOUT 12, pulseRight

LOOP ' Repeat main routine.

Comments

  • SRLMSRLM Posts: 5,045
    edited 2012-12-09 16:15
    Why do you use ABS? That would make the robot pulseLeft if it's tilted either way more than 100 units. You should do something like
    IF x > 100 THEN
       pulseLeft = 750 - x
    ELSEIF x < -100 THEN
       pulseRight = 750 + x
    ELSE
       ...
    
    Don't take this for syntactically correct code, but for the idea it presents.

    ps: this seams very similar to what might happen for the FTC "Ring it Up" challenge... http://www.usfirst.org/roboticsprograms/ftc/game

    pps: please also use the [ code ] [ /code ] tags (without the spaces) when posting code.
  • joy1joy1 Posts: 32
    edited 2012-12-09 16:51
    SRLM..thanks for your quick post...I have tried that solution but it doesn't work since there somehow has to be a separation between the two motors such that when left motor goes moves up it will move down as well....Then, if the other side needs to go up (goes on plywood) then it has to be allowed to go down and not mixed up with the other motor...hope this makes sense

    On another note, I think I've seen that USfirst stuff before...is that what you do?
  • SRLMSRLM Posts: 5,045
    edited 2012-12-09 17:21
    Hmm, I'm not really understanding what you said. If you wanted both motors to move, couldn't you just add in the second motor?
    IF x > 100 THEN
       pulseLeft = 750 - x
       pulseRight = 750
    ELSEIF x < -100 THEN
       pulseRight = 750 + x
       pulseLeft = 750
    ELSE
       ...
    

    Maybe you're saying that at one point, the left motor goes on the plywood while the right motor is not. You want the robot to level itself. Then it drives forward, and now both motors are on the plywood. You want the robot to level itself again. Is this correct?

    If that is the case, you can do this with a simple loop (no need to remember past robot state). Just fill in the else case with the motors in even position.
  • joy1joy1 Posts: 32
    edited 2012-12-09 18:09
    We'll be loading and unloading pallets constantly on an unlevel loading platform. The platform will be 3/4 thick plywood and we have to place pallets in a shelving unit up to 3 feet high. the robot will be partially on the platform and partially off for loading onto the side shelves. this makes it hard to get the pallet in the shelf, especially for the top shelves since the slant of the robot is exagerated the higher you are. To solve this problem, we are attempting to use the accelerometer to "self-level". Currently, the code I presented will work for one of the sides. (let's say the left side) When the robot goes on the furthest part of the platform only one wheel is on, one is off. The accelerometer will corrrect this problem and level the robot so our loading mechanism is level. when we leave the platform, the program works to once again, self-level since both wheels are now on the ground. Now the problem is when I'm on the other side of the platform (the right side) I need the right motor to do the same thing. But, I can not figure out a way to work the second actuator to self-level without getting it confused with the first or left hand side actuator. Do I need to use two accelerometers to get the job done, or, can it be done by using code. It would be nice to be able to figure it out with code to save a few bucks.

    thanks
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-12-09 19:01
    No, you don't need a second accelerometer.

    You need to get rid of the absolute value function as SLRM suggested. If x if positive you know it's tilting one way, if x is negative you're tilting the other direction.

    You might not be able to use SLRM's exact code but he showed you the general idea. You say "I have tried that solution but it doesn't work". Then post what your tried and tell us what you expected it to do and also tell us what it did do. I'm sure someone around here can help you figure this out.

    By using the absolute value of x your losing the ability to sense one of the directions of tilt.
Sign In or Register to comment.