A simple c++ driver for Dji M3508 and M2006.
Linux
- CMake
- a USB2Can board that supports socketCan
-
Plug in your socketCan board and use
ip link show
to check its portname. Normally, it should be "can0". -
Specify portname and initialize
CanManager
.
CanManager::Instance()->SetPortName("can0");
CanManager::Instance()->Init();
- Construct Motor object and set CanID.
M3508 testMotor; //Construct object
testMotor.Registration(0x201); //specify Can ID
- Configure Control Mode.
Here we provided 3 control modes: SPD_MODE
, POS_MODE
and RELAX_MODE
. When in RELAX_MODE
the motor will have no output torque.
Example: to use speed control mode:
testMotor.controlMode = Motor::SPD_MODE;
- Configure PID Parameter.
In the motor class we provide a nested PID controller for position and speed control. (Check M3508::Update()
).
Configuring pidPosition
is not required when using SPD_MODE
, but we strongly recommand you to do so.
//Position Control
testMotor.pidPosition.kp = ...;
testMotor.pidPosition.ki = ...;
testMotor.pidPosition.kd = ...;
testMotor.pidPosition.maxOut = ...;
testMotor.pidPosition.maxIOut = ...;
//Speed Control
testMotor.pidSpeed.kp = ...;
testMotor.pidSpeed.ki = ...;
testMotor.pidSpeed.kd = ...;
testMotor.pidSpeed.maxOut = ...;
testMotor.pidSpeed.maxIOut = ...;
-
The control loop should be ran at about 1000 Hz.
-
To control the motor, simply set
speedSet
orpositionSet
.
testMotor.speedSet = velDesired;
or
testMotor.positionSet = posDesired;
- Then, call
update()
.
testMotor.update();
See Motor.hpp
for details.
- Send Command
DjiMotorManager::Instance()->Update();
int main()
{
CanManager::Instance()->SetPortName("can0");
CanManager::Instance()->Init();
M3508 testMotor;
testMotor.Registration(0x207); //set ID
testMotor.pidSpeed.kp = 3;
testMotor.pidSpeed.ki = 0.2;
testMotor.pidSpeed.kd = 0.05;
testMotor.pidSpeed.maxOut = 10;
testMotor.pidSpeed.maxIOut = 10;
testMotor.controlMode = Motor::SPD_MODE; //set control mode
while(true)
{
testMotor.speedSet = 2.0f;
testMotor.Update();
DjiMotorManager::Instance()->Update();
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}