Bing Speech Recognition icon Bing Speech Recognition Accurate Bing cloud speech-to-text for ARC: wake-word, programmable control, $BingSpeech output, Windows language support, headset compatible Try it →
Asked — Edited

**Safeint And Clampint Explained**

@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.

ARC Pro

Upgrade to ARC Pro

ARC Pro is more than a tool; it's a creative playground for robot enthusiasts, where you can turn your wildest ideas into reality.

Author Avatar
PRO
Canada
Twitter YouTube GitHub
#1  

Yes - in that ARC JavaScript script, safeInt() and clampInt() are there to make sure the values stored in ARC globals are valid, predictable integers before another skill/device uses them.

What safeInt() does

function safeInt(v, def) {
  var n = parseInt(v);
  if (isNaN(n)) return def;
  return n;
}

Purpose

It converts a value into an integer safely.

  • If v can be parsed as a number, it returns that integer.
  • If v is not a number (NaN), it returns the fallback value def.

Why it’s used

It prevents the script from breaking or storing bad data if a value comes in as:

  • empty
  • text
  • undefined
  • malformed

Example

safeInt("123", 0)   // returns 123
safeInt("abc", 0)   // returns 0
safeInt("", 99)     // returns 99

What clampInt() does

function clampInt(v, min, max) {
  if (v < min) return min;
  if (v > max) return max;
  return v;
}

Purpose

It forces a value to stay within a safe range.

  • If the value is too low, it becomes min
  • If the value is too high, it becomes max
  • If it is already in range, it stays unchanged

Why 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

clampInt(50, 0, 100)    // returns 50
clampInt(-10, 0, 100)   // returns 0
clampInt(150, 0, 100)   // returns 100

How they work together in your script

Your script does this:

  1. safeInt() converts the raw values into integers.
  2. clampInt() restricts those integers to expected ranges.
  3. setVar() stores them as ARC global variables for other skills to read.

So this:

var pathTotalDistance = safeInt(50000, 0);
pathTotalDistance = clampInt(pathTotalDistance, 0, 120000);

means:

  • use 50000
  • if it’s invalid, default to 0
  • then make sure it stays between 0 and 120000

Can the values be changed?

Yes - in two ways:

1. Change the default/fallback values in safeInt()

Example:

var desiredBearing100 = safeInt(8300, 9000);

If 8300 were invalid, it would use 9000.

So you can change the second parameter:

safeInt(value, defaultValue)

2. Change the allowed range in clampInt()

Example:

bearingAlignment = clampInt(bearingAlignment, 0, 1);

That means only 0 or 1 are allowed.

If you wanted a wider range, you could change it:

bearingAlignment = clampInt(bearingAlignment, 0, 2);

Why these values matter in ARC

Because you’re storing values with:

setVar("$pathTotalDistance", pathTotalDistance);

those globals may be used by:

  • another Script skill
  • a UART control
  • a navigation routine
  • a custom robot skill

Using safeInt() and clampInt() 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:

if (v max) return max;

It should be:

if (v > max) return max;

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"
  • They’re used to protect your robot logic and ensure clean ARC global values
  • You change values by editing:
    • the default value in safeInt()
    • the min/max limits in 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.
#2  

@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

Author Avatar
PRO
Canada
Twitter YouTube GitHub
#3  

Absolutely - below is a cleaner ARC JavaScript version of LEG_v5 that:

  • uses clearer helper functions
  • fixes the clampInt() typo
  • initializes $actualBearing and $desiredBearing from Servo.getPosition(17)
  • stores the globals in a way that another Script skill or your UART_v5 skill can read them

Note: I’m assuming servo port 17 means d17 in ARC. If you meant a virtual servo port or another skill-controlled servo, tell me and I’ll adjust it.


Cleaner LEG_v5 ARC JavaScript

print("LEG_v5 started");

// ---------- Helpers ----------
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;
}

// ---------- Initial values ----------
// Read current servo position on port d17
var bearingPos = safeInt(Servo.getPosition(d17), 0);

// Use the servo position as both starting values
var actualBearing = bearingPos;
var desiredBearing = bearingPos;

// 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 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 / other skills
setVar("$pathTotalDistance", pathTotalDistance);
setVar("$combinedStepsDesired", combinedStepsDesired);
setVar("$oppositeDistance", oppositeDistance);
setVar("$bearingAlignment", bearingAlignment);
setVar("$desiredBearing", desiredBearing);
setVar("$actualBearing", actualBearing);
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("desiredBearing = " + desiredBearing);
print("actualBearing = " + actualBearing);
print("trackDir = " + trackDir);

How UART_v5 would read these globals

Any other Script skill can read the globals with getVar().

Example: read them in another ARC JavaScript skill

var actualBearing = getVar("$actualBearing", 0);
var desiredBearing = getVar("$desiredBearing", 0);
var pathTotalDistance = getVar("$pathTotalDistance", 0);
var combinedStepsDesired = getVar("$combinedStepsDesired", 0);

print("actualBearing = " + actualBearing);
print("desiredBearing = " + desiredBearing);
print("pathTotalDistance = " + pathTotalDistance);
print("combinedStepsDesired = " + combinedStepsDesired);

Example: UART_v5 sending them out

If UART_v5 is another Script skill, it can pull the values and transmit them however you need. For example:

var actualBearing = getVar("$actualBearing", 0);
var desiredBearing = getVar("$desiredBearing", 0);
var pathTotalDistance = getVar("$pathTotalDistance", 0);
var combinedStepsDesired = getVar("$combinedStepsDesired", 0);
var oppositeDistance = getVar("$oppositeDistance", 0);

// Example payload
var payload = 
  "AB=" + actualBearing +
  ",DB=" + desiredBearing +
  ",TD=" + pathTotalDistance +
  ",SD=" + combinedStepsDesired +
  ",OD=" + oppositeDistance;

print("UART_v5 payload: " + payload);

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:

  • d0 to d23

So if your servo is on digital port 17, the call should usually be:

Servo.getPosition(d17)

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:

  • $actualBearing current measured bearing
  • $desiredBearing target bearing
  • $pathTotalDistance total planned leg distance
  • $combinedStepsDesired expected total steps

That makes it easier for UART_v5 or 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:

  • CSV
  • JSON
  • binary bytes
  • or a custom text command

If you paste your current UART_v5 script, 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.