-
Notifications
You must be signed in to change notification settings - Fork 0
/
Precise_Angle_Controller.py
186 lines (152 loc) · 4.79 KB
/
Precise_Angle_Controller.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# (C) Ansh Chablani
# All Rights Reserved
#
# PD controller for the robot to turn precisely that many angles
# Here I am basing the input to be received from the shell.
#
# For an angle controller
#
# Use: To turn and drive straight (in case of 0 degrees)
# Goal: Difference in encoder counts corresponding to a certain angle.
# Error: Goal - (Left Counts - Right Counts)
# Output: Add to one motor, subtract from the other.
#
# I am taking the convention of acw rotation to be positive and cw rotation to be negative.
from RobotAPI import Micromouse
from machine import Pin, PWM
from time import sleep
import time
from rotary_irq_rp2 import RotaryIRQ
import math
mm = Micromouse()
kona = float(input("Enter angle to rotate: "))
positive = 0
if(kona >= 0):
print("Positive Angle Provided")
positive = 1
else:
print("Negative Angle Provided")
positive = 0
# kona = kona * (950/90)
move = 1
errorLeft = 0
errorRight = 0
print("1")
Kp = 35000 # To be detemined accurately
Kd = 5000
# In cm
radius = 16 // 2
print("2")
# kona * r
distanceToMove = kona * radius * (math.pi / 180)
print("3")
leftOneRotationEncValue = 343
print("4")
rightOneRotationEncValue = 347
circumference = 10.053 # In cm
print("5")
leftEncValuePerCM = leftOneRotationEncValue / circumference
rightEncValuePerCM = rightOneRotationEncValue / circumference
print("6")
toMoveLeft = (leftEncValuePerCM * distanceToMove + rightEncValuePerCM * distanceToMove) / 2
toMoveRight = (leftEncValuePerCM * distanceToMove + rightEncValuePerCM * distanceToMove) / 2
# So what I want is that we should be able to make the left and the right motor until both of them move the same amount
# That is decided by the average of the encoder steps they are required to move individually.
# First take count of the initial encoder steps.
print("7")
oldPosLeft = mm.getLeftEncoderValue()
oldPosRight = mm.getRightEncoderValue()
print("8")
# Then let us have counts for the moves that have happened in the left and the right motor.
movedLeft = 0
movedRight = 0
print(toMoveLeft, toMoveRight)
flag1 = 0
flag2 = 0
# Initial PWM supplied
PWMLeft = 40000
PWMRight = 40000
print(toMoveLeft, toMoveRight)
while True:
if(input()):
break
while True:
# First set the positions of the motors
if flag1 == 0:
print("Here before enableLeftMTRFWD")
print(positive)
if positive == 1:
print("here")
mm.enableLeftMTRFWD()
elif positive == 0:
mm.enableLeftMTRBWD()
else:
print("Here before mm.leftMTRStop()")
mm.leftMTRStop()
#break # this is for testing
PWMLeft = 0
if flag2 == 0:
print("Here before mm.enableRightMTRBWD()")
if positive == 1:
mm.enableRightMTRBWD()
elif positive == 0:
mm.enableRightMTRFWD()
else:
print("Here before mm.rightMTRStop().......................................")
mm.rightMTRStop()
#break # this is for testing
PWMRight = 0
# Relaxation time to stop
time.sleep_ms(5)
# Then give them some speed
if not (flag1 and flag2):
mm.setLeftMTR_PWM(PWMLeft)
mm.setRightMTR_PWM(PWMRight)
currentPosLeft = mm.getLeftEncoderValue()
currentPosRight = mm.getRightEncoderValue()
if oldPosLeft != currentPosLeft:
oldPosLeft = currentPosLeft
if oldPosRight != currentPosRight:
oldPosRight = currentPosRight
# Change speed based if the destination is close
if abs(currentPosLeft) >= abs(toMoveLeft) - 100:
PWMLeft -= 20000
if abs(currentPosRight) >= abs(toMoveRight) - 100:
PWMRight -= 20000
# if abs(oldPosLeft) >= toMoveLeft - 50:
# PWMLeft -= 20000
#
# if abs(oldPosRight) >= toMoveRight - 50:
# PWMRight -= 20000
#
# if abs(oldPosLeft) >= toMoveLeft - 10:
# PWMLeft -= 15000
#
# if abs(oldPosRight) >= toMoveRight - 10:
# PWMRight -= 15000
# Stop if the destination is reached.
if abs(currentPosLeft) >= abs(toMoveLeft):
print("Reached Left")
mm.leftMTRStop()
PWMLeft = 0
# Relaxation time to stop
time.sleep_ms(5)
flag1 = 1
if abs(currentPosRight) >= abs(toMoveRight):
print("Reached Right")
PWMRight = 0
print("rightMTRStop.................., flag 2 is set")
mm.rightMTRStop()
# mm.bothMotorsStop()
# Relaxation time to stop
time.sleep_ms(5)
flag2 = 1
print("flag1 =", flag1, "flag2 =", flag2)
if(flag1 and flag2):
print("-----------break---------------------")
break
print(currentPosLeft, currentPosRight)
time.sleep_ms(5)
print(currentPosLeft, currentPosRight)
#mm.bothMotorsStop()
print("10")