Homebrew Angle or Rotation Sensor

 

This sensor is capable of 8 counts per revolution.

The sensor uses two Touch Sensors positioned at 45degrees. They are held in position with LEGO hinges available from S@H #5165. Two hinges on the bottom lock the sensors in place. The blue 2x2 round corners (available in the #7110 Landspeeder) act like cams to provide the quadrature position. A Technic plate (removed for clarity) goes between one of the Touch Sensors and the axle to support the top of the gear.

Side

Top

Bottom

As the gear rotates the 2x2 round corners engage the Touch Sensors in a pattern shown below. This creates a pattern known as Gray code. Each switch could be connected to a separate RCX input and decoded in software. The state diagram tells you whether to add or subtract from the angle as the states change. This could be done in software, but you will be limited in how fast the sensor can rotate before you loose counts.

Here is a Visual Basic program that will handle the rotation sensor. It keeps the old and new states of A and B in the bottom 4 bits of a variable and then checks to see if it needs to add or subtract from the angle. There are 4 possible state transition combinations that should never happen. If they do the sensor is probably rotating too fast. There is a short beep sound if this happens. I used variable 0 for the angle so you can use the VariableChange OLE event to automatically watch the angle change.

'Old and New Touch Switch States are stored in
'the bottom 4 bits of variable 2.
'Bit No.  3   2   1   0
'Age     OLD OLD NEW NEW
'Switch   A   B   A   B
With Spirit1
.SelectPrgm 4 'Program 5
.StopAllTasks 'Make sure everything has stopped
.DeleteAllTasks 'Clean up everything

.BeginOfTask 1 'Rotation Sensor Task
   .SetVar 0, 2, 0 'Set initial angle to 0
   .SetVar 2, 2, 0 'Set initial state to 0
   .Loop 2, 0 'Loop forever
    .SetVar 3, 9, 0 'Var 3 = A
    .SetVar 1, 9, 1 'Var 1 = B
    .SumVar 2, 0, 2 'Shift state left 1 bit
                    'by adding state to itself
    .SumVar 2, 0, 3 'Put As value in bottom bit
    .SumVar 2, 0, 2 'Shift state left 1 bit
    .SumVar 2, 0, 1 'Put Bs value in bottom bit
    .AndVar 2, 2, 15 'Throw away the last old state
    'Add or Subtract 1 from angle based on
    'old state and new state values
    'If new = old you do not add or subtract
    'If bad transition make little beep to warn
    .If 0, 2, 2, 2, 1
     .SumVar 0, 2, 1
    .EndIf
    .If 0, 2, 2, 2, 2
     .SubVar 0, 2, 1
    .EndIf
    .If 0, 2, 2, 2, 3 'bad transition
     .PlaySystemSound 0
    .EndIf
    .If 0, 2, 2, 2, 4
     .SubVar 0, 2, 1
    .EndIf
    .If 0, 2, 2, 2, 6 'bad transition
     .PlaySystemSound 0
    .EndIf
    .If 0, 2, 2, 2, 7
     .SumVar 0, 2, 1
    .EndIf
    .If 0, 2, 2, 2, 8
     .SumVar 0, 2, 1
    .EndIf
    .If 0, 2, 2, 2, 9 'bad transition
     .PlaySystemSound 0
    .EndIf
    .If 0, 2, 2, 2, 11
     .SubVar 0, 2, 1
    .EndIf
    .If 0, 2, 2, 2, 12 'bad transition
     .PlaySystemSound 0
    .EndIf
    .If 0, 2, 2, 2, 13
     .SubVar 0, 2, 1
    .EndIf
    .If 0, 2, 2, 2, 14
     .SumVar 0, 2, 1
    .EndIf
   .EndLoop
.EndOfTask

.BeginOfTask 0 'Run Button Task
 .SetSensorType 0, 1 'Touch on input labeled 1 (A)
 .SetSensorType 1, 1 'Touch on input labeled 2 (B)
 .StartTask 1 'Start Rotation Task
.EndOfTask
End With

The real LEGO Rotation sensor encodes the A and B signals into 4 analog values 1.8, 2.6, 3.8 and 5.1V. The following circuit makes the homebrew sensor look just like the real one. Originally I tried to use an opamp, but on Sven Horstmann's suggestion, I changed it to a comparator and got much better results. The RCX doesn't loose counts until the shaft is rotated quite fast.

 

Click Here to Return to the Main Page