Skip to content

3.6 BasScript Class

Overview

BasScript is the structured script builder exposed by the C++17 SDK.

3.6.1 Script Object

cpp
BasScript()
BasScript(const std::string& scriptName)
ItemDescription
DescriptionCreates a script object
NotesA script name can be supplied, or the default constructor can be used

Public Members

MemberDescription
nameScript name
contentScript text lines
flagIfIF closure count
flagSwitchSWITCH closure count
flagWhileWHILE closure count
indentCurrent indentation level

Basic Methods

cpp
GetScript() -> std::string
SetParam(ParamType type, ValueType valueType, SetParamVal_t value) -> STATUS_CODE
AssignValue(AssignType param1, int32_t index, AssignParam2 param2, AssignValue_t value, int32_t optIndex = 0, int32_t optValue = 0) -> STATUS_CODE
MethodDescriptionReturn
GetScriptOutputs the script text generated by the current builderstd::string
SetParamSets a script parameterSTATUS_CODE
AssignValueGenerates an assignmentSTATUS_CODE

Additional notes:

  • BasScript(scriptName) prewrites two lines: scriptName and SUB main .
  • BasScript() creates an empty script object. Use the named constructor when a script header is required.

3.6.2 BasMotion

cpp
MoveJoint(MovePoseType, int32_t, SpeedType, Float64, SmoothType, Float64 = 0.0, const ExtraParam* = nullptr)
MoveLine(MovePoseType, int32_t, SpeedType, Float64, SmoothType, Float64 = 0.0, const ExtraParam* = nullptr)
MoveCircle(MovePoseType, int32_t, MovePoseType, int32_t, SpeedType, Float64, SmoothType, Float64 = 0.0, const ExtraParam* = nullptr)
MoveJump(MovePoseType, int32_t, Float64, Float64, SpeedType, Float64, SmoothType, Float64 = 0.0, const ExtraParam* = nullptr)
MoveJump3(MovePoseType, const std::vector<int32_t>&, Float64, Float64, SmoothType, Float64 = 0.0, const ExtraParam* = nullptr)
MoveJump3cp(MovePoseType, const std::vector<int32_t>&, Float64, SmoothType, Float64 = 0.0, const ExtraParam* = nullptr)
MethodDescriptionKey Constraints
MoveJointJoint motion statementVALUE speed range 0~100
MoveLineLinear motion statementVALUE speed range 0~5000
MoveCircleCircular motion statementVALUE speed range 0~5000
MoveJumpJump statementspeedRatio range 0~100
MoveJump3Three-point Jump statementposeIndex.size() must be 3
MoveJump3cpThree-point continuous Jump statementposeIndex.size() must be 3

Common constraints:

  • When SmoothType::SMOOTH_DISTANCE is used, smoothDistance must be in the range 0~1000 .
  • If an invalid ExtraParam configuration is passed, motion methods return INVALID_PARAMETER .

3.6.3 BasLogical

cpp
LogiIf(LogiParam1, int32_t, LogiParam2, LogiValue_t, BooleanOperator = EQ)
LogiElseIf(LogiParam1, int32_t, LogiParam2, LogiValue_t, BooleanOperator = EQ)
LogiElse()
LogiEndIf()
LogiSwitch(LogiParam1, int32_t)
LogiCase(LogiParam2, LogiCaseValue_t)
LogiDefault()
LogiEndSwitch()
LogiWhile(LogiParam1, int32_t, LogiParam2, LogiValue_t, BooleanOperator = EQ)
LogiEndWhile()
LogiGoto(int32_t index)
LogiLabel(int32_t index)
LogiSkipCondition(LogiParam1, int32_t, LogiParam2, LogiValue_t, BooleanOperator = EQ)
LogiBreak()
LogiContinue()
MethodDescriptionKey Constraints
LogiIfIFSupports registers, IO, literals, strings, and IO status
LogiElseIfELSE IFSame as above
LogiElseELSENone
LogiEndIfENDIFNone
LogiSwitchSWITCHActually outputs SELECT
LogiCaseCASEOtherType supports only VALUE / STRING
LogiDefaultDEFAULTNone
LogiEndSwitchENDSELECTNone
LogiWhileWHILECondition rules are the same as LogiIf
LogiEndWhileWENDNone
LogiGotoGOTOTarget is LABELn
LogiLabelLABELActually outputs scriptName::LABELn
LogiSkipConditionSKIP CONDITIONNeither side may use MH / MI
LogiBreakBREAKNone
LogiContinueCONTINUENone

