Skip to content

Latest commit

History

29 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

DaMiao Python Library

Language: 中文 | ENGLISH

This library supports macOS, Linux, and Windows platforms.

🎉 Join the QQ group for DaMiao motor technical discussions: [677900232].
🛒 Visit the DaMiao Store on Taobao: DaMiao Intelligent Control Store.


Table of Contents

  1. Importing the DaMiao Library
  2. Defining Motor Control Objects
  3. Motor States
  4. Motor Control Modes
  5. Reading Motor States
  6. Modifying Motor Parameters

1. Importing the DaMiao Library

Ensure that DM_CAN.py is in your project folder. Import it as follows:

fromDM_CANimport*

The library depends on the following Python packages: serial, numpy. Install them using:

pip install -r requirements.txt

2. Defining Motor Control Objects

Create motor objects using:

Motor1=Motor(DM_Motor_Type.DM4310, 0x01, 0x11)
Motor2=Motor(DM_Motor_Type.DM4310, 0x02, 0x12)
Motor3=Motor(DM_Motor_Type.DM4310, 0x03, 0x13)
  • First Parameter: Motor type
  • Second Parameter: Slave ID (Motor's CAN ID)
  • Third Parameter: Master ID (Host ID; must be unique and not 0x00)

Set up the serial device (example for Windows):

serial_device=serial.Serial('COM8', 921600, timeout=0.5)

Initialize the motor control object:

MotorControl1=MotorControl(serial_device)

3. Motor States

3.1 Adding Motors

MotorControl1.addMotor(Motor1)
MotorControl1.addMotor(Motor2)
MotorControl1.addMotor(Motor3)

3.2 Enabling Motors

Enable motors (ensure parameters are set before enabling):

MotorControl1.enable(Motor1)
MotorControl1.enable(Motor2)
MotorControl1.enable(Motor3)

For older firmware, specify the control mode:

MotorControl1.enable_old(Motor1, Control_Type.MIT)
MotorControl1.enable_old(Motor2, Control_Type.POS_VEL)
MotorControl1.enable_old(Motor3, Control_Type.VEL)

3.3 Setting Zero Position

Move the motor to the desired zero position (in disabled state) and set it as the new zero position:

MotorControl1.set_zero_position(Motor3)
MotorControl1.set_zero_position(Motor6)

3.4 Disabling Motors

MotorControl1.disable(Motor3)
MotorControl1.disable(Motor6)

3.5 Fetching Motor Status

To fetch motor status (torque, position, velocity) without sending control commands:

MotorControl1.refresh_motor_status(Motor1)
print("Motor1:","POS:", Motor1.getPosition(), "VEL:", Motor1.getVelocity(), "TORQUE:", Motor1.getTorque())

4. Motor Control Modes

4.1 MIT Mode

After enabling the motor, use MIT mode for control:

MotorControl1.controlMIT(Motor1, 50, 0.3, 0, 0, 0)

4.2 Position-Velocity Mode

Control motor position and velocity:

q=math.sin(time.time())
MotorControl1.control_Pos_Vel(Motor1, q*10, 2)

4.3 Velocity Mode

Control motor velocity:

q=math.sin(time.time())
MotorControl1.control_Vel(Motor1, q*5)

4.4 Force-Position Mixed Mode

Control motor position and force:

MotorControl1.control_pos_force(Motor1, 10, 1000, 100)

5. Reading Motor States

Motor states are stored in the motor object. You can read the following parameters:

  • Velocity
  • Position
  • Torque

Use the following functions:

vel=Motor1.getVelocity() # Get motor velocitypos=Motor1.getPosition() # Get motor positiontau=Motor1.getTorque() # Get motor torque

After refreshing motor status:

MotorControl1.refresh_motor_status(Motor1)
print("Motor1:", "POS:", Motor1.getPosition(), "VEL:", Motor1.getVelocity(), "TORQUE:", Motor1.getTorque())

6. Modifying Motor Parameters

6.1 Changing Control Mode

You can change the motor's control mode using the following function:

ifMotorControl1.switchControlMode(Motor1, Control_Type.POS_VEL):
print("Switch to POS_VEL mode success")
ifMotorControl1.switchControlMode(Motor2, Control_Type.VEL):
print("Switch to VEL mode success")

Note: The change is only temporary and will be reset after power cycle.

6.2 Saving Parameters

To save all changed parameters to the motor's flash memory:

MotorControl1.save_motor_param(Motor1)

6.3 Reading Internal Registers

You can read internal registers of the motor, such as:

print("PMAX:", MotorControl1.read_motor_param(Motor1, DM_variable.PMAX))
print("MST_ID:", MotorControl1.read_motor_param(Motor1, DM_variable.MST_ID))
print("VMAX:", MotorControl1.read_motor_param(Motor1, DM_variable.VMAX))
print("TMAX:", MotorControl1.read_motor_param(Motor1, DM_variable.TMAX))

Use getParam to retrieve stored values:

print("PMAX", Motor1.getParam(DM_variable.PMAX))

6.4 Writing Internal Registers

Some internal register values can be modified. Here's an example:

ifMotorControl1.change_motor_param(Motor1, DM_variable.KP_APR, 54):
print("Write success")

Note: Changes will be lost after power cycle unless saved to flash memory.

7.Internal Register Parameter List Note:

Note:

  • RW: Read and Write
  • RO: Read Only - Corresponding variable names can be directly used in DM_variable.
Register Address (Decimal)VariableDescriptionR/WRangeData Type
0UV_ValueUnder-voltage ValueRW(10.0,3.4E38]float
1KT_ValueTorque CoefficientRW[0.0,3.4E38]float
2OT_ValueOver-temperatureRW[80.0,200)float
3OC_ValueOver-current ValueRW(0.0,1.0)float
4ACCAccelerationRW(0.0,3.4E38)float
5DECDecelerationRW[-3.4E38,0.0)float
6MAX_SPDMaximum SpeedRW(0.0,3.4E38]float
7MST_IDFeedback IDRW[0,0x7FF]uint32
8ESC_IDReceive IDRW[0,0x7FF]uint32
9TIMEOUTTimeout Alarm TimeRW[0,2^32-1]uint32
10CTRL_MODEControl ModeRW[1,4]uint32
11DampMotor Damping Coeff.RO/float
12InertiaMotor InertiaRO/float
13hw_verReservedRO/uint32
14sw_verSoftware VersionRO/uint32
15SNReservedRO/uint32
16NPPMotor Pole PairsRO/uint32
17RsMotor Phase ResistanceRO/float
18LsMotor Phase InductanceRO/float
19FluxMotor Flux ValueRO/float
20GrGear Reduction RatioRO/float
21PMAXPosition Mapping MaxRW(0.0,3.4E38]float
22VMAXSpeed Mapping MaxRW(0.0,3.4E38]float
23TMAXTorque Mapping MaxRW(0.0,3.4E38]float
24I_BWCurrent Loop BandwidthRW[100.0,10000.0]float
25KP_ASRSpeed Loop KpRW[0.0,3.4E38]float
26KI_ASRSpeed Loop KiRW[0.0,3.4E38]float
27KP_APRPosition Loop KpRW[0.0,3.4E38]float
28KI_APRPosition Loop KiRW[0.0,3.4E38]float
29OV_ValueOver-voltage ValueRWTBDfloat
30GREFGear Torque EfficiencyRW(0.0,1.0]float
31DetaSpeed Loop Damping Coeff.RW[1.0,30.0]float
32V_BWSpeed Loop Filter BandwidthRW(0.0,500.0)float
33IQ_c1Current Loop GainRW[100.0,10000.0]float
34VL_c1Speed Loop GainRW(0.0,10000.0]float
35can_brCAN Baud Rate CodeRW[0,4]uint32
36sub_verSub-versionROuint32
50u_offU Phase OffsetROfloat
51v_offV Phase OffsetROfloat
52k1Compensation Factor 1ROfloat
53k2Compensation Factor 2ROfloat
54m_offAngle OffsetROfloat
55dirDirectionROfloat
80p_mMotor PositionROfloat
81xoutOutput Shaft PositionROfloat

About

Damiao Motor Control Library – A Python library for controlling Damiao motors via CAN. Supports Windows, Linux, and macOS. Flexible control modes and real-time motor status feedback. 达妙电机控制库 – 一个用于通过CAN控制达妙电机的Python库。支持 Windows、Linux 和 macOS。提供灵活的控制模式和实时电机状态反馈。

Resources

Stars

76 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages