Yes, that works - many thanks.
I see now there’s no inner loop in the balance.py
code. It could be my imagination, but it does seem to twitch about a lot, presumably because of latency as it checks the loop in the python code but I can fix that later.
Here is the working code for the VL6180X to work in the flask web app. First up, get the ST_VL6108X python code from here. Save the ST_VL6180X file in the same directory as the server_balboa.py
file. Connect the sensor to the I2C bus to access the sensor from the Raspberry Pi. The pinout diagram shows how to connect it up.
In the index.html
file located in the pi/server_balboa_resources/templates
folder, add the following HTML table:
<table>
<tr><th rowspan=2>VL6180X Time Of Flight</th><td>ToF Lux</td><td>ToF Distance</td></tr>
<tr><td id="tof_lux"></td><td id="tof_distance"></td></tr>
</table>
then, the pi/server_balboa.py
script should be modified to add the setup for the sensor and then render the values in the HTML table:
#!/usr/bin/env python3
# Copyright Pololu Corporation. For more information, see https://www.pololu.com/
from flask import Flask
from flask import render_template
from flask import redirect
from subprocess import call
app = Flask(__name__, static_folder='server_balboa_resources/static', template_folder='server_balboa_resources/templates')
app.debug = True
import sys
from a_star import AStar
a_star = AStar()
from ST_VL6180X import VL6180X
debug = False
tof_address = 0x29
tof_sensor = VL6180X(address=tof_address, debug=debug)
# apply pre calibrated offset
tof_sensor.set_range_offset(23)
tof_sensor.default_settings()
from balance import Balancer
balancer = Balancer()
tof_distance = 255
import json
led0_state = False
led1_state = False
led2_state = False
@app.route("/")
def hello():
return render_template("index.html")
@app.route("/status.json")
def status():
buttons = a_star.read_buttons()
analog = a_star.read_analog()
battery_millivolts = a_star.read_battery_millivolts()
encoders = a_star.read_encoders()
calibrated = balancer.calibrated
tof_distance = tof_sensor.get_distance()
tof_lux = round(tof_sensor.get_ambient_light(20), 2)
data = {
"buttons": buttons,
"battery_millivolts": battery_millivolts,
"analog": analog,
"encoders": encoders,
"calibrated": calibrated,
# adding in the sensor values here
"tof_distance": tof_distance,
"tof_lux": tof_lux
}
return json.dumps(data)
@app.route("/calibrate")
def calibrate():
balancer.setup()
balancer.start()
return ""
@app.route("/stand_up")
def stand_up():
balancer.stand_up()
return ""
@app.route("/drive/<left>,<right>")
def drive(left, right):
balancer.drive(int(left), int(right))
return ""
@app.route("/leds/<int:led0>,<int:led1>,<int:led2>")
def leds(led0, led1, led2):
a_star.leds(led0, led1, led2)
global led0_state
global led1_state
global led2_state
led0_state = led0
led1_state = led1
led2_state = led2
return ""
@app.route("/heartbeat/<int:state>")
def hearbeat(state):
if state == 0:
a_star.leds(led0_state, led1_state, led2_state)
else:
a_star.leds(not led0_state, not led1_state, not led2_state)
return ""
@app.route("/play_notes/<notes>")
def play_notes(notes):
a_star.play_notes(notes)
return ""
@app.route("/halt")
def halt():
call(["bash", "-c", "(sleep 2; sudo halt)&"])
return redirect("/shutting-down")
@app.route("/shutting-down")
def shutting_down():
return "Shutting down in 2 seconds! You can remove power when the green LED stops flashing."
if __name__ == "__main__":
app.run(host = "0.0.0.0")
finally the Javascript in pi/server_balboa_resources/static
needs to be modified to pass the collected JSON values back to the HTML.
function update_status(json) {
s = JSON.parse(json)
$("#button0").html(s["buttons"][0] ? '1' : '0')
$("#button1").html(s["buttons"][1] ? '1' : '0')
$("#button2").html(s["buttons"][2] ? '1' : '0')
$("#battery_millivolts").html(s["battery_millivolts"])
$("#analog0").html(s["analog"][0])
$("#analog1").html(s["analog"][1])
$("#analog2").html(s["analog"][2])
$("#analog3").html(s["analog"][3])
$("#analog4").html(s["analog"][4])
$("#analog5").html(s["analog"][5])
$("#tof_distance").html(s["tof_distance"])
$("#tof_lux").html(s["tof_lux"])
$("#encoders0").html(s["encoders"][0])
$("#encoders1").html(s["encoders"][1])
if(!calibrated && s["calibrated"])
{
calibrateDone()
calibrated = true
}
setTimeout(poll, 100)
}
Should look like this: