Permalink
Browse files

Visual feedback of wheel angle and velocity

  • Loading branch information...
Roger-random committed Feb 16, 2018
1 parent 8c70e5a commit 96ffe9e8717da51379d6206bc31c3aa568545f25
Showing with 95 additions and 7 deletions.
  1. +14 −2 SGVHAK_Rover/menu.py
  2. +76 −0 SGVHAK_Rover/static/chassis_config.js
  3. +5 −5 SGVHAK_Rover/templates/chassis_config.html
@@ -54,15 +54,15 @@ def drive():
@app.route('/drive_command', methods=['POST'])
def drive_command():
angle = float(request.form['angle'])*45/100
magnitude = float(request.form['magnitude'])*75
magnitude = float(request.form['magnitude'])

if angle >= 0:
radius = chassis.radius_for('front_right', angle)
else:
radius = chassis.radius_for('front_left', angle)

chassis.updateMotion(magnitude, radius)
chassis.normalizeVelocity(7500)
chassis.normalizeVelocity(100)

return json.jsonify({'Success':1})

@@ -81,6 +81,18 @@ def chassis_config():
angles = chassis.angles,
roboclaw_table = chassis.roboclaw_table(rclaw))

@app.route('/request_wheel_status', methods=['POST'])
def request_wheel_status():
wheelInfo = dict()
for wheel in chassis.wheels:
name = wheel['name']
velocity = chassis.velocity[name]
angle = chassis.angles[name]
wheelInfo[name] = dict()
wheelInfo[name]['velocity'] = velocity
wheelInfo[name]['angle'] = angle
return json.jsonify(wheelInfo)

@app.route('/chassis_test')
def chassis_test():
flash("chassis_test placeholder","success")
@@ -0,0 +1,76 @@
/*
MIT License
Copyright (c) 2018 Roger Cheng
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

// Upon document load, start to query the server for wheel angle and velocity.
$(document).ready(function() {
setTimeout(requestWheels, 1000);
})

// Begin the asyncyronous server request to obtain latest wheel updates.
// If successful, call updateWheels to process results.
var requestWheels = function() {
$.ajax({
type: "POST",
url: document.getElementById("request_wheel_status").value,
data: "all",
success: updateWheels
});
}

// Upon successful completion of POST started by requestWheels(), we receive
// a chunk of JSON that represents the wheel status. Iterate through each
// wheel and call updateWheelCanvas to draw the update in visual form.
// If all goes well, set a timer to repeat the process soon.
var updateWheels = function(data, textStatus, jqXHR) {
Object.keys(data).forEach(function(key,index) {
updateWheelCanvas(key, data[key].angle, data[key].velocity);
})
setTimeout(requestWheels, 200);
}

// Given a wheel name, its angle, and its velocity, find the <canvas> tag
// representing that wheel and draw a visual representation.
var updateWheelCanvas = function(name, angle, velocity) {
var canvas = document.getElementById("canvas_"+name);
var ctx = canvas.getContext("2d");
var wheelW = canvas.width*0.2;
var wheelH = canvas.height*0.8;

ctx.clearRect(0,0,canvas.width,canvas.height);

ctx.save();
ctx.translate(canvas.width/2, canvas.height/2);
ctx.rotate(angle * Math.PI/180);
ctx.strokeRect(-wheelW/2, -wheelH/2, wheelW, wheelH);
ctx.fillStyle = "#008000";
if (velocity > 0) {
var drawH = (wheelH/2)*(velocity/100);
ctx.fillRect(-wheelW/2, -drawH, wheelW, drawH);
} else if (velocity < 0) {
var drawH = (wheelH/2)*(-velocity/100);
ctx.fillRect(-wheelW/2, 0, wheelW, drawH);
}
ctx.restore();

}
@@ -23,9 +23,8 @@
-->
{% extends "layout.html" %}
{% block body %}
<script>
setTimeout(() => {location.reload(true);}, 500); // Reload page every half second
</script>
<script type="text/javascript" src="{{url_for('static', filename='chassis_config.js')}}"></script>
<input type="hidden" id="request_wheel_status" value="{{url_for('request_wheel_status')}}" />
<div class="container">
<h4 class="center_header">Chassis Configuraton</h4>
<table>
@@ -35,6 +34,9 @@ <h4 class="center_header">Chassis Configuraton</h4>
<td class="center-align">
{% for wheel in wheelTableColumn %}
<div class="container blue lighten-4">
<canvas id="canvas_{{wheel['name']}}" width="100" height="100">
</canvas>
<br/>
<b>{{wheel['name']}}</b>
<br/>
({{wheel['x']}},{{wheel['y']}})
@@ -55,8 +57,6 @@ <h4 class="center_header">Chassis Configuraton</h4>
{% else %}
Steering: TBD
{% endif %}
<br/>
Velocity {{ velocity[wheel['name']]|int }} @ {{ angles[wheel['name']]|int }}&deg
</div>
{% endfor %}
</td>

0 comments on commit 96ffe9e

Please sign in to comment.