-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobot_control.py
139 lines (112 loc) · 3.23 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
# Line Follower Robot Code for Jetbot
# Deigned by: Akshat Bisht (a.bisht@auckland.ac.nz)
# Designed for: University of Auckland
# Date 18 July 2022
import multiprocessing
import Jetson.GPIO as GPIO
import time
import signal
from jetbot import Robot
robot = Robot()
queue = multiprocessing.Queue()
def lfInit():
global queue, input_pin, input_pin_2
print(GPIO.JETSON_INFO)
print("Initilising Line Following Jetbot")
p = multiprocessing.Process(target=line_follow_run, args=(queue,robot))
p.start()
def lfDeinit():
global queue
sendDic = {'stop':-1}
queue.put(sendDic)
print("Deinitialised")
def lfStop():
global queue
sendDic = {'stop':1}
queue.put(sendDic)
def lfStart():
global queue
sendDic = {'stop':0}
queue.put(sendDic)
def lfSpeed(setSpeed):
global queue
sendDic = {'speed':setSpeed}
queue.put(sendDic)
def lfTurnSpeed(setSpeed):
global queue
sendDic = {'turnSpeed':setSpeed}
queue.put(sendDic)
def lfLineFollow(line_follow_on):
"""Turn line follow on or off"""
global queue
sendDic={"lineFollow": line_follow_on}
queue.put(sendDic)
def line_follow_run(q,lRobot):
input_pin= 15
input_pin_2 = 16
next_move = 0
prev_move = -10
speed = 0.15
turnSpeed = 0.25
stop = 1
oldSpeed = speed
line_follow = True
GPIO.setmode(GPIO.BOARD) # BCM pin-numbering scheme from Raspberry Pi
GPIO.setup(input_pin, GPIO.IN) # set pin as an input pin
GPIO.setup(input_pin_2, GPIO.IN)
while True:
try:
revDic = q.get(False)
for key in revDic.keys():
if key == "speed":
speed = revDic[key]
if key == "stop":
stop = revDic[key]
if key == "turnSpeed":
turnSpeed = revDic[turnSpeed]
if key == "lineFollow":
line_follow = revDic[key]
except:
pass
if stop == -1:
lRobot.stop()
prev_move = 100
break
if stop == 1:
lRobot.stop()
prev_move = 100
continue
if oldSpeed != speed:
oldSpeed = speed
print(speed)
prev_move = 100
if not line_follow:
lRobot.forward(speed)
continue
value = GPIO.input(input_pin)
if value == GPIO.HIGH:
value_str = "HIGH"
next_move = 1
else:
value_str = "LOW"
value = GPIO.input(input_pin_2)
if value == GPIO.HIGH:
value_str_2 = "HIGH"
next_move = -1
else:
value_str_2 = "LOW"
if value_str == "HIGH" and value_str_2 == "HIGH":
next_move = 2
if prev_move != next_move:
robot.stop()
if next_move == -1:
lRobot.set_motors(-0.1,turnSpeed)
if next_move == 1:
lRobot.set_motors(turnSpeed,-0.1)
if next_move == 0:
lRobot.forward(speed)
if next_move == 2:
lRobot.backward(speed)
prev_move = next_move
next_move = 0
GPIO.cleanup()