Rotation Sensor

From: Don Hewitt <donhewitt@worldnet.att.net>
To: <lego-robotics@crynwr.com>
Subject: (long) Rotation Sensor from "Extreme Creatures" Fiber Optics
Date: Tue, 22 Dec 1998 00:20:41 -0600

Several list members have asked for instructions on making a simple
rotation sensor using the standard Mindstorms light sensor coupled to the
light / fiber optic gizmo that comes in the "Extreme Creatures" add-on set.

Here goes:

1) The problem:
Devise a way of tracking distance or counting shaft rotation using
available parts (in this case, Mindstorms and Extreme Creatures).

2) Key parts:
a) Standard Mindstorms Light Sensor - in this application, you only want
the sensor, not the LED.  Place a 1x2 brick with a round hole in front of
the Light Sensor.  In that way, the brick blocks the LED, but the hole
allows light to pass to the sensor.  The bottom of the 1x2 should be at the
same height as the bottom of the sensor.

b) Extreme Creatures fiber optics parts - this consists of a rotating lamp
and eight fiber strands.  In case you haven't seen it, the lamp component has:
  ->One power input (to power the lamp) - connect this to a motor output of
the RCX - you have to run the motor output at full power for this
application to work (more below under "Difficulties").
  ->A hole for a shaft - connect this up to your drive motors / wheels /
differentials, etc.
  ->8 small output holes for the fiber optic strands.  The holes are
arranged in a circle, and a lamp (apparently a LED internal to the
component) is rotated (by the shaft) behind this circle of holes so that
each hole is lit in turn during a rotation.  For this application, use only
one hole & fiber - you will see a flash of light in the fiber once per
every rotation of the shaft.

3) Assembly

+ Mechanical
- Put a shaft through the lamp component.  The sensor will count rotations
of this shaft.
- Put one fiber in one output hole of the lamp component.  It doesn't
matter which hole.
- Put one of the short (1 1/3 length) grey connector pegs onto the free end
of the fiber - it should be a tight fit.
- Put the peg (and end of the fiber) into the hole of a 1x2 brick.  Align
the brick with the hole of a light sensor so that the fiber shines into the
sensor.

+ Electrical
- Connect the light sensor to one of the RCX input ports
- Connect the lamp component to one of the RCX output (motor) ports

+ Programming - see my example - This is spirit.ocx code, but should be
adaptable to the standard RCX software.  

NOTES:
Spirit.ocx labels motors and sensors 0,1,2 rather than 1,2,3 and A,B,C
Put the LAMP on Output C (motor2 in spirit).
Put the LIGHT SENSOR on Input 2 (input 1 in spirit).
I have motors on Outputs A, B (motor0 and motor1 in spirit)
Task 0 sets up inputs and outputs, fires up other tasks, and goes into a
loop waiting for things to happen.
Task 3 is the counter.
My program has two other tasks (Tasks 1 and 2) that looked at other and set
variable #1 when other events occured.
End result is for robot to go forward until Tasks 1 or 2 go off, pause,
then back up to its starting point.
Variables:
  #0 - holds the count of rotations 
  #1 - used to trap the external inputs from tasks 1 and 2
  #2 - if (1) then sensor counts up, if (-1) then sensor counts down
Here's the code:

With Lego
    .SelectPrgm 3
    .DeleteAllTasks
    
    .BeginOfTask 0
        .SetPower "light2", 2, 7		'Turn on the LAMP to full power
        .SetFwd "light2"
        .On "light2"
        .SetSensorType 1, 3 		' set input2 to Light Sensor
        .SetSensorMode 1, 0, 0 		' boolean, std slope
        .SetPower "motor0motor1", 2, 4   ' Turn on the motors
        .SetFwd "motor0motor1"
        .On "motor0motor1"
        
        .SetVar 0, 2, 0     ' set varible #0 to 0 
                            ' variable #0 holds the count from the sensor
        .SetVar 1, 2, 0     ' set variable #1 to 0
        .StartTask 1        ' Tasks 1 and 2 (not listed) set variable #1
				 '    to "1" when other sensors are pushed
        .StartTask 2
        
        .SetVar 2, 2, 1      ' Set variable #2 to "1". (see task 3)
	 .On "motor0motor1"    ' turn on motors and go forward
        .StartTask 3         ' start counting task
       
        .While 0, 1, 2, 2, 0  ' loop until variable #1 becomes non-zero
        .EndWhile		   '  (other tasks have to set this variable!)	
           ' while loop is exited when tasks 1 or 2 detect other sensors
        .StopTask 1            ' Turn off these other tasks
        .StopTask 2
        
        .Off "motor0motor1"     ' stop the robot
        
        .Wait 2, 500			' wait for 5 seconds
            
        .AlterDir "motor0motor1"    ' backup
        .On "motor0motor1"          'resume motion
        
        .SetVar 2, 2, -1            ' set (var#2 = -1)so task 3 counts down
        .While 0, 0, 0, 2, 0        ' while var1 is > 0 (count down)
        .EndWhile
        
        .PlaySystemSound 5		'shut down
        .StopTask 3
        .Float "012"
        .SetSensorType 1, 0        ' turn off light sensor
        .EndOfTask
        
' This is the sensor task - it adds the contents of variable #2
'      to variable #0 every rotation of the shaft.
'      Setting variable #2 to 1 counts up, -1 counts down
'      It also plays an audible click each rotation   
'      High and low limits of 720 and 750 were found by watching
'          value of the light sensor on the RCX display.
'          You may need to fine-tune these parameters     
        .BeginOfTask 3
          .Loop 2, 0                'repeat until task stopped
            .While 9, 1, 0, 2, 720   ' Wait until light > 720
            .EndWhile
            .PlaySystemSound 0      ' click every rev of sensor
            .While 9, 1, 1, 2, 750    ' Wait until light < 750
            .EndWhile
            .SumVar 0, 0, 2            ' add var 2 to count of distance
          .EndLoop
        .EndOfTask
    End With

4) Difficulties / comments
--> Make sure that the lamp is run at full power.  Otherwise, the lamp will
blink on and off at the pulse rate of the motor port, and you will count
extra pulses.
--> You may need to shield the fiber from ambient light.  This is because
the fiber is designed to leak so that you can see light passing through it.
 So light gets in as well!  I found that I would miss rotations if the
robot was in bright light.
--> This sensor well if all you want to do is count rotations.  It doesn't
work if you need to detect angular position within a single shaft rotation
(you can always gear-up the input) and doesn't automatically count up /
down if you go forward / reverse (although you should usually know which
way the motor is being driven.)  
--> I don't know the maximum speed of this sensor / software, but I've
tried it successfully at speeds up to about 2 rotations per second.
--> Use the Lego / Dacta rotation sensor if you have one - it should do
each of these tasks much better!!

Happy building!

Don Hewitt
donhewitt@worldnet.att.net


Russell Nelson
Last modified: Wed Feb 17 23:04:15 EST 1999