Shop OBEX P1 Docs P2 Docs Learn Events
radar system using PING sensor and stepper motor.... — Parallax Forums

radar system using PING sensor and stepper motor....

neddnedd Posts: 19
edited 2011-03-26 09:36 in Robotics
i wan build mini project about PING Sensor that detect distance... and i wan make this sensor work with stepper motor..

this is my simple radar will works:
start>>motor rotate in cw & ccw>>sensor detect the object>>motor stop>>show the distance

my coding:
' {$STAMP BS2}
' {$PBASIC 2.5}
Phase VAR OUTB 'phase control outputs
StpsPerRev CON 24 'whole steps per rev (24=180, 48-=360)
idx VAR Byte 'loop counter
stpldx VAR Nib 'step pointer
Steps DATA %0011, %0110, %1100, %1001

Setup:
DIRB=%1111 'make P4-P7 outputs

Main:
FOR idx= 1 TO StpsPerRev 'one revolution
GOSUB STEP_Fwd 'rotate clockwise
NEXT
PAUSE 1000 'wait 1 second
FOR idx = 1 TO StpsPerRev 'one revolutionGOSUB Step_Rev 'rotate counter-clockwise
NEXT
PAUSE 1000 'wait 1second
GOTO Main
END
Step_Fwd:
stpldx = stpldx+1//4 'point to next step
GOTO Do_Step
Step_Rev:
stpldx = stpldx+3//4 ' point to previous step
GOTO Do_Step
Do_Step:
READ(Steps+stpldx), Phase ' outputs new phase data
PAUSE 1000 ' delay between steps

'RECEIVE THE SIGNAL
cmconstant CON 2260
cmdistance VAR Word
time VAR Word

PULSOUT 15,5 'port 15
PULSIN 15,1,time

cmdistance=cmconstant**time
END

but not work... some can help?

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2011-03-25 09:45
    There is no RETURN statement at the end of the Step_Fwd subroutine. You call Step_Fwd, it GOTOs Do_Step which steps the motor, reads the PING data, calculates cmdistance, and stops everything at the END statement.
  • neddnedd Posts: 19
    edited 2011-03-25 09:58
    new coding:

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    ' {$PORT COM13}
    Phase VAR OUTB 'phase control outputs
    StpsPerRev CON 48 'whole steps per rev
    idx VAR Byte 'loop counter
    stpldx VAR Nib 'step pointer
    stpDelay VAR Byte 'delay for speed control
    Steps DATA %0011,%0110,%1100,%1001

    Setup:
    DIRB = %1111 'make P4..P7 outputs
    stpDelay = 15 'set step delay

    Main:
    FOR idx = 1 TO StpsPerRev 'one revolution
    GOSUB Step_Fwd 'rotate clockwise
    NEXT
    PAUSE 500 'wait 1/2 second
    FOR idx = 1 TO StpsPerRev 'one revolution
    GOSUB Step_Rev 'rotate counter-clockwise
    NEXT
    PAUSE 500 'wait 1/2 second
    GOTO main
    END

    Step_Fwd:
    stpldx = stpldx + 1 // 4 'point next step
    GOTO DO_Step

    Step_Rev:
    stpldx = stpldx + 3//4 'point to previous step
    GOTO DO_Step

    Do_Step:
    READ(Steps+stpldx), Phase 'output new phase data
    PAUSE stpDelay 'pause between step
    RETURN

    cmconstant CON 2260
    cmdistance VAR Word
    time VAR Word

    DO

    PULSOUT 15,5
    PULSIN 15,1,time

    cmdistance=cmconstant**time

    DEBUG HOME,"distance1=",DEC3 cmdistance,"cm"

    PAUSE 1000

    LOOP

    END

    motor work... sensor cant show distance.... just blue
  • Mike GreenMike Green Posts: 23,101
    edited 2011-03-25 10:05
    Download and read the "What's a Microcontroller?" tutorial and download the "Basic Stamp Syntax and Reference Manual". You need to first learn how to program the Stamp ... how to make your program work. In particular, read about the GOSUB and RETURN statements and the DO / LOOP statement. Both of these references are included with the Stamp Editor's help files. After you've looked at those, if you still don't understand why your program doesn't work, ask again.
  • neddnedd Posts: 19
    edited 2011-03-26 08:23
    thank you.....
    this time motor work with PING sensor... but how to make... motor will stop after PING sensor detect the object below 314cm?

    my coding:
    ' {$STAMP BS2}
    ' {$PBASIC 2.0}
    ' {$PORT COM13}

    Phase VAR OUTB
    StpsPerRev CON 48
    idx VAR Byte
    stpldx VAR Nib
    stpDelay VAR Byte
    Steps DATA %0011,%0110,%1100,%1001
    cmconstant CON 2260
    cmdistance VAR Word
    time VAR Word


    Setup:
    DIRB = %1111
    stpDelay = 15

    Main:
    FOR idx = 1 TO StpsPerRev
    GOSUB Step_Fwd
    NEXT
    PAUSE 500
    FOR idx = 1 TO StpsPerRev
    GOSUB Step_Rev
    NEXT
    PAUSE 500
    GOTO main
    END

    Step_Fwd:
    stpldx = stpldx + 1 // 4
    GOTO DO_Step

    Step_Rev:
    stpldx = stpldx + 3 // 4
    GOTO DO_Step

    Do_Step:
    READ(Steps+stpldx), Phase
    DO
    PULSOUT 15,5
    PULSIN 15,1,time
    cmdistance=cmconstant**time
    DEBUG HOME,"Distance From Radar=",DEC3 cmdistance,"cm"
    PAUSE 1000
    LOOP
    PAUSE stpDelay
    RETURN
  • Mike GreenMike Green Posts: 23,101
    edited 2011-03-26 09:36
    There is no way your program can work as you describe. The way you've written it, it will do one step forwards, then repeatedly display the distance on the debug window.

    Please, read the description of DO / LOOP in the Manual. Try removing the DO and LOOP in the Do_Step routine.
Sign In or Register to comment.