Asked
— Edited
@Athena, can you explain the functions safeInt and clampInt? Why are they used and how can the values be changed? Thanks - Jack.
Context: This looks like an ARC JavaScript script (likely running in a Robot Skill) that initializes leg/waypoint variables, clamps them to expected ranges, and stores them as ARC globals via setVar for UART_v5 (so another robot skill or device can consume them). I'm posting the full script below for reference.
print("LEG_v5 started");
function safeInt(v, def) {
var n = parseInt(v);
if (isNaN(n)) return def;
return n;
}
function clampInt(v, min, max) {
if (v < min) return min;
if (v > max) return max;
return v;
}
// Example leg values
var pathTotalDistance = safeInt(50000, 0);
var combinedStepsDesired = safeInt(50000, 0);
var oppositeDistance = safeInt(12000, 0);
var bearingAlignment = safeInt(0, 0); // 0 = DA, 1 = OA
var desiredBearing100 = safeInt(8300, 9000);
var actualBearing100 = safeInt(8100, 9000);
var trackDir = safeInt(0, 0); // 0 = FD, 1 = RD
// Clamp to expected ranges
pathTotalDistance = clampInt(pathTotalDistance, 0, 120000);
combinedStepsDesired = clampInt(combinedStepsDesired, 0, 84000);
oppositeDistance = clampInt(oppositeDistance, 0, 24000);
bearingAlignment = clampInt(bearingAlignment, 0, 1);
trackDir = clampInt(trackDir, 0, 1);
// Store globals for UART_v5
setVar("$pathTotalDistance", pathTotalDistance);
setVar("$combinedStepsDesired", combinedStepsDesired);
setVar("$oppositeDistance", oppositeDistance);
setVar("$bearingAlignment", bearingAlignment);
setVar("$desiredBearing100", desiredBearing100);
setVar("$actualBearing100", actualBearing100);
setVar("$trackDir", trackDir);
// Control flags
setVar("$waypointSent", 0);
setVar("$sendWaypointNow", 1);
setVar("$sendStopNow", 0);
setVar("$sendActualBearingNow", 0);
setVar("$obsScan", 0);
setVar("$run", 0);
setVar("$legComplete", 0);
setVar("$totalStepsTaken", 0);
print("LEG_v5 loaded:");
print("pathTotalDistance = " + pathTotalDistance);
print("combinedStepsDesired = " + combinedStepsDesired);
print("oppositeDistance = " + oppositeDistance);
print("bearingAlignment = " + bearingAlignment);
print("desiredBearing100 = " + desiredBearing100);
print("actualBearing100 = " + actualBearing100);
print("trackDir = " + trackDir);
Related Hardware (view all EZB hardware)
EZ-B V4
by EZ-Robot
EZ-B v4/2 robot controller: dual Cortex ARM, Wi-Fi, audio/video, 24 servo/digital ports, I2C/UART, camera and Synthiam ARC control for custom robots
Wi-Fi / USB
Servos
24
Camera
Audio
UART
3
I2C
ADC
8
Digital
24
Related Robot Skills (view all robot skills)
Script
by Synthiam
Multi-language ARC Script: build, run and debug Blockly, JavaScript, EZ-Script or Python with Intellisense, run/save/load and Roboscratch support
Task Script
by Synthiam Inc.
Split complex robot tasks into sequential scripts, synchronizing asynchronous skills via NEXT/CANCEL commands for staged automation.
Requires ARC
v8

Yes - in that ARC JavaScript script,
safeInt()andclampInt()are there to make sure the values stored in ARC globals are valid, predictable integers before another skill/device uses them.What
safeInt()doesPurpose
It converts a value into an integer safely.
vcan be parsed as a number, it returns that integer.vis not a number (NaN), it returns the fallback valuedef.Why it’s used
It prevents the script from breaking or storing bad data if a value comes in as:
Example
What
clampInt()doesPurpose
It forces a value to stay within a safe range.
minmaxWhy it’s used
This protects your robot logic from impossible or dangerous values.
For example, if a distance or bearing value goes outside the range your UART/device expects, clamping keeps the data sane.
Example
How they work together in your script
Your script does this:
safeInt()converts the raw values into integers.clampInt()restricts those integers to expected ranges.setVar()stores them as ARC global variables for other skills to read.So this:
means:
5000000and120000Can the values be changed?
Yes - in two ways:
1. Change the default/fallback values in
safeInt()Example:
If
8300were invalid, it would use9000.So you can change the second parameter:
2. Change the allowed range in
clampInt()Example:
That means only
0or1are allowed.If you wanted a wider range, you could change it:
Why these values matter in ARC
Because you’re storing values with:
those globals may be used by:
Using
safeInt()andclampInt()helps keep those shared values consistent and prevents bad data from spreading through the project.Small note about your script
This line is likely a typo in the sample you pasted:
It should be:
Without the
>, the function won’t work correctly.In short
safeInt()= "convert to integer, or use a default if invalid"clampInt()= "keep the number inside an allowed min/max range"safeInt()clampInt()If you want, I can also rewrite your script in a cleaner ARC JavaScript style and show how these globals would be read by the UART_v5 skill or another Script skill.
I am a customer support GPT-powered AI and still learning. If you have doubts about my response, please use the support documentation to verify my answer.
@Athena can you please rewrite LEG_v5 in a cleaner ARC JavaScript style and show how these globals would be read by the UART_v5 skill? I wish to have LEG_v5 begin with $actualBearing and $desiredBearing equal to Servo.getPosition(17). Thanks. Jack
Absolutely - below is a cleaner ARC JavaScript version of LEG_v5 that:
clampInt()typo$actualBearingand$desiredBearingfromServo.getPosition(17)Cleaner
LEG_v5ARC JavaScriptHow
UART_v5would read these globalsAny other Script skill can read the globals with
getVar().Example: read them in another ARC JavaScript skill
Example: UART_v5 sending them out
If
UART_v5is another Script skill, it can pull the values and transmit them however you need. For example:If you are using a COM port or UART skill, I can format that as actual transmit code too.
Important note about
Servo.getPosition(17)In ARC JavaScript, servo ports are typically constants like:
d0tod23So if your servo is on digital port 17, the call should usually be:
If you write
Servo.getPosition(17), ARC may interpret that differently than a servo port constant. If you want, I can help you make this completely consistent with your current project.Suggested improvement
If you want cleaner naming, I’d also recommend this convention:
$actualBearingcurrent measured bearing$desiredBearingtarget bearing$pathTotalDistancetotal planned leg distance$combinedStepsDesiredexpected total stepsThat makes it easier for
UART_v5or any later skill to read the intent quickly.Quick question
Do you want me to also rewrite UART_v5 so it reads these globals and sends a compact packet format like:
If you paste your current
UART_v5script, I can convert that too.I am a customer support GPT-powered AI and still learning. If you have doubts about my response, please use the support documentation to verify my answer.