-
Notifications
You must be signed in to change notification settings - Fork 0
/
FINALE.py
executable file
·327 lines (253 loc) · 10.9 KB
/
FINALE.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import picamera
import time
import pika
from ftplib import FTP
import datetime
import sys
import serial
import json
import codecs
from urllib.request import urlopen
import smbus
import geocoder
import requests
import pygame
import os
import subprocess
now = datetime.datetime.now().time()
#usbport= '/dev/ttyACM0'
usbport= '/dev/ttyUSB0'
ser = serial.Serial(usbport,115200,timeout=1)
bus = smbus.SMBus(1)
address = 0x60
##create connection pika
credentials = pika.PlainCredentials('turret.tank030', 'TA171801030')
parameters = pika.ConnectionParameters('167.205.7.226',5672,'/turret',credentials)
#credentials = pika.PlainCredentials('rangga', '1234')
#parameters = pika.ConnectionParameters('192.168.43.49',5672,'/',credentials)
connection = pika.BlockingConnection(parameters)
channel = connection.channel()
args={'x-max-length':2}
channel.queue_declare(queue='raspitoholo', durable=False, arguments=args )
channel.exchange_declare(exchange='ex_raspitoholo', exchange_type='topic', durable=False, arguments=args)
channel.queue_bind(queue='raspitoholo',
exchange='ex_raspitoholo',
routing_key='rk_raspitoholo'
)
channel.queue_declare(queue='holotoraspi', durable=False,
arguments=args)
channel.queue_declare(queue='inTopic', durable=False, arguments=args)
channel.exchange_declare(exchange='amq.topic', exchange_type='topic', durable=True, arguments=args)
channel.queue_bind(queue='inTopic',
exchange='amq.topic',
routing_key='inTopic'
)
##########################################
##program utama FTP
##########################################
#freegeoip = "http://freegeoip.net/json"
#geo_r = requests.get(freegeoip)
#geo_json = geo_r.json()
#user_postition = [geo_json["latitude"], geo_json["longitude"]]
#lat_ip=user_postition[0]
#lon_ip=user_postition[1]
lat_ip=1
lon_ip=2
##############################################
#bgupload = subprocess.Popen(["sudo","python3", "/home/pi/TA/uploadfile.py"])
print('x1')
bgupload = subprocess.Popen(["python3", "uploadfile.py"])
##############################################
status = 'Manual'
keyboardcam = True
yaw = 0.0
pitch = 0.0
yawcam = 0.0
pitchcam = 0.0
prev_keyboardcam = True
##############################################
def bearing3599():
bear1 = bus.read_byte_data(address, 2)
bear2 = bus.read_byte_data(address, 3)
bear = (bear1 << 8) + bear2
bear = bear/10.0
return bear
#return 132
def inputmode(keyboardcam, yawcam, pitchcam, yaw, pitch):
import pygame
print('fungsi terpanggil')
pygame.init()
print('init')
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption('The amazing key presser!')
pygame.key.set_repeat(1, 200)
for e in pygame.event.get():
if e.type == pygame.KEYDOWN:
if (keyboardcam == True) :
if (e.key == pygame.K_k):
pitchcam -= 1
if pitchcam <= 0 :
pitchcam = 0
# print ("Pitch = %d"%pitch)
elif (e.key == pygame.K_i):
pitchcam += 1
if pitchcam >= 20 :
pitchcam = 20
# print ("Pitch = %d"%pitch)
elif (e.key == pygame.K_l):
yawcam += 1
yawcam = yawcam % 360
# print ("Yaw = %d"%yaw)
elif (e.key == pygame.K_j):
yawcam -= 1
yawcam = yawcam % 360
# print ("Yaw = %d"%yaw)
elif (e.key == pygame.K_s):
pitch -= 1.0
if pitch <= 0.0 :
pitch = 0.0
# print ("Pitch = %d"%pitch)
elif (e.key == pygame.K_w):
pitch += 1.0
if pitch >= 20.0 :
pitch = 20.0
# print ("Pitch = %d"%pitch)
elif (e.key == pygame.K_d):
yaw += 1.0
yaw = yaw % 360
# print ("Yaw = %d"%yaw)
elif (e.key == pygame.K_a):
yaw -= 1.0
yaw = yaw % 360
elif (e.key == pygame.K_q):
keyboardcam = True
elif (e.key == pygame.K_e):
keyboardcam = False
else :
keyboardcam = keyboardcam
yaw = yaw
pitch = pitch
yawcam = yawcam
pitchcam = pitchcam
else :
if (e.key == pygame.K_s):
pitch -= 1.0
if pitch <= 0.0 :
pitch = 0.0
# print ("Pitch = %d"%pitch)
elif (e.key == pygame.K_w):
pitch += 1.0
if pitch >= 20.0 :
pitch = 20.0
# print ("Pitch = %d"%pitch)
elif (e.key == pygame.K_d):
yaw += 1.0
yaw = yaw % 360
elif (e.key == pygame.K_a):
yaw -= 1.0
yaw = yaw % 360
elif (e.key == pygame.K_q):
keyboardcam = True
elif (e.key == pygame.K_e):
keyboardcam = False
else :
keyboardcam = keyboardcam
yaw = yaw
pitch = pitch
yawcam = yawcam
pitchcam = pitchcam
return (keyboardcam, yawcam, pitchcam, yaw, pitch)
def callback(ch, method, properties, body):
global status
global yaw
global pitch
global yawcam
global pitchcam
global keyboardcam
global prev_keyboardcam
global i
numAttempt = 0
with open('timeraspi.txt','a') as f:
f.write(str(datetime.datetime.now().time()))
f.write('\t')
print('x')
try:
x = body.decode('utf8')
jsonObject = json.loads(x)
c=jsonObject['yaw']
t=jsonObject['pitch']
#print(c, t)
print("dari %r ke %r"%(prev_keyboardcam, keyboardcam))
if (jsonObject['indBoxTrackedPlusOne'] == 0):
print('json masuk')
(keyboardcam,yawcam,pitchcam,yaw,pitch)= inputmode(keyboardcam,yawcam,pitchcam,yaw,pitch)
print('key presser aktif')
if ((keyboardcam == True)and(prev_keyboardcam == False)):
yawcam = c
pitchcam = t
if (keyboardcam == True):
#mode keyboard camera on
status = 'Manual keyboard camera on'
#i=1
print('c{0:.2f},t{1:.2f},g{2:.2f},y{3:.2f}'.format(yawcam,pitchcam,pitch,yaw))
ser.write(bytes('c{0:.2f},t{1:.2f},g{2:.2f},y{3:.2f}\n'.format(yawcam,pitchcam,pitch,yaw),'UTF-8'))
prev_keyboardcam = True
else :
#mode keyboard camera off
status = 'Manual keyboard camera off'
#print (status)
print('c{0:.2f},t{1:.2f},g{2:.2f},y{3:.2f}'.format(c,t,pitch,yaw))
#i = 0
ser.write(bytes('c{0:.2f},t{1:.2f},g{2:.2f},y{3:.2f}\n'.format(c,t,pitch,yaw),'UTF-8'))
prev_keyboardcam = False
else :
dataSudut1={
"cam":"%f"%c,
"tilt":"%f"%t,
"gun":"%f"%c,
"yaw_act":"%f"%c
}
jsonSudut1 = json.dumps(dataSudut1)
#print(1)
print('c{0:.2f},t{1:.2f},g{2:.2f},y{3:.2f}'.format(c,t,t,c))
# EDIT
# ser.write(bytes((jsonSudut1),'UTF-8'))
yaw = c
pitch = t
ser.write(bytes('c{0:.2f},t{1:.2f},g{1:.2f},y{0:.2f}\n'.format(jsonObject['yaw'],jsonObject['pitch']),'UTF-8'))
status = 'Tracking'
i = 0
yawdata={
"yaw":"%d"%yaw
}
jsonYaw = json.dumps(yawdata)
#print("yaw : {0} ; pitch : {1}".format(yaw,pitch))
channel.basic_publish(exchange='amq.topic',
routing_key='inTopic',
body= jsonYaw)
data={
"sensor1":"compass",
"head":"%d"%bearing3599(),
"sensor2":"GPS",
"latitude":"%s"%lat_ip,
"longitude":"%s"%lon_ip
}
# print("%d"%bearing3599())
jsonData = json.dumps(data)
channel.basic_publish(exchange='ex_raspitoholo',
routing_key='rk_raspitoholo',
body= jsonData)
#return (status)
print(status)
except :
numAttempt +=1
if(numAttempt >= 10):
bgupload.terminate()
print("error")
#sys.exit(1)
with open('timeraspi.txt','a') as f:
f.write(str(datetime.datetime.now().time()))
f.write('\n')
channel.basic_consume(callback,queue='holotoraspi',no_ack=True)
channel.start_consuming()
###############################################