Examples and TestApi.cpp (RobotDriver)
main.cpp is a simple example for reading version information. The examples folder contains usage examples for various APIs, burn-in examples, and more.
This document explains the purpose of examples/TestApi.cpp and how each code block maps to RobotArm usage. It applies to MyCobotPro450 with Linux + TCP integration. For environment setup and build, see 6.4.1 and 6.4.2; for API semantics, see 6.4.7 API.
Important Notes (Read First)
TestApi.cppis an integration demo script. It calls many APIs block by block and is not a production-ready safety program.- Do not compile and run the entire file blindly: it may trigger large movements, long JOG operations, drag-teaching recording, etc. Only test after ensuring a clear workspace, accessible emergency stop, and understanding each block.
- Recommended usage: copy only required blocks into your own
main, remove unrelated tests, and adjust parameters (IP, port, angles, coordinates, speed) according to your site. - Build note: in the current root
CMakeLists.txt,examples/TestApi.cppis commented out by default and is not included in theRobotDriverexecutable. If you want to use it as entry, uncomment it inSRC_SOURCESand comment out other example sources as needed, or add a separateadd_executableforTestApi.cppto avoid multiplemainconflicts.
In source code, 1 in RobotArm robot(1, params) means TCP_COMM (consistent with include/Protocol.hpp).
Document Convention
The sections below map one-to-one to the comment blocks in TestApi.cpp (// ========== N.). Each section includes Purpose, Main APIs, and Usage Notes for selective reuse.
1. System Configuration and Version Information
- Purpose: Read controller/model/end-effector/library versions before power-on, and verify communication/firmware information.
- Main APIs:
getSystemVersion,getModifyVersion,getRobotType,getAtomVersion,getToolModifyVersion,getLibVersion. - Usage Notes: No motion risk; version string format depends on firmware.
2. Power-On and Status Query
- Purpose: Power on, then query power status, refresh mode, free-move mode, and zero-calibration first item.
- Main APIs:
powerOn,isPowerOn,getFreshMode,setFreeMoveMode,getFreeMoveMode,isInitCalibration. - Usage Notes:
powerOff()andsetFreshModeswitching are commented in source—power-off may take seconds; after changing refresh mode, firmware may have a long internal delay (source comment example: around 6 s).
3. Motion Control Mode
- Purpose: Read whether current mode is position or torque.
- Main APIs:
getControlMode; (optional)setControlMode. - Usage Notes: Confirm robot status and safe space before switching modes.
4. Joint Angle Control
- Purpose: Read single/all joint angles; demonstrate difference between open-loop (
setWaitArrival(false)+ send command + manualusleep) and closed-loop (setWaitArrival(true)withsendAngleswaiting for arrival). - Main APIs:
getAngle,getAngles,setFreeMoveMode,setWaitArrival,getWaitArrival,sendAngle,sendAngles. - Usage Notes:
- Usually turn off free-move mode before position commands (
setFreeMoveMode(0)in example), otherwise firmware may not execute position commands. - Target angles and speed must follow the MyCobotPro450 ranges in Joint Control.
- Usually turn off free-move mode before position commands (
5. Coordinate Control
- Purpose: Read current
getCoords; show open-loop single-axissendCoord; interrupt withstop. - Main APIs:
getCoords,sendCoord,stop. - Usage Notes:
- Source comments indicate invalid poses such as
SendCoords({0,0,0,0,0,0})may cause abnormal firmwarePOS_ARRIVEDflooding. This is disabled in the demo; do not blindly use all-zero coordinates. - Coordinate and speed ranges are listed in Coordinate Control.
- Source comments indicate invalid poses such as
6. Motion State Control
- Purpose: Query moving state;
pause/isPaused/resume/stop; angle-modeisInPositionexample. - Main APIs:
isMoving,pause,isPaused,resume,stop,isInPosition. - Usage Notes: In
isInPosition, second argumentfalsemeans angle comparison. The example uses all-zero target; use real target and respect limits in practice.
7. Software Joint Limits
- Purpose: Loop and print software min/max angle for each joint.
- Main APIs:
getJointMinAngle,getJointMaxAngle. - Usage Notes: Read-only, no motion.
8. Speed / Acceleration Parameters
- Purpose: Read and sample-write max speed and max acceleration for angle/coordinate modes.
- Main APIs:
getMaxSpeed,setMaxSpeed,getMaxAcc,setMaxAcc. - Usage Notes:
mode0 for joint-angle related values, 1 for coordinate related values; valid numeric ranges are in 6.4.7. Improper settings may affect motion smoothness and safety.
9. Encoders
- Purpose: Read six servo encoder integer values.
- Main APIs:
getServoEncoders. - Usage Notes: Parsing and units follow controller behavior.
10. JOG Mode
- Purpose: Run
jogAngleandjogCoordfor a period, then stop withstop. - Main APIs:
jogAngle,jogCoord,stop. - Usage Notes: JOG is continuous motion; always reserve a stop path or limit protection. Direction 0/1, speed 1~100.
11. Drag Teaching
- Purpose:
stopto settle → enable free-move → record withdragTeachSave→ pause → disable free-move → playbackdragTeachExecute→ cleardragTeachClean. - Main APIs:
stop,setFreeMoveMode,dragTeachSave,dragTeachPause,dragTeachExecute,dragTeachClean. - Usage Notes: Recommended recording duration ≤ 120 s; manual dragging must be done in a safe environment. If teaching is not needed, remove this whole block instead of running it as-is.
12. Exception Information (Source Comment Labels This as 13)
- Purpose: Read motion errors and call
clearErrorInfomation; print fields ingetRobotStatusDetail(collision, motion state, limits, joint status, communication status). - Main APIs:
getErrorInformation,clearErrorInfomation,getRobotStatusDetail. - Usage Notes: Keep method spelling as
clearErrorInfomationper header. Error-code meanings are listed in 6.4.7.getMotorsRunErris left commented in source and can be enabled if needed.
Summary
| Customer goal | Suggested blocks from TestApi |
|---|---|
| Read-only status/version | Block 1 |
| Power and mode | Blocks 2, 3 |
| Joint point-to-point | Block 4 (choose open-loop or closed-loop as needed) |
| Coordinate point-to-point | Block 5 (avoid invalid coordinates) |
| Pause/continue/stop | Block 6 |
| Limits and dynamic parameters | Blocks 7, 8 |
| Encoders | Block 9 |
| Jogging | Block 10 |
| Teaching | Block 11 (optional) |
| Fault troubleshooting | Block 12 |
Again: reuse by block, adjust IP/port and motion parameters, and validate in a safe environment.