Rotary encoder / Wheel encoder by manticus 3d model
3dmdb logo
Thingiverse
Rotary encoder / Wheel encoder by manticus

Rotary encoder / Wheel encoder by manticus

by Thingiverse
Last crawled date: 5 years ago
Wheel speed
Why ?
Convert a binary information in a speed and with direction knowledged
Requirement
1 GPIO (Raspberry pi)
2 endstop sensors
1 spring
3 nails
6 screws (2 by endstop and 2 for spring)
Print parts
Wheel
Diameter : 50mm
4 holes so 4 states 0 and 4 states 1 for a complet revolution
Main part
To fix spring, wheel and sensors
Holder
To fix main part and spring
What we want to get on the raspberry GPIO
number of change of state 0 -> 1 -> 0 -> 1 ... by rotation : 8
Config
You can change settings:
pin numbers used by 2 endstop sensors (23 & 24 by default)
wheel diameter (5cm by default)
number of holes in wheel (4 by default)
Code part
Create a file "nb_turn_second.py"
Add permissions (chmod +x nb_turn_second.py"
Insert this script inside and run it with "./nb_turn_second.py"
#!/usr/bin/python

import RPi.GPIO as GPIO
import time

print "starting..."

pin_id = 23
pin2_id = 24

wheel_diameter = 5 #cm
nb_holes_in_wheel = 4

nb_states_by_rotation = float(nb_holes_in_wheel * 2)
pi = 3.14
wheel_perimeter = pi * wheel_diameter

GPIO.setmode(GPIO.BCM)
GPIO.setup(pin_id, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(pin2_id, GPIO.IN, pull_up_down=GPIO.PUD_UP)

last_state = 0
nb_states = 0

previous_second = 0
previous_nb_states = 0
is_direction_A = True

try:
while True:
input_state = GPIO.input(pin_id)
input2_state = GPIO.input(pin2_id)

if input_state != last_state:
if input_state == input2_state:
nb_states -= 1
if is_direction_A:
is_direction_A = False
print('direction changed')
else:
nb_states += 1
if not is_direction_A:
is_direction_A = True
print('direction changed')
last_state = input_state

current_second = time.strftime('%S')
if current_second != previous_second:
if nb_states != previous_nb_states:
previous_nb_states = nb_states
nb_rotation = 0
if previous_nb_states != 0:
nb_rotation = previous_nb_states / nb_states_by_rotation
speed_second = nb_rotation * wheel_perimeter
print ('nb rotation: ' + str(nb_rotation) + '; speed: ' + str(speed_second) + " cm/s")
previous_second = current_second
nb_states = 0

time.sleep(0.01)

except:
GPIO.cleanup()
This script will display every second the new speed only if the speed changed
output example
starting...
nb rotation: 0.875; speed: 13.7375 cm/s
nb rotation: 0.75; speed: 11.775 cm/s
nb rotation: 1.0; speed: 15.7 cm/s
direction changed
nb rotation: 1.25; speed: 19.625 cm/s
nb rotation: -0.625; speed: -9.8125 cm/s
nb rotation: -0.75; speed: -11.775 cm/s
nb rotation: -0.25; speed: -3.925 cm/s
nb rotation: 0; speed: 0.0 cm/s

Tags