-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.py
More file actions
154 lines (103 loc) · 3.44 KB
/
app.py
File metadata and controls
154 lines (103 loc) · 3.44 KB
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
#imports
from flask import render_template
from flask import request
import paho.mqtt.publish as publish
import paho.mqtt.client as mqtt
from flask import Flask
app = Flask(__name__)
data = ''
plan = ''
## load pages and link them with local host
@app.route('/')
def result1():
return render_template('index.html')
@app.route('/remote')
def result2():
return render_template('remote.html')
@app.route('/automatic')
def result3():
return render_template('automatic.html')
## function that publish comannds of remote control
@app.route('/cmd/<val>')
def cmdF(val):
try:
publish.single('cic-cmd', val, hostname='broker.hivemq.com')
except:
print ('error')
return render_template('remote.html')
## get function to get sensor readings :
@app.route('/getReadings', methods=['GET'])
def update():
global data
data = "sensors = 1,2,3,4,5,6,7,8,9,10"
return data
## Get Request to get plan:
#convert ros coordinates to web coordinates to visualize plan on the ui
outMinX = 0
outMaxX = 945
outMinY = 0
outMaxY = 681
inMinX = -8.36
inMaxX = 18
inMinY = 8.18
inMaxY = -11.1
@app.route('/getPlan', methods=['GET'])
def updateplan():
global plan
if (plan == ''):
return ''
print('return')
out = ''
temp = plan.split('_')
for point in temp:
pt = point.split(',')
if (len(pt) == 2):
x = (pt [0])
y = (pt [1])
## Equation
outx = ((float(x)- inMinX) / (inMaxX - inMinX)) * (outMaxX - outMinX) + outMinX
outy = ((float(y) - inMinY) / (inMaxY - inMinY)) * (outMaxY - outMinY) + outMinY
out += str(outx)+','+str(outy)+'_'
return out[0:-1]
##Get goal position
@app.route('/getpose/<val>')
def updatepose(val):
print(val.split(','))
publish.single('cic_pose', val, hostname='broker.hivemq.com')
return 'ok'
## MQTT:
##decoding messages from string to the type what we need (int ,float,..) then print in terminal
def onFB(_, __, msg):
global data
data = msg.payload.decode('utf-8')
print(data)
def onPlan(_, __, msg):
global plan
plan = msg.payload.decode('utf-8')
print(plan)
#check if disconnect ---> connect again
def on_disconnect(client, userdata, rc):
print("disconnecting reason " +str(rc))
client.connect('broker.hivemq.com', 1883, 60)
#check connectivity and callback the functions and create MQTT subscriber on any topic called cic/...
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.message_callback_add('cic/data', onFB)
client.message_callback_add('cic/plan', onPlan)
client.subscribe('cic/#')
#(in_message) To access the message from you main script requires
def on_message(client, userdata, msg):
print (msg.topic)
# Create an MQTT client and attach our routines to it.
if __name__ == '__main__':
print ('Start')
client = mqtt.Client()
print ('Created Client')
client.on_connect = on_connect
client.on_disconnect = on_disconnect
print ('Added OnConnect')
client.on_message = on_message
client.connect('broker.hivemq.com', 1883, 60)
print ('Connected')
client.loop_start()
app.run(host='0.0.0.0', port=5000)