Rotary encoder / Wheel encoder
por manticus
Ficheiros imprimíveis (3)
-
stlrotary-encoder-holder.stl
140 Ko · 323 descargas
-
stlrotary-encoder-wheel.stl
89 Ko · 322 descargas
-
stlrotary-encoder-end-stop-x2.stl
704 Ko · 330 descargas
Descrição
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/pythonimport RPi.GPIO as GPIOimport timeprint "starting..."pin_id = 23pin2_id = 24wheel_diameter = 5 #cmnb_holes_in_wheel = 4nb_states_by_rotation = float(nb_holes_in_wheel * 2)pi = 3.14wheel_perimeter = pi * wheel_diameterGPIO.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 = 0nb_states = 0previous_second = 0previous_nb_states = 0is_direction_A = Truetry: 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/snb rotation: 0.75; speed: 11.775 cm/snb rotation: 1.0; speed: 15.7 cm/sdirection changednb rotation: 1.25; speed: 19.625 cm/snb rotation: -0.625; speed: -9.8125 cm/snb rotation: -0.75; speed: -11.775 cm/snb rotation: -0.25; speed: -3.925 cm/snb rotation: 0; speed: 0.0 cm/s