Gauge Exercise
Exercise: Using Round Gauge
Gauge Configuration
- Begin by creating 3 Tags. The tag names are "Gas Peddle", "Gauge", and "Button". The data types will consist of "Gas Peddle" will be a BOOL, "Gauge" will be an INT, and "Button" will be a REAL.
- Create a new script in the Script Editor called "Gauge". Set the running type to periodic.
- Paste the following code into the "Gauge" script.
var currentSpeed = 0;
var throttleEngaged = false;
var maxSpeed = 1000;
var minSpeed = 0;
var speedStep = 10; // Adjust speed increment based on throttle engagement
// Main loop to control the speed based on the throttle
while (true) {
// Check if the throttle is engaged (assuming the "Throttle" tag reflects engagement)
if (tag.read("Gas Peddle") == 1) {
throttleEngaged = true; // Throttle engaged
} else {
throttleEngaged = false; // Throttle disengaged
}
// If throttle is engaged, increase the speed
if (throttleEngaged) {
currentSpeed += speedStep; // Increase speed
if (currentSpeed > maxSpeed) {
currentSpeed = maxSpeed; // Cap the speed at the maximum value (240)
}
} else {
currentSpeed -= speedStep; // Decrease speed when throttle is disengaged
if (currentSpeed < minSpeed) {
currentSpeed = minSpeed; // Ensure speed doesn't drop below 0
}
}
// Write the current speed to the "Speed" tag (update the speedometer)
tag.write("Gauge", currentSpeed);
// Small delay before the next loop iteration
thread.msleep(100); // Adjust for smoother or faster speed changes
}
- Create another script in the Script Editor called "Speed Gauge". Set the Running type to periodic.
- Paste the following code into the "Speed Gauge" script.
thread.msleep(5100)
while (1){
var control_speed_tag = tag.read("Gauge")
var control = control_speed_tag * 360/(15 * 100 * 12)
thread.msleep(10)
}
- Create another script in the Script Editor called "Speed Gauge 1". Set the Running type to periodic.
- Paste the following code into the "Speed Gauge 1" script.
while (1) {
if (tag.read("Gas Peddle")){
tag.write("Gauge", tag.read("Gauge") + 1);
}
thread.msleep(50)
}
- This code will be used for the objects in the next section. If the arrow is pressed and held down it will move the car to the right, and if the button is released it'll move the car to the left while also moving the Gauge.
- Create 5 objects on the screen; two vector resource library arrows, a momentary button, a car from the resource library, and lastly a round Gauge.
9. Follow the images below and make sure tags we created earlier are on the correct object and that action buttons are set correctly.
10. Now that everything is tagged and "Action buttons" are set correctly move the arrow which has the "button" tag on it ontop of the second arrow we created and change the order of it to bring it to the front.
11. Take the "Momentary button" and do the same thing placing it ontop of the arrows, but change the opacity of it to 0.01 once ontop of the Arrows. Also change the order and bring it to the front as well.
12. Select the "Round Gauge" and change the max value to 950, or whatever amount you want to display on the Gauge.
Gauge Runtime
- Click Tools > Launch Simulator to launch the Canvas Simulator.
- Click and hold the arrow to the right and watch the car move to the right and the gauge increase. Release the arrow and watch the car move to the left and the gauge decrease.