/
robot_control.py
162 lines (141 loc) · 4.58 KB
/
robot_control.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
import RPi.GPIO as GPIO
import time
import pygame
import sys
from pygame.locals import *
import os
stop_d = 0#(1.5/21.5)*100
stop_f = 1/(0.0215)
d1 = 1.5
d2 = 1.5
freq1 = 21.5
freq2 = 21.5
panic = 1
run = True
#CONTROL INITIALIZATION--------------------------------
GPIO.setmode(GPIO.BCM)
GPIO.setup(16, GPIO.OUT)
GPIO.setup(5, GPIO.OUT)
#left
p1 = GPIO.PWM(16, 46.1)
p1.start(0)
#right
p2 = GPIO.PWM(5, 46.1)
p2.start(0)
#--------------------------------------------------
#Screen Init--------------------------------------
for_1_d = 1.65/21.65
for_1_f = 1/0.02165
back_1_d = 1.38/21.38
back_1_f = 1/0.02138
#direction clockwise = 1, stop = 0, counter = -1
def full_speed (servo, direction) :
if ( direction == 1 ) :
if ( servo == 1 ) :
p1.ChangeDutyCycle(for_1_d*100)
p1.ChangeFrequency(for_1_f)
if ( servo == 2 ):
p2.ChangeDutyCycle(back_1_d*100)
p2.ChangeFrequency(back_1_f)
if ( direction == 0 ) :
if ( servo == 1 ) :
p1.ChangeDutyCycle(stop_d)
p1.ChangeFrequency(stop_f)
if ( servo == 2 ):
p2.ChangeDutyCycle(stop_d)
p2.ChangeFrequency(stop_f)
if ( direction == -1 ) :
if ( servo == 1 ) :
p1.ChangeDutyCycle(back_1_d*100)
p1.ChangeFrequency(back_1_f)
if ( servo == 2 ) :
p2.start(for_1_d*100)
p2.ChangeFrequency(for_1_f)
def left_turn():
start = time.time()
while ( time.time() - start < 0.5 ) :
full_speed(1, 0)
full_speed(2, 1)
def forward() :
full_speed(1,1)
full_speed(2,1)
def backward():
full_speed(1,-1)
full_speed(2,-1)
def right_turn():
start = time.time()
while ( time.time() - start < 0.5) :
full_speed(2,0)
full_speed(1,1)
def stop():
full_speed(2,0)
full_speed(1,0)
FIFO = '/home/pi/WifiRobot/project/robot_fifo'
cal = 30
still = 0
try :
run = True
fifo = open(FIFO)
side = 0
z = 0
fw_bw = 0
lf_rg = 0
while run :
time.sleep(0.2)
#Read from FIFO
while True:
line = fifo.read()
if len(line) > 2 :
#Move robot according to accelerometer readings
split_line = line.split( "\n" )
for l in split_line :
t = l.split(":")
if cal > 0 :
if ( t[0] == "zValue") :
still_z = float(t[1])
elif ( t[0] == "xValue" ) :
still_x = float(t[1]);
cal = cal - 1;
#Move forward or backwards depending on the ZValue
elif ( t[0] == "zValue" ) :
z = float(t[1])
if ( (z > still_z + 0.1) and (side < still_x - 0.1) ) :
fw_bw = fw_bw + 1
if ( fw_bw > 2 ) :
forward()
print ("forward\n")
elif ( (z < still_z - 0.1) and (side > still_x + 0.1)):
fw_bw = fw_bw + 1
if ( fw_bw > 2 ) :
backward()
print ("backward\n")
else :
fw_bw = 0
if ( lf_rg == 0 ) :
stop()
print ("still\n")
#Move left or right
elif ( t[0] == "xValue" and fw_bw == 0 ):
print("legal turn, x= "+(str(t[1])))
side = float(t[1])
if ( side < still_x - 0.1 ) :
lf_rg = lf_rg + 1
if ( lf_rg > 2 ) :
right_turn()
lf_rg = 0
print ("right\n")
elif ( side >= still_x + 0.05 ):
lf_rg = lf_rg + 1
if ( lf_rg > 2 ) :
left_turn()
lf_rg= 0
print ("left\n")
else :
lf_rg = 0
if ( fw_bw == 0 ) :
stop()
print ("still\n")
except KeyboardInterrupt:
p1.stop()
p2.stop()
GPIO.cleanup()