Package: xarm
xArm Servo ControllerThis is a library of methods to control the LewanSoul-Lobot-HiWonder xArm and LeArm, a 6-DOF robot arm, over TTL Serial or USB.xArmLeArmTable of ContentReporting a problemTTL Serial ControlUSB ControlSetting up a Python development environmentInstalling the xArm moduleMethods and examplesclassServoclassControllersetPositiongetPositionservoOffgetBatteryVoltageThings left To-DoReporting a ProblemIf you run into a problem installing or using this library, open an issue athttps://github.com/ccourson/xArmServoController/issues.TTL Serial ControlThe control board mounted on the base of the xArm and LeArm has a 4-pin connector which provides a signal path and power to an external host controller.PinDescriptionGNDPower and signal ground.TXTransmit serial TTL signal from arm to host controller. Mark = 5VDC, Space = 0VDC.RXReceive serial TTL signal from host controller to arm. Mark = 5VDC, Space = 0VDC.5V5 Volts DC power to power an eternal host controller. Current rating is not yet known. In most circumstances this is left unconnected.|:warning: Warning: Do not connect 5V pin to an external power source. Doing so will cause the arm to beep loudly and may damage the control board. The 5V pin is for powering an external host controller.USB ControlThe control board mounted on the base of the xArm and LeArm has a USB connector. The xArm has type mini-B and the LeArm has type micro-B. The arms are listed as Human Interface Devices in Windows Device Manager as "HID-compliant bar code badge reader".VIDPID04835750:warning: Warning: The xArm and LeArm Windows apps will recognize either arm. The position ranges for each arm are very different and using the wrong app may cause damage to the arm.Setting up a Windows Python development environmentThere are many options for setting up Python development environment in Windows. To get xArmController running you install the following:Python 3An editor or IDExArmController moduleHow to install Python 3If you already have Python installed, ensure you are running version 3.6 or higher.WindowsOpen a  web browser tohttps://www.python.org/Click on the "Download Python {version here}" button on the home page.After downloaded, run the installer and follow the prompts.When prompted, choose to "Add python.exe to Path".Linux including Ubuntu and Lubuntu$ apt install python3MacOS$ brew add python3Installing an editorPython includes a very simple GUI editor called IDLE. This editor is adequate for the examples and demos in this library.A more robust editor is VSCode (Visual Studio Code). It can be installed fromhttps://code.visualstudio.com/. There are a couple plugins that will be required to get the best experience from VSCode. VSCode willautomagicallyprompt you for these plugins when it detects you are working with Python.If you prefer a more traditional IDE for Python, tryPyCharm by Jet Brains.Installing the xArm moduleFrom the Windows Command Prompt or Terminal in Linux/MacOS, run the following command to install xArmController and its module dependency hidusb. Just copy from here and paste to the terminal command line.pip install xarmMethods and examplesConnecting to the xArmThis example is the bare minimum to connect to the xArm via USB and read the battery voltage.importxarm# arm is the first xArm detected which is connected to USBarm=xarm.Controller('USB')print('Battery voltage in volts:',arm.getBatteryVoltage())Output:Battery voltage: 7.677The Controller can connect to a specific xArm by appending the serial number to "USB". To find the serial number of your xArm, turn on debug, then after connecting one xArm, run this code.importxarmarm=xarm.Controller('USB',debug=True)Output:Serial number: 497223563535Now knowing the serial number you can connect to a specific xArm like this example using two.importxarmarm1=xarm.Controller('USB497223563535')arm2=xarm.Controller('USB497253743535')print('Battery voltage of arm1 in volts:',arm1.getBatteryVoltage())print('Battery voltage of arm2 in volts:',arm2.getBatteryVoltage())Output:Battery voltage of arm1 in volts: 7.677Battery voltage of arm2 in volts: 7.662In the same way, the Controller can connect to one or more serailly connected xArms. To do this, supply Controller the serial ports they are connected to.Classes and MethodsclassServo(servo_id[,position=500])Returns aServoobject. The Servo class is used to hold the position of a given servo in units and angle.Properties include:servo_id - ID of servo.position - Position of servo in units (0 to 1000).angle - Angle of servo in degrees (-125.0 to 125.0 in 0.25 degree integral).Thepositionparamter may be specified with anintvalue between 0 and 1000. When set to afloatvalue between -125.0 to 125.0, theanglevalue is set in degrees and rounded to a 0.25 degree integral. There are 4 servo units per degree of angle.Note: In Python, anintvalue does not have a decimal point (500). Afloatvalue has a decimal point (0.0).Setting position will also set the corresponding angle and visa-versa. Ifpositionis not specified, it will default to '500' andanglewill be set to '0.0'.importxarmarm=xarm.Controller('USB')# define servo as servo ID 1 with position 300servo=xarm.Servo(1,300)print('servo ID:',servo.servo_id)print('servo position:',servo.position)print('servo angle:',servo.angle)Output:servo ID: 1servo position: 300servo angle: -50.0classController(com_port[,debug=False])Returns aControllerobject. The Controller class connects Python to the xArm. The port to connect to the xArm through is determined bycom_portwhich can be a serial port (COM5) or USB port (USB). Multiple xArms may be connected. If more than one xArm is attached by USB, each can be identified by appending the serial number to 'USB' (USB497223563535).Optionally, whendebugisTrue, communication diganostic information will be printed to the terminal.# attach to xArm connected to USBarm1=xarm.Controller('USB')# attach to USB connected xArm with serial number '497223563535'arm2=xarm.Controller('USB497223563535')# attach to xArm connected to serial port 'COM5'arm3=xarm.Controller('COM5')# enable debugarm4=xarm.Controller('COM6',True)# positional argumentarm5=xarm.Controller('COM7',debug=True)# named argumentsetPosition(servos[,position=None,duration=1000,wait=False])Moves one or moreservosto a specifiedpositionover adurationand optionalllywaits during the duration.Whenservosis anintvalue, it represents a servo ID and thepositionparameter is required.Thepositionparamter may be anintto indicate a unit position (0 to 1000) or afloatto indicate an angle in degrees (-125.0 to 125.0).# Set servo ID 1 to position 500.setPosition(1,500)Whenservosis aServoobject, thepositionparameter is ignored.The Servo parameter holds the position and angle.Whenservosis alist, it may containServoobjects or servo ID and position pairs and thepositionparameter is ignored.importxarmarm=xarm.Controller('USB')servo1=xarm.Servo(1)# assumes default unit position 500servo2=xarm.Servo(2,300)# unit position 300servo3=xarm.Servo(3,90.0)# angle 90 degrees# sets servo 1 to unit position 300 and waits the default 1 second# before returningarm.setPosition(1,300,wait=True)# sets servo 2 to unit position 700 and moves the servo at a# rate of 2 secondsarm.setPosition(2,700,2000,True)# sets servo1 to 45 degrees and waits the default 1 second# before returningarm.setPosition(3,45.0,wait=True)# sets servo 2 to position 300 as defined above but continues to# the next method before completing movementarm.setPosition(servo2)# sets servos 1-3 as defined and continues without waitingarm.setPosition([servo1,servo3])# sets servos 1 to unit position 200 and servo 2 to 90 degreesarm.setPosition([[1,200],[2,90.0]],wait=True)# Servo object and servo ID/position pairs can be combinedarm.setPosition([servo1,[2,500],[3,0.0]],2000)getPosition(servos[,degrees=False])Returns the currentpositionof one or moreservos.By default, theunit positionis returned. WhendegreesisTrue, theangleis returned.Theservosparameter may be a servo ID (1 to 6) or aServoobject or a list of one or moreServoobjects.importxarmarm=xarm.Controller('USB')servo1=xarm.Servo(1)servo2=xarm.Servo(2)servo3=xarm.Servo(3)# Gets the position of servo 1 in unitsposition=arm.getPosition(1)print('Servo 1 position:',position)# Gets the position of servo 2 as defined aboveposition=arm.getPosition(servo2)print('Servo 2 position:',position)# Gets the position of servo 3 in degreesposition=arm.getPosition(3,True)print('Servo 3 position (degrees):',position)# Gets the position of servo 2 as defined above# It is not necessary to set the degreees parameter# because the Servo object performes that conversionposition=arm.getPosition([servo1,servo2,servo3])print('Servo 1 position (degrees):',servo1.angle)print('Servo 2 position (degrees):',servo2.angle)print('Servo 3 position (degrees):',servo3.angle)servoOff([servos=None])Turns off motor of one or more servos. Ifservosparamter is not specified, will turn off all servo motors.importxarmarm=xarm.Controller('USB')servo2=xarm.Servo(2)servo5=xarm.Servo(5)servo6=xarm.Servo(6)# Turns off servo motor 1arm.servoOff(1)# Turns off servo motor 1arm.servoOff(servo2)# Turns off servo motors 3 and 4arm.servoOff([3,4])# Turns off servo motors 5 and 6arm.servoOff([servo5,servo6])getBatteryVoltage()Returns battery or power supply voltage on volts.importxarmarm=xarm.Controller('USB')battery_voltage=arm.getBatteryVoltage()print('Battery voltage (volts):',battery_voltage)To-DogripCalibrate - Gripper will slowly close and detect when it has reached the fully closed condition.gripClose - Gripper will slowly close and stop when it has detected gripper is no longer closing.gripOpen - Gripper will open to a specified with in centimeters.gripMove - Gripper will move to a Euclidean coordinate while maintining its angle.Built-in functionsgroupRungroupStopgroupErasegroupDownloadgroupSpeedoffsetReadoffsetWriteoffsetAdjust