Shop OBEX P1 Docs P2 Docs Learn Events
How to control my Servo Motor with the IR SENSOR in a BS2 — Parallax Forums

How to control my Servo Motor with the IR SENSOR in a BS2

AndresAndres Posts: 10
edited 2009-10-09 22:29 in BASIC Stamp
This program is from parallax and calculates the binary number, Decimal Value and Voltage from a sensor IR.

[noparse][[/noparse] Title ]
' Basic Analog and Digital - PL3_1R0.bs2
' Program Listing 3.1 Revision 0.
' {$STAMP BS2}
' {$PBASIC 2.5}
'
[noparse][[/noparse] Declarations ]
adcBits VAR Byte
v VAR Byte
r VAR Byte
v2 VAR Byte
v3 VAR Byte
'
[noparse][[/noparse] Initialization ]
CS PIN 0
CLK PIN 1
DataOutput PIN 2
DEBUG CLS 'Start display.
'
[noparse][[/noparse] Main Routine ]
DO
GOSUB ADC_Data
GOSUB Calc_Volts
GOSUB Display
LOOP
'
[noparse][[/noparse] Subroutines ]
ADC_Data:
LOW CLK
LOW CS
PULSOUT CLK, 210
SHIFTIN DataOutput,CLK,MSBPOST,[noparse][[/noparse]adcBits\8]
HIGH CS
RETURN

Calc_Volts:
v = 5 * adcBits / 255 '? new line
r = 5 * adcBits // 255 '? new line
v2 = 100 * R / 255 '? new line
v3 = 100 * r // 255 '? new line
v3 = 10 * v3 / 255 '? new line
IF (v3 >= 5) THEN v2 = v2 + 1 '? new line
IF (v2 >= 100) THEN '? new line
v = v + 1 '? new line
v2 = 0 '? new line
ENDIF '? new line
RETURN

Display:
DEBUG HOME
DEBUG "8-bit binary value: ", BIN8 adcBits
DEBUG CR, CR, "Decimal value: ", DEC3 adcBits '? new line
DEBUG CR, CR, "DVM Reading: " '? new line
DEBUG DEC1 v, ".", DEC2 v2, " Volts" '? new line
RETURN

My question is how I can scale my output of ADC to the motion continuous Servo Motor in a program.

I was thinking using PWM FUNCTION TO DO IT !!!!!!!!!!!

Also how I can can store those outputs values from my ADC.

Measurement GP2D12 and ADC031 CONVERTOR ( Room light and hand)
Distance Vin Vout Decimal slope (Vout/Vin Current (A )
10 4.9828 5 255 1.003451874 0 0.11
12 4.9828 4.45 227 0.893072168 0 0.11
14 4.9828 3.72 192 0.746568195 0 0.11
16 4.9828 3.27 167 0.656257526 0 0.11
18 4.9828 2.98 152 0.598057317 0 0.11
20 4.9828 2.57 131 0.515774263 0 0.11
22 4.9828 2.31 119 0.463594766 0 0.11
24 4.9828 2.18 111 0.437505017 0 0.11
26 4.9828 1.94 99 0.389339327 0 0.11
28 4.9828 1.88 96 0.377297905 0 0.11
30 4.9828 1.8 86 0.361242675 0 0.11

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-10-09 20:29
    What do you mean by "scale my output of ADC to the motion continuous Servo Motor in a program". Give a bit more of a description and maybe some examples.

    What do you mean by "how I can can store those outputs values from my ADC"? Store what? Store where?

    "using PWM FUNCTION TO DO IT !!!". Do what? Have you read the description of the PWM statement? Do you understand how it's used?
  • AndresAndres Posts: 10
    edited 2009-10-09 21:04
    The project is about moving a curtain between 10cm to 30cm with a Servo Motor. But the movement of the hand will determine the position of the curtain towards IR sensor.

    Structure:

    Sensor is connected to the AD Converter, then the output of AD converter is a binary number. There is where I don't know what to do with these binary numbers that come from the IR Sensor or where to store it. So I can use the Binary Output for the motion of the servo motor.

    Scaling meaning depending the movement of the hand there is different values or voltages that the IR SENSOR WILL DETECT.

    These are the scaing values

    DistancE (cm) Vin Vout Decimal value
    10 4.9828 5 255
    12 4.9828 4.45 227
    14 4.9828 3.72 192
    16 4.9828 3.27 167
    18 4.9828 2.98 152
    20 4.9828 2.57 131
    22 4.9828 2.31 119
    24 4.9828 2.18 111
    26 4.9828 1.94 99
    28 4.9828 1.88 96
    30 4.9828 1.8 86
  • Mike GreenMike Green Posts: 23,101
    edited 2009-10-09 21:38
    First you have to understand how to use a servo motor. Download and read the "Robotics with the BoeBot" tutorial. Go to the main Parallax webpage and click on the "Resources" tab. On that page, select "Downloads and Press", then "Stamps in Class Downloads" and you'll see a link to the tutorial (and others). You'll see how the position of a standard servo is determined by the width of a control pulse (usually 1ms to 2ms) that's usually sent with a PULSOUT statement. The PULSOUT statement works in units of 2us (on the BS2) so the value used ranges from 500 to 1000. You have to adjust the value you get from the IR sensor to produce a value for the PULSOUT statement. Usually this is done with a multiplication. Look at the "*/" operator description in the Stamp Basic Manual for a convenient way to do this sort of adjustment.

    If you're using a continuous motion servo, the pulse width translates into direction and speed rather than position. I don't recommend moving the curtain that way since you lose the position feedback. A continuous motion servo has no way to know its position. If you can, use a standard servo.
  • AndresAndres Posts: 10
    edited 2009-10-09 22:13
    Thank you I will go over "robotics with the BoeBot". But I have to use a continuous servo motor
  • Mike GreenMike Green Posts: 23,101
    edited 2009-10-09 22:29
    If you use a continuous servo motor, you won't be able to determine the position of the curtain without using some kind of sensor. I can't tell from your description if you're trying to use the IR sensor to determine that.

    The "Robotics with the BoeBot" tutorial will show you how to adjust your continuous motion servo so it stops moving when the control pulse width is 1.5ms (1500us or a PULSOUT count of 750). A longer pulse will cause the servo to rotate in one direction and a shorter pulse will cause the servo to rotate in the other direction.
Sign In or Register to comment.