-
Notifications
You must be signed in to change notification settings - Fork 0
/
boardSim.py
191 lines (177 loc) · 10.2 KB
/
boardSim.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
185
186
187
188
189
190
191
# -----------------------------------------------------------------------------
# Script Name: boardSim.py
# Script Funtion: To simulate a TI Board publishing messages to MQTT
# Date Created: 05/06/2020
# Author: Kulneet Singh
# -----------------------------------------------------------------------------
# Neccesary Libraries
import time
import datetime
import paho.mqtt.client as mqtt
import paho.mqtt.subscribe as subscribe
import json
from collections import defaultdict
from signal import signal, SIGINT
from sys import exit
# Function ran whenever script publishes anything
def on_publish(client,userdata,result): #create function for callback
print('Sent MoveMotor with ret code: ' + str(result))
# Set-up info for server
broker_address = "172.16.0.29"
port = 1883
# Establishing connection with RPi server
client = mqtt.Client('boardSim')
client.on_publish = on_publish
client.connect(broker_address, port=port)
# Defining test cases
def testCase1():
print("\nRunning test case 1...")
ret = client.publish("/cc3200/MoveMotors", "{\"type\": \"DROP\", \"motor0\": 180, \"motor1\": 90, \"motor2\": 70, \"motor3\": 90}")
msg = subscribe.simple("/server/DoneMoving", msg_count=2)
ret = client.publish("/cc3200/MoveMotors", "{\"type\": \"PICK\", \"motor0\": 37, \"motor1\": 100, \"motor2\": 90, \"motor3\": 160}")
msg = subscribe.simple("/server/DoneMoving", msg_count=2)
ret = client.publish("/cc3200/MoveMotors", "{\"type\": \"DROP\", \"motor0\": 37, \"motor1\": 160, \"motor2\": 100, \"motor3\": 40}")
msg = subscribe.simple("/server/DoneMoving", msg_count=2)
ret = client.publish("/cc3200/MoveMotors", "{\"type\": \"DROP\", \"motor0\": 180, \"motor1\": 160, \"motor2\": 90, \"motor3\": 40}")
msg = subscribe.simple("/server/DoneMoving", msg_count=2)
ret = client.publish("/cc3200/MoveMotors", "{\"type\": \"DROP\", \"motor0\": 180, \"motor1\": 160, \"motor2\": 90, \"motor3\": 90}")
msg = subscribe.simple("/server/DoneMoving", msg_count=2)
ret = client.publish("/cc3200/MoveMotors", "{\"type\": \"PICK\", \"motor0\": 90, \"motor1\": 100, \"motor2\": 90, \"motor3\": 90}")
msg = subscribe.simple("/server/DoneMoving", msg_count=2)
print("Recieved done.\n")
def testCase2():
print("\nRunning test case 2...")
ret = client.publish("/cc3200/MoveMotors", "{\"type\": \"PICK\", \"motor0\": 180, \"motor1\": 90, \"motor2\": 80, \"motor3\": 90}")
msg = subscribe.simple("/server/DoneMoving", msg_count=2)
ret = client.publish("/cc3200/MoveMotors", "{\"type\": \"DROP\", \"motor0\": 180, \"motor1\": 170, \"motor2\": 85, \"motor3\": 40}")
msg = subscribe.simple("/server/DoneMoving", msg_count=2)
ret = client.publish("/cc3200/MoveMotors", "{\"type\": \"PICK\", \"motor0\": 40, \"motor1\": 160, \"motor2\": 110, \"motor3\": 40}")
msg = subscribe.simple("/server/DoneMoving", msg_count=2)
ret = client.publish("/cc3200/MoveMotors", "{\"type\": \"PICK\", \"motor0\": 40, \"motor1\": 170, \"motor2\": 110, \"motor3\": 40}")
msg = subscribe.simple("/server/DoneMoving", msg_count=2)
ret = client.publish("/cc3200/MoveMotors", "{\"type\": \"PICK\", \"motor0\": 40, \"motor1\": 110, \"motor2\": 85, \"motor3\": 160}")
msg = subscribe.simple("/server/DoneMoving", msg_count=2)
ret = client.publish("/cc3200/MoveMotors", "{\"type\": \"DROP\", \"motor0\": 180, \"motor1\": 110, \"motor2\": 85, \"motor3\": 160}")
msg = subscribe.simple("/server/DoneMoving", msg_count=2)
ret = client.publish("/cc3200/MoveMotors", "{\"type\": \"DROP\", \"motor0\": 180, \"motor1\": 110, \"motor2\": 70, \"motor3\": 160}")
msg = subscribe.simple("/server/DoneMoving", msg_count=2)
ret = client.publish("/cc3200/MoveMotors", "{\"type\": \"DROP\", \"motor0\": 180, \"motor1\": 110, \"motor2\": 70, \"motor3\": 90}")
msg = subscribe.simple("/server/DoneMoving", msg_count=2)
ret = client.publish("/cc3200/MoveMotors", "{\"type\": \"PICK\", \"motor0\": 90, \"motor1\": 100, \"motor2\": 90, \"motor3\": 90}")
msg = subscribe.simple("/server/DoneMoving", msg_count=2)
print("Recieved done.\n")
def testCase3():
print("\nRunning test case 3...")
ret = client.publish("/cc3200/MoveMotors", "{\"type\": \"DROP\", \"motor0\": 180, \"motor1\": 90, \"motor2\": 80, \"motor3\": 90}")
msg = subscribe.simple("/server/DoneMoving", msg_count=2)
ret = client.publish("/cc3200/MoveMotors", "{\"type\": \"PICK\", \"motor0\": 37, \"motor1\": 100, \"motor2\": 100, \"motor3\": 160}")
msg = subscribe.simple("/server/DoneMoving", msg_count=2)
ret = client.publish("/cc3200/MoveMotors", "{\"type\": \"PICK\", \"motor0\": 37, \"motor1\": 30, \"motor2\": 150, \"motor3\": 90}")
msg = subscribe.simple("/server/DoneMoving", msg_count=2)
ret = client.publish("/cc3200/MoveMotors", "{\"type\": \"PICK\", \"motor0\": 180, \"motor1\": 10, \"motor2\": 150, \"motor3\": 90}")
msg = subscribe.simple("/server/DoneMoving", msg_count=2)
ret = client.publish("/cc3200/MoveMotors", "{\"type\": \"PICK\", \"motor0\": 90, \"motor1\": 100, \"motor2\": 90, \"motor3\": 90}")
msg = subscribe.simple("/server/DoneMoving", msg_count=2)
print("Recieved done.\n")
def testCase4():
print("\nRunning test case 4...")
ret = client.publish("/cc3200/MoveMotors", "{\"type\": \"PICK\", \"motor0\": 180, \"motor1\": 10, \"motor2\": 100, \"motor3\": 90}")
msg = subscribe.simple("/server/DoneMoving", msg_count=2)
ret = client.publish("/cc3200/MoveMotors", "{\"type\": \"PICK\", \"motor0\": 40, \"motor1\": 30, \"motor2\": 150, \"motor3\": 90}")
msg = subscribe.simple("/server/DoneMoving", msg_count=2)
ret = client.publish("/cc3200/MoveMotors", "{\"type\": \"DROP\", \"motor0\": 40, \"motor1\": 110, \"motor2\": 90, \"motor3\": 160}")
msg = subscribe.simple("/server/DoneMoving", msg_count=2)
ret = client.publish("/cc3200/MoveMotors", "{\"type\": \"DROP\", \"motor0\": 180, \"motor1\": 110, \"motor2\": 80, \"motor3\": 160}")
msg = subscribe.simple("/server/DoneMoving", msg_count=2)
ret = client.publish("/cc3200/MoveMotors", "{\"type\": \"PICK\", \"motor0\": 90, \"motor1\": 100, \"motor2\": 90, \"motor3\": 90}")
msg = subscribe.simple("/server/DoneMoving", msg_count=2)
print("Recieved done.\n")
def testCase5():
print("\nRunning test case 5...")
ret = client.publish("/cc3200/MoveMotors", "{\"type\": \"DROP\", \"motor0\": 180, \"motor1\": 90, \"motor2\": 80, \"motor3\": 90}")
msg = subscribe.simple("/server/DoneMoving", msg_count=2)
ret = client.publish("/cc3200/MoveMotors", "{\"type\": \"DROP\", \"motor0\": 180, \"motor1\": 160, \"motor2\": 100, \"motor3\": 40}")
msg = subscribe.simple("/server/DoneMoving", msg_count=2)
ret = client.publish("/cc3200/MoveMotors", "{\"type\": \"DROP\", \"motor0\": 37, \"motor1\": 160, \"motor2\": 100, \"motor3\": 40}")
msg = subscribe.simple("/server/DoneMoving", msg_count=2)
ret = client.publish("/cc3200/MoveMotors", "{\"type\": \"PICK\", \"motor0\": 37, \"motor1\": 30, \"motor2\": 130, \"motor3\": 90}")
msg = subscribe.simple("/server/DoneMoving", msg_count=2)
ret = client.publish("/cc3200/MoveMotors", "{\"type\": \"PICK\", \"motor0\": 180, \"motor1\": 10, \"motor2\": 150, \"motor3\": 90}")
msg = subscribe.simple("/server/DoneMoving", msg_count=2)
ret = client.publish("/cc3200/MoveMotors", "{\"type\": \"PICK\", \"motor0\": 90, \"motor1\": 100, \"motor2\": 90, \"motor3\": 90}")
msg = subscribe.simple("/server/DoneMoving", msg_count=2)
print("Recieved done.\n")
def testCase6():
print("\nRunning test case 6...")
ret = client.publish("/cc3200/MoveMotors", "{\"type\": \"PICK\", \"motor0\": 180, \"motor1\": 10, \"motor2\": 100, \"motor3\": 90}")
msg = subscribe.simple("/server/DoneMoving", msg_count=2)
ret = client.publish("/cc3200/MoveMotors", "{\"type\": \"PICK\", \"motor0\": 37, \"motor1\": 30, \"motor2\": 150, \"motor3\": 90}")
msg = subscribe.simple("/server/DoneMoving", msg_count=2)
ret = client.publish("/cc3200/MoveMotors", "{\"type\": \"DROP\", \"motor0\": 37, \"motor1\": 160, \"motor2\": 100, \"motor3\": 40}")
msg = subscribe.simple("/server/DoneMoving", msg_count=2)
ret = client.publish("/cc3200/MoveMotors", "{\"type\": \"DROP\", \"motor0\": 180, \"motor1\": 170, \"motor2\": 90, \"motor3\": 40}")
msg = subscribe.simple("/server/DoneMoving", msg_count=2)
ret = client.publish("/cc3200/MoveMotors", "{\"type\": \"PICK\", \"motor0\": 90, \"motor1\": 100, \"motor2\": 90, \"motor3\": 90}")
msg = subscribe.simple("/server/DoneMoving", msg_count=2)
print("Recieved done.\n")
def testCase7():
print("\nRunning test case 7...")
ret = client.publish("/cc3200/MoveMotors", "{\"type\": \"PICK\", \"motor0\": 90, \"motor1\": 90, \"motor2\": 90, \"motor3\": 90}")
msg = subscribe.simple("/server/DoneMoving", msg_count=2)
print("Recieved done.\n")
def runLoop():
val = 0
while val != -1:
val = input("\nTestcase # (1-6) to run, '10' to run script, '-1' to end sim: ")
if val == 1:
testCase1()
elif val == 2:
testCase2()
elif val == 3:
testCase3()
elif val == 4:
testCase4()
elif val == 5:
testCase5()
elif val == 6:
testCase6()
elif val == 7:
testCase7()
elif val == 10:
testCase1()
time.sleep(1)
testCase2()
time.sleep(1)
testCase3()
time.sleep(1)
testCase4()
time.sleep(1)
testCase1()
time.sleep(1)
testCase5()
time.sleep(1)
testCase6()
time.sleep(1)
testCase2()
elif val == -1:
print("End of Simulation.")
client.disconnect()
exit(0)
else:
print("Error: Invalid input try again...")
def handler(signal_received, frame):
print('\nSIGINT or CTRL-C detected. Running default test case, then restarting')
val = input("Restart (1) or Exit (0): ")
if val == 1:
testCase7()
runLoop()
else:
print("End of Simulation.")
client.disconnect()
exit(0)
if __name__ == '__main__':
print("Start of Simulation:")
signal(SIGINT, handler)
runLoop()
client.disconnect()
print("End of Simulation.")