Table of contents
Line following with Sensor Array and PID control algorithm 7
RoboSensei javascript functions(Preliminary) 14
Advanced: Using JavaScript Timers 17
Move Forward | |
Blocks | javaScript |
//Set motor power move(50, 50); //continue for 3 seconds await gofor(3); // Reset power to zero stop(); | |
Left and Right motors have equal, and positive power | |
Move Backward | |
Blocks | javaScript |
//Set motor power move(-50, -50); //continue for 3 seconds await gofor(3); // Reset power to zero stop(); | |
Left and Right motors have equal, and negative power | |
Left Turn | |
Blocks | javaScript |
//Set motor power move(-50, 50); //continue for 3 seconds await gofor(3); // Reset power to zero stop(); | |
Differential power (left power - right power) is negative | |
Right Turn | |
Blocks | javaScript |
//Set motor power move(50, -50); //continue for 3 seconds await gofor(3); // Reset power to zero stop(); | |
Differential (left - right) power is positive | |
Following a dark line | |
Blocks | javaScript |
while (true) { // Must read inputs in the loop for latest status // readInputs() also needs await keyword await readInputs(); if( irLeft() < 50) { move(-50, 50); } else if ( irRight() < 50) { move(50, -50); } else { move(50, 50); } } | |
Adjust the power to move faster. Adjust the differential power to get sharper turns | |
Navigating the T-junction | |
Blocks | javaScript |
while (true) { // Must read inputs in the loop to get the latest status // readInputs() also needs await keyword await readInputs(); // If BOTH sensors on the line, it is a T-junction if ( ( irLeft() < 50 ) && ( irRight() < 50 ) ) { TLeft(); }
// Check if left sensor is on or near the line else if ( irLeft() < 50 ) { move(-40, 70); } // Check if right sensor is near the line else if (irRight() < 50) { move(70, -40); }
// Otherwise keep going straight else { move(100, 100); } } // Function to handle T-junction followied // by line seek to the left async function TLeft() { move(20, 20); await gofor(.2);
while( irRight() < 50 ) { move(-40, 40); // We must get the latest input state // before the next loop iteration await readInputs(); } } | |
Adjust the power to move faster and get sharper turns. | |
Line following with Sensor Array and PID control algorithm | |
Blocks | javaScript |
// PID Control example with RoboSensei IR sensor array var kp = 5; var ki = 5; var kd = 3; var P = 0; var I = 0; var D = 0; var prevError = 0; var correction = 0; var lp = 70; // left power var rp = 70; // right power var error = 0; while (true) { // Must read inputs in the loop to get the latest status // readInputs() also needs await keyword await readInputs();
error = irArrayErrorGet();
if (error == 255) { tprint('oops! out of line!') }
P = error; I = I + error; D = prevError - error; prevError = error;
correction = kp * P + ki * I + kd * D; move(lp - correction, rp + correction); } | |
Google / checkout wikipedia to learn more about PID control algorithms. Your air conditioner will be using this algorithm. Experiment with tuning kp, ki, and kd to improve the line tracking performance. Start with slow speed, slowly increase the speed and tune the control loop. | |
Adjustable Screen Split
You can now adjust the width of the coding area and the robot simulation area as you need. Simply move your cursor over the thick purple line. When you see the drag cursor simply click hold your left mouse button and adjust the screen ratio.
Access to Help File without leaving the coding area
Help content is not shown on the right side of the split screen. You can view the help content and build your code while viewing the help content.
New: IR Sensor Array
A new IR Sensor with 8 sensors is now available for SenseiBot ONLY. You can select this from the Options menu.
Here is the new JS API for the IR sensor array.
API | Use notes | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ir8Array() | var sensorArray = ir8Array(); Returns an array of 8 integers whose value is between 0 (black color) and 255(white color) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ir8Byte() | var sensorByte = ir8Byte(); Returns an integer, whose least significant byte represents a 1/0 status of each sensor based on threshold value of 100.
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
irArrayErrorGet() | var error = irArrayErrorGet() This function will return an error value based on the detected sensor values. This is a convenience function. You may want to write your own function. This function return the following values: In the following table ‘x’ (don’t care) means value does not impact the returned value. This could be a 0 or 1.
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
July Challenges:
As usual new tracks are added for July. Line following UP continues from June. No one has completed this track yet.
Bug Fixes:
Bump Sensor: Under some circumstances, bump sensor value is not updated. This is now fixed.
Ton of new things again!
Currently this is preview, available at: https://rrca-senseitest.azurewebsites.net/Account/Login
Changes in this release
New: Leaderboard
Checkout the cool new leader board right on the login page. Currently this board shows current month leaders. You can scroll on each tile to see more scores.
New: Modified Login workflow
One need to click on the LOGIN menu link to get the login popup. This is done to provide a clean view for leaderboard. There are some annoyances with the new login popup. For example, if you enter wrong credentials, it will simply put you back on to the leaderboard. You need to click on the LOGIN menu again to try.
New: New robot option & tires
Notice that it also comes with cool new tires. These wheels are slightly bigger! Of course you can use them with other robots. We hope that you like the new robot.
Fixed: Saving and retrieving JavaScript code for missions
You can now save missions JS code and have RoboSensei reload when you select the mission. JS is a lot more fun and we hope you try it out.
Changes in this release
New: Help view
A new item in the sidebar for accessing documentation is added. Selecting this will take you to the Help view where you will find release notes and other documents.
New: JavaScript APIs
Several new JS APIs are added. You can find the details in the JS API table in this document.
Modified: Saving Missions & Activities Code
Saving Missions and Activities code is now unified with Save File menu. To save your code when a mission is selected, simply select the Save File icon and RoboSensei will provide you with an option to save your code under the currently selected mission. No file name is required.
Note: There is no change to the way you save files that are not related to Missions and Activities.
New: “Code Running” animation
When the program is running, the Play button is animated to provide a visual cue.
API | Use notes |
await readInputs() | usage: await readInputs(); This function needs to be called before using sensors in the code. In case of loops, where sensor is used; call this at the beginning, inside the loop. |
move(powerLeft, powerRight) | usage : move(powerLeft, powerRight); powerLeft: left motor power 0 - 100 powerRight: right motor power 0 - 100 |
await movefor(powerLeft, powerRight, timeSeconds) | usage: await movefor(powerLeft, powerRight, timeSeconds); powerLeft: left motor power 0 - 100 powerRight: right motor power 0 - 100 timeSeconds: time in seconds (decimal number) Example: await movefor(20, 20, 2); |
await delay(timeSeconds) await gofor(timeSeconds) | Usage: await delay(timeSeconds); Usage: await gofor(timeSeconds); timeSeconds: time in seconds (decimal number) |
stop() | Usage: stop() Stops the robot by applying zero power to left and right motors. Same as move(0,0); |
irLeft() | Usage: irLeft(); Returns the value of the LEFT IR sensor |
irRight() | Usage: irRight() Returns the value of the RIGHT IR sensor |
distance() | Usage: distance(); Returns value of distance sensor in mm. |
bumperLeft() bumperRight() Xplorer ONLY | Usage: bumperLeft(); bumperRight() Returns true : bumper is hit; false: bumper not hit |
ledon() ledoff() Xplorer ONLY | Usage: ledon(ledId); ---> ledon(1); ledoff(ledId; ----> lefoff(1); Red: 0, Yellow: 1, Green: 2, Left: 3, Right: 5 |
tprint(text) | Usage: tprint(‘text’); Prints text to the debug console located at the bottom of the the javascript window You can also print sensor values. Example: tprint( ‘Distance: ’ + distance() ); will print distance sensor value Caution: tprint command severely impacts the robot performance, hence should not be used in loops where high performance sensor reading and code execution is required for example in a line follower loop. Generally only use this for debugging purposes. |
rgbleft() rgbright() mBot ONLY | Usage: rgbleft(rValue, gValue, bValue); rgbright(rValue, gValue, bValue); Controls the RGB LEDs on mBOT. rValue, gValue, bValue: 0 to 255 Sample code to blink left RGB LED: while(true) { rgbleft(255, 0, 0); await delay(1); rgbleft(0,0,0); await delay(1); } |
JS Window must be visible to execute JS code
Terminal output:
/* Start typing JS code */
tprint ('Crossing home line');
// must use await where timing is involved
await movefor(30, 30, 2);
await readInputs();
tprint ('irLeft: ' + irLeft() + ' ' + 'irRight: ' + irRight());
tprint ('Off to chasing the line');
while (true)
{
// Must read inputs in the loop to get the latest status
// readInputs() also needs await keyword
await readInputs();
if(irLeft() < 50)
{
move(-40, 70);
}
else if (irRight() < 50)
{
move(70, -40);
}
else
{
move(100, 100);
}
}
// ASYNCHRONOUS PROGRAMMING WITH JS TIMERS
// To see the following example, use xplorer robot to see red light blink with 500 ms interval
var myVar = setInterval(myTimer, 500)
// We need to make sure the program does not exit
while (true)
{
await delay(3);
}
// Function to run on the timer interval
var isOn = false;
function myTimer()
{
if(isOn)
{
ledoff(0);
}
else
{
ledon(0);
}
isOn = !isOn;
}