-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
67 lines (44 loc) · 1.55 KB
/
app.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
from flask import Flask, render_template # Flask
# ROS imports to setup the node
import rclpy
from std_msgs.msg import String
# Imports for threading operations
import sys
from threading import Thread
import atexit
new_image = None # Global variable to hold the image name
##### Setting up the ROS node:
def listener_callback(msg):
global new_image
node.get_logger().info('I heard: "%s"' % msg.data)
new_image = msg.data
# Initializing the node
rclpy.init(args=None)
node = rclpy.create_node('Show_image_python')
# start the ROS node called Show_image_python in a new thread
Thread(target=lambda:node).start() # Starting the Thread with a target in the node
# Subscriber to the /image_name topic
subscription = node.create_subscription(String,'/image_name', listener_callback, 10)
# create flask app
app = Flask(__name__)
# spin ROS once and refresh the node
def get_image():
rclpy.spin_once(node,timeout_sec=1.0)
return new_image
# main flask page gets the image and renders
@app.route('/')
def index():
new_image = get_image()
return render_template('index.html', new_image=new_image)
#defining function to run on shutdown
def close_running_threads():
rclpy.shutdown()
print("closed ROS")
sys.exit(0)
## Main funcion, only initiate the Flask app
def main(args=None):
atexit.register(close_running_threads) # call the function to close things properly when the server is down
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = -1
app.run(host='0.0.0.0', port=5000 ,debug=False)
if __name__ == '__main__':
main()