-
Notifications
You must be signed in to change notification settings - Fork 0
/
follow_row.py
121 lines (103 loc) · 3.82 KB
/
follow_row.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
from pickle import TRUE
from statistics import mode
import sys, time
from matplotlib.style import available
sys.path.insert(1, 'modules')
from modules import control_Rover,vision_Rover
import keyboard
import cv2
grnd_Speed = 1600
mode = 'test'
control = 'PID'
MAX_ALT = 0
STATE = "takeoff" # takeoff land track search - To begin first set STATE to takeoff, for the rover --> Arm
def setup():
global cap
#Connecting the Rover
print("Connecting to Agribot")
if mode == "drive":
print("MODE = Drive")
control_Rover.connect_rover('/dev/ttyTHS1') #Refer Control Module
else:
print("MODE = test") # Launch Simulator if rover is not connected
control_Rover.connect_rover('tcp:127.0.0.1:5763')
cap = cv2.VideoCapture("slownewvideo.mp4")
setup()
#PID
control_Rover.configure_PID(control)
def readFrames():
global ret,frame
ret,frame = cap.read()
resizeframe = cv2.resize(frame,(640,480),interpolation = cv2.INTER_AREA)
#cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
return "holdMission"
return resizeframe
def trackPath():
print("State is TRACKING -> " + STATE) # Setting the STATE to path tracking
while True:
if keyboard.is_pressed('q'): # if key 'q' is pressed
print("Closing due to manual interruption")
holdMission() # Closes the loop and program
if tracks == TRUE:
cropRawAngle = vision_Rover.getAngle(readFrames()) # Function to take angle of the row and the heading of the rover
#MA_X.append(cropRawAngle)
lenth = 1
if lenth > 0:
#while 1:
# print("ANGLE FOUNDED")
#x_delta_MA = calculate_ma(MA_X) #Function to scale to millisecond values(Not Necessary)
control_Rover.setPathdelta(cropRawAngle)
steer_command = control_Rover.getMovementSteerAngle() #Return PID calculated values
print(steer_command)
control_Rover.control_rover() #In the back od the code, this funtions write mavlink command with PID calculated value
print("Rover Controlled")
else: # Keep search
return "search"
def search():
global tracks
print("State is SEARCH -> " + STATE)
start = time.time()
control_Rover.stop_rover()
while time.time() - start < 20:
if keyboard.is_pressed('q'): # if key 'q' is pressed
print("Closing due to manual interruption")
holdMission() # Closes the loop and program
if vision_Rover.detect(readFrames()): #Row Detector: Should return TRUE
#while 1:
# print("ROW FOUNDED")
print("Row is Founded")
tracks = TRUE #Checking if there are rows to follow
return "track"
return "hold"
def takeoff():
control_Rover.print_rover_report()
print("State = TAKEOFF -> " + STATE)
control_Rover.arm_and_takeoff() #start control when Agribot is ready
return "search"
def hold():
print("State = Hold -> " + STATE)
control_Rover.stop_rover()
def holdMission():
print("State = Hold Mission -> " + STATE)
control_Rover.disarm()
cap.relese()
cv2.destroyAllWindows()
sys.exit(0)
print("Initialization is completed")
while True:
# main program loop
"""" True or False values depend whether or not
a PID controller or a P controller will be used """
if STATE == "track":
control_Rover.set_system_state("track")
STATE = trackPath()
elif STATE == "search":
control_Rover.set_system_state("search")
STATE = search()
elif STATE == "takeoff":
STATE = takeoff()
elif STATE == "hold":
STATE = hold()
elif STATE == "holdMission":
STATE = holdMission()