Added basic lidar functionality
This commit is contained in:
parent
c0d3bd7f60
commit
7ad3b795cc
2 changed files with 18 additions and 4 deletions
15
main.py
15
main.py
|
|
@ -1,6 +1,8 @@
|
||||||
# These two modules allow us to run a web server.
|
# These two modules allow us to run a web server.
|
||||||
from flask import Flask, render_template
|
from flask import Flask, render_template
|
||||||
from flask_socketio import SocketIO
|
from flask_socketio import SocketIO
|
||||||
|
from tfluna import TFLuna
|
||||||
|
|
||||||
# This module lets us pick random numbers, you can remove it later.
|
# This module lets us pick random numbers, you can remove it later.
|
||||||
import random
|
import random
|
||||||
|
|
||||||
|
|
@ -8,6 +10,14 @@ import random
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
socketio = SocketIO(app)
|
socketio = SocketIO(app)
|
||||||
|
|
||||||
|
tfluna = TFLuna()
|
||||||
|
tfluna.open()
|
||||||
|
tfluna.set_samp_rate(10)
|
||||||
|
def printLidar():
|
||||||
|
distance, strength, temperature = tfluna.read()
|
||||||
|
lidarValues = {distance, strength, temperature}
|
||||||
|
return str(lidarValues)
|
||||||
|
|
||||||
# When someone requests the root page from our web server, we return 'index.html'.
|
# When someone requests the root page from our web server, we return 'index.html'.
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
def index():
|
def index():
|
||||||
|
|
@ -17,7 +27,7 @@ def index():
|
||||||
def background_thread():
|
def background_thread():
|
||||||
while True:
|
while True:
|
||||||
# We sleep here for a single second, but this can be increased or decreased depending on how quickly you want data to be pushed to clients.
|
# We sleep here for a single second, but this can be increased or decreased depending on how quickly you want data to be pushed to clients.
|
||||||
socketio.sleep(1)
|
socketio.sleep(0.1)
|
||||||
# Then, we emit an event called "update_data" - but this can actually be whatever we want - with the data being a dictionary
|
# Then, we emit an event called "update_data" - but this can actually be whatever we want - with the data being a dictionary
|
||||||
# where 'randomNumber' is set to a random number we choose here. You should replace the data being sent back with your sensor data
|
# where 'randomNumber' is set to a random number we choose here. You should replace the data being sent back with your sensor data
|
||||||
# that you fetch from things connected to your Pi.
|
# that you fetch from things connected to your Pi.
|
||||||
|
|
@ -27,6 +37,7 @@ def background_thread():
|
||||||
'randomNumber': random.randint(1, 100),
|
'randomNumber': random.randint(1, 100),
|
||||||
# you can add more here! for instance, something along the lines of:
|
# you can add more here! for instance, something along the lines of:
|
||||||
# 'mySensor': mysensor.get_sensor_data(),
|
# 'mySensor': mysensor.get_sensor_data(),
|
||||||
|
'lidar': printLidar()
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
# To add a your first new sensor, try giving https://docs.aerospacejam.org/getting-started/first-sensor a read!
|
# To add a your first new sensor, try giving https://docs.aerospacejam.org/getting-started/first-sensor a read!
|
||||||
|
|
@ -40,7 +51,7 @@ def handle_connect():
|
||||||
# This function is called
|
# This function is called
|
||||||
def main():
|
def main():
|
||||||
# These specific arguments are required to make sure the webserver is hosted in a consistent spot, so don't change them unless you know what you're doing.
|
# These specific arguments are required to make sure the webserver is hosted in a consistent spot, so don't change them unless you know what you're doing.
|
||||||
socketio.run(app, host='0.0.0.0', port=80, allow_unsafe_werkzeug=True)
|
socketio.run(app, host='0.0.0.0', port=8085, allow_unsafe_werkzeug=True)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@
|
||||||
<h1>Aerospace Jam Example</h1>
|
<h1>Aerospace Jam Example</h1>
|
||||||
<!-- Notice how the span that contains the placeholder value has an ID. We reference this ID later in the script, and it lets us pick this element in specific. -->
|
<!-- Notice how the span that contains the placeholder value has an ID. We reference this ID later in the script, and it lets us pick this element in specific. -->
|
||||||
<p><b>Random number chosen by the Pi: </b> <span id="randomNumber">Loading...</span></p>
|
<p><b>Random number chosen by the Pi: </b> <span id="randomNumber">Loading...</span></p>
|
||||||
|
<p><b>Lidar Values: </b> <span id="lidar">Loading...</span></p>
|
||||||
<script>
|
<script>
|
||||||
// This function wrapper (addEventListener) makes the inner contents run only when the DOM (Document Object Model) content is loaded.
|
// This function wrapper (addEventListener) makes the inner contents run only when the DOM (Document Object Model) content is loaded.
|
||||||
// In simpler terms, this runs once the page has finished loading.
|
// In simpler terms, this runs once the page has finished loading.
|
||||||
|
|
@ -20,8 +21,10 @@
|
||||||
socket.on('update_data', function(msg) {
|
socket.on('update_data', function(msg) {
|
||||||
// Now, we get the span that we defined before by its ID...
|
// Now, we get the span that we defined before by its ID...
|
||||||
var randomNumberSpan = document.getElementById('randomNumber');
|
var randomNumberSpan = document.getElementById('randomNumber');
|
||||||
// And we set its content to the random number we just got from the server.
|
var lidarDistanceSpan = document.getElementById('lidar');
|
||||||
|
// And we set its content to the random number we just got from the server.
|
||||||
randomNumberSpan.textContent = msg.randomNumber;
|
randomNumberSpan.textContent = msg.randomNumber;
|
||||||
|
lidarDistanceSpan.textContent = msg.lidar;
|
||||||
// For new sensors, we can really easily repeat this! For instance, if you added a piece to the data on the server side called "mySensor", as in the other example, you could do:
|
// For new sensors, we can really easily repeat this! For instance, if you added a piece to the data on the server side called "mySensor", as in the other example, you could do:
|
||||||
// var mySensorSpan = document.getElementById('mySensor');
|
// var mySensorSpan = document.getElementById('mySensor');
|
||||||
// mySensorSpan.textContent = msg.mySensor;
|
// mySensorSpan.textContent = msg.mySensor;
|
||||||
|
|
@ -29,4 +32,4 @@
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue