Sketch Pad icon Sketch Pad Script-driven Sketch Pad for ARC: draw shapes, text, lines and faces via controlCommand() to visualize object locations, paths and robot data. 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)
Rotate and position camera or object with roll/pitch/yaw controls; save camera settings; bind or monitor a 3-value EZ-Script array for live rotation.
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

ARC Pro

Upgrade to ARC Pro

Get access to the latest features and updates before they're released. You'll have everything that's needed to unleash your robot's potential!

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