3.6.4 BasStructure

cpp
Wait(LogiParam1, int32_t, WaitParam2, WaitValue_t, BooleanOperator = EQ)
WaitTime(ValueType, SetParamVal_t)
Pause()
Abort()
Call(const std::string& name)
Run(const std::string& name)
Load(LoadType param, LoadValue_t value)
Unload(LoadType param, LoadValue_t value)
Exec(LoadType param, LoadValue_t value)
MethodDescriptionKey Constraints
WaitConditional waitLeft operand does not support RegisterType::SR ; OtherType supports only IO_STATUS
WaitTimeTime waitVALUE outputs a literal; R outputs R[index]
PausePauseNone
AbortAbortNone
CallSynchronously calls a scriptAutomatically inserts IMPORT name at the file header
RunRuns a script asynchronouslyNone
LoadLoadR/SR pass an index, STRING passes a string, and VALUE passes a numeric value
UnloadUnloadSame as Load
ExecExecuteSame as Load

3.6.5 BasSocket

cpp
SocketOpen(int32_t index)
SocketClose(int32_t index)
SocketConnect(int32_t index)
SocketSend(int32_t index, StrType msgType, SocketValue_t value)
SocketRecv(int32_t index, int32_t msgLength, StrType msgType, SocketValue_t value)
MethodDescriptionKey Constraints
SocketOpenOpens the connectionActually outputs SOCKET_OPEN SK[index]
SocketCloseCloses the connectionActually outputs SOCKET_CLOSE SK[index]
SocketConnectConnectsActually outputs SOCKET_CONNECT SK[index]
SocketSendSends dataSR accepts only an integer index; STRING accepts only a string
SocketRecvReceives dataSR accepts only an integer index; STRING accepts only a string

3.6.6 BasModbus

cpp
ModbusReadMH(int32_t, int32_t, int32_t, int32_t, int32_t)
ModbusReadMI(int32_t, int32_t, int32_t, int32_t, int32_t)
ModbusWriteMH(int32_t, int32_t, int32_t, int32_t, ValueType, int32_t)
MethodDescriptionKey Constraints
ModbusReadMHReads MHResult is written to R[rIndex]
ModbusReadMIReads MIResult is written to R[rIndex]
ModbusWriteMHWrites MHValueType::R writes R[index] ; VALUE writes a literal

3.6.7 BasVision

cpp
VisionFind(const std::string& name)
VisionGetOffset(const std::string& name, int32_t index, int32_t labelIndex)
VisionGetQuantity(const std::string& name, int32_t index)
MethodDescriptionActual Output
VisionFindFindVISION_FIND "name"
VisionGetOffsetOffsetVISION_GET_OFFSET "name" VR[index] GOTO LABEL[labelIndex]
VisionGetQuantityQuantityVISION_GET_QUANTITY "name" R[index]

3.6.8 ExtraParam

cpp
Acceleration(Float64 value)
Rctp()
Offset(int32_t index)
Tb(Float64 second, const std::string& type, const std::string& name = "", int32_t index = 0, const std::string& status = "")
Skip(int32_t index)
Approach(Float64 departureDist, Float64 approachingDist)
MethodDescriptionKey Behavior
AccelerationAdditional accelerationValues outside 1~120 are output as 100.0
RctpRTCP parameterThe C++ name is Rctp ; the script output is RTCP
OffsetOffset parameterOutputs FRAME_OFFSET PR[index]
TbTB parameterMust satisfy either "RUN with name " or "ASSIGN with index+status "; otherwise it is marked as an error
SkipSkip parameterOutputs SKIP GOTO LABELn
ApproachDeparture/approach distanceUsed only as an additional parameter for Jump instructions

