-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotors.py
88 lines (66 loc) · 2.44 KB
/
motors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# !/bin python
# -*- coding: utf-8 -*-
import RPi.GPIO as io
from time import sleep
io.setmode(io.BCM)
#TODO: obsluzyc wyjatki
class MotorWithEncoder:
def __init__(self, DIR_pin, PWM_pin, ENC_pin=None, circ=339.12, statesPerRotation=64):
io.setup(DIR_pin, io.OUT)
io.setup(PWM_pin, io.OUT)
#Motor
self.DIR_pin = DIR_pin
self.PWM_pin = PWM_pin
self.PWM_MAX = 100
self.PWM = io.PWM(PWM_pin, self.PWM_MAX)
#Encoder
if ENC_pin is not None:
self.ENC_pin = ENC_pin
io.setup(ENC_pin, io.IN, pull_up_down=io.PUD_UP)
self.stateLast = io.input(ENC_pin)
self.rotationCount = 0
self.stateCount = 0
self.stateCountTotal = 0
self.distance = 0
if ENC_pin is None:
raise Warning('Warning! You have to specify encoder channel.')
# TODO: zmierzy dokladnie z opona
self.circ = circ #mm
self.gearRatio = 18.75
self.statesPerRotation = statesPerRotation
self.distancePerStep = circ / statesPerRotation / self.gearRatio
def run(self, speed=0, direction='F'):
if direction == 'F':
io.output(self.DIR_pin, io.LOW)
elif direction == 'B':
io.output(self.DIR_pin, io.HIGH)
else:
raise ValueError('Wrong direction. Should be "F" or "B"')
if speed < 0 or speed > 100:
raise ValueError('Speed is not between <0, 100>.')
self.PWM.start(speed)
self.measure_distance()
def measure_distance(self):
self.stateCurrent = io.input(self.ENC_pin)
if self.stateCurrent != self.stateLast:
self.stateLast = self.stateCurrent
self.stateCount += 1
self.stateCountTotal += 1
if self.stateCount == self.statesPerRotation:
self.rotationCount += 1
self.stateCount = 0
self.distance = self.distancePerStep * self.stateCountTotal
def reset_measurement(self):
self.stateLast = io.input(self.ENC_pin)
self.rotationCount = 0
self.stateCount = 0
self.stateCountTotal = 0
self.distance = 0
def get_distance(self, do_print=True):
if do_print:
print(f'Distance = {self.distance} mm.')
return self.distance
def stop(self):
self.PWM.start(0)
def shutdown(self): #FIXME: Might be static?
io.cleanup()