3.6.9 Execution Semantics

ItemDescription
GetScriptReturns the current script text
ExecuteBasScriptValidates first, then automatically appends RETURN / END
Closure validationIF / SWITCH / WHILE must be closed
CallInserts IMPORT name at the script header, then generates CALL "name"

Minimal Call Example

cpp
#include <iostream>      // Standard output stream for printing generated script text
#include "arm_api.h"    // Arm entry point and BasScript builder type
#include "status_code.h" // STATUS_CODE for checking builder call results

int main()
{
    Arm arm;
    STATUS_CODE connectRet = arm.Connect("192.168.110.2", "");
    if (connectRet != STATUS_CODE::OK) {
        return 1;
    }

    BasScript script("demo.bas");
    STATUS_CODE waitRet = script.structure.WaitTime(
        ValueType::VALUE,
        Float64{0.1}
    );
    if (waitRet != STATUS_CODE::OK) {
        return 1;
    }

    STATUS_CODE loadRet = script.structure.Load(
        LoadType::STRING,
        std::string("demo_program")
    );
    if (loadRet != STATUS_CODE::OK) {
        return 1;
    }

    std::cout << script.GetScript() << "\n";
    // STATUS_CODE execRet = arm.programManager.ExecuteBasScript(script);
    return 0;
}

Scenario Examples

The snippets below cover the BasScript builder APIs across basic construction, motion statements, logical statements, flow-control statements, and Socket/Modbus/vision statements. The examples only build script text and do not send it to the controller.

Basic Builder and Extra Parameters

cpp
BasScript script("demo.bas");
BasScript emptyScript;
ExtraParam extra;
extra.Acceleration(80.0);
extra.Rctp();
extra.Offset(1);
extra.Tb(0.1, "RUN", "helper.bas");
extra.Skip(10);
extra.Approach(10.0, 10.0);
STATUS_CODE setTfRet = script.SetParam(ParamType::TF_NO, ValueType::VALUE, int32_t{0});
STATUS_CODE assignRet = script.AssignValue(AssignType::R, 1, OtherType::VALUE, Float64{3.14});
std::cout << script.GetScript() << "\n";

Generate Motion Statements

cpp
BasScript script("motion.bas");
ExtraParam extra;
extra.Acceleration(80.0);
STATUS_CODE moveJointRet = script.motion.MoveJoint(MovePoseType::PR, 1, SpeedType::VALUE, 20.0, SmoothType::FINE, 0.0, &extra);
STATUS_CODE moveLineRet = script.motion.MoveLine(MovePoseType::PR, 2, SpeedType::VALUE, 100.0, SmoothType::SMOOTH_DISTANCE, 5.0, &extra);
STATUS_CODE moveCircleRet = script.motion.MoveCircle(MovePoseType::PR, 2, MovePoseType::PR, 3, SpeedType::VALUE, 100.0, SmoothType::FINE, 0.0, &extra);
STATUS_CODE moveJumpRet = script.motion.MoveJump(MovePoseType::PR, 4, 100.0, 50.0, SpeedType::VALUE, 20.0, SmoothType::FINE, 0.0, &extra);
STATUS_CODE moveJump3Ret = script.motion.MoveJump3(MovePoseType::PR, std::vector<int32_t>{1, 2, 3}, 100.0, 50.0, SmoothType::FINE, 0.0, &extra);
STATUS_CODE moveJump3cpRet = script.motion.MoveJump3cp(MovePoseType::PR, std::vector<int32_t>{1, 2, 3}, 100.0, SmoothType::FINE, 0.0, &extra);

Generate Condition and Loop Statements

cpp
BasScript script("logical.bas");
STATUS_CODE ifRet = script.logical.LogiIf(RegisterType::R, 1, OtherType::VALUE, Float64{1.0}, BooleanOperator::EQ);
STATUS_CODE elseIfRet = script.logical.LogiElseIf(RegisterType::R, 1, OtherType::VALUE, Float64{2.0}, BooleanOperator::EQ);
STATUS_CODE elseRet = script.logical.LogiElse();
STATUS_CODE endIfRet = script.logical.LogiEndIf();
STATUS_CODE switchRet = script.logical.LogiSwitch(RegisterType::R, 1);
STATUS_CODE caseRet = script.logical.LogiCase(OtherType::VALUE, int32_t{1});
STATUS_CODE defaultRet = script.logical.LogiDefault();
STATUS_CODE endSwitchRet = script.logical.LogiEndSwitch();
STATUS_CODE whileRet = script.logical.LogiWhile(RegisterType::R, 1, OtherType::VALUE, Float64{1.0}, BooleanOperator::EQ);
STATUS_CODE continueRet = script.logical.LogiContinue();
STATUS_CODE breakRet = script.logical.LogiBreak();
STATUS_CODE endWhileRet = script.logical.LogiEndWhile();
STATUS_CODE labelRet = script.logical.LogiLabel(10);
STATUS_CODE gotoRet = script.logical.LogiGoto(10);
STATUS_CODE skipRet = script.logical.LogiSkipCondition(IOType::DI, 0, OtherType::IO_STATUS, IOStatus::ON, BooleanOperator::EQ);

Generate Flow-Control Statements

cpp
BasScript script("structure.bas");
STATUS_CODE waitRet = script.structure.Wait(IOType::DI, 0, OtherType::IO_STATUS, IOStatus::ON, BooleanOperator::EQ);
STATUS_CODE waitTimeRet = script.structure.WaitTime(ValueType::VALUE, Float64{0.1});
STATUS_CODE pauseRet = script.structure.Pause();
STATUS_CODE abortRet = script.structure.Abort();
STATUS_CODE callRet = script.structure.Call("helper.bas");
STATUS_CODE runRet = script.structure.Run("helper.bas");
STATUS_CODE loadRet = script.structure.Load(LoadType::STRING, std::string("demo_program"));
STATUS_CODE unloadRet = script.structure.Unload(LoadType::STRING, std::string("demo_program"));
STATUS_CODE execRet = script.structure.Exec(LoadType::STRING, std::string("demo_program"));

Generate Socket, Modbus, and Vision Statements

cpp
BasScript script("io.bas");
STATUS_CODE socketOpenRet = script.socket.SocketOpen(1);
STATUS_CODE socketConnectRet = script.socket.SocketConnect(1);
STATUS_CODE socketSendRet = script.socket.SocketSend(1, StrType::STRING, std::string("hello"));
STATUS_CODE socketRecvRet = script.socket.SocketRecv(1, 32, StrType::SR, int32_t{1});
STATUS_CODE socketCloseRet = script.socket.SocketClose(1);
STATUS_CODE readMhRet = script.modbus.ModbusReadMH(0, 1, 0, 2, 1);
STATUS_CODE readMiRet = script.modbus.ModbusReadMI(0, 1, 0, 2, 2);
STATUS_CODE writeMhRet = script.modbus.ModbusWriteMH(0, 1, 0, 1, ValueType::VALUE, 10);
STATUS_CODE visionFindRet = script.vision.VisionFind("camera");
STATUS_CODE visionOffsetRet = script.vision.VisionGetOffset("camera", 1, 10);
STATUS_CODE visionQuantityRet = script.vision.VisionGetQuantity("camera", 2);

Example code:

cpp17/bas_script_execution/src/main.cpp
cpp
#include "build_bas_script/run.h"
#include "execute_bas_script/run.h"

int main(void)
{
    // [ZH] 默认只调用一个门面方法;如需体验其他接口,请把下一行替换成下面任意一行。
    // [EN] The main function calls only one facade by default. Replace the next line with any line below to try other APIs.
    return RunBasScriptExecutionBuildBasScript();
    // return RunBasScriptExecutionExecuteBasScript();
}