Skip to content

Commit

Permalink
damiao motor on dart is on CAN2 and is working now. Enable damiao mot…
Browse files Browse the repository at this point in the history
…or method modified.
  • Loading branch information
Raventhatfly committed Aug 1, 2023
1 parent 897bd3a commit 165cee6
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 22 deletions.
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,13 @@ add_executable(DART
dev/application/vehicles/dart/can_motor_config.cpp
dev/application/vehicles/dart/main_dart.cpp
dev/application/vehicles/dart/user_dart.cpp

dev/interface/damiao_motor/damiao_motor_feedback.cpp
dev/interface/damiao_motor/damiao_motor_interface.cpp
dev/interface/damiao_motor/damiao_motor_controller.cpp
)
target_include_directories(DART PRIVATE dev/application/vehicles/dart)
target_include_directories(DART PRIVATE dev/application/vehicles/dart
dev/interface/damiao_motor)
target_compile_definitions(DART PRIVATE DART)

# ======================================= Unit Tests Programs =======================================
Expand Down
56 changes: 56 additions & 0 deletions dev/application/vehicles/dart/damiao_motor_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// Created by Wu Feiyang on 7/13/23.
//

#ifndef META_DAMIAO_MOTOR_CFG_H
#define META_DAMIAO_MOTOR_CFG_H

/**
* Damiao 4310 motor.
*/
#include "hal.h"
#define can_channel_1 &CAND1
#define can_channel_2 &CAND2

typedef enum motor_mode{
MIT_MODE,
POS_VEL_MODE,
VEL_MODE,
}motor_mode_t;

class DamiaoMotorBase{
public:
CANDriver* can_driver;
int masterID;
int slaveID;
float mitKp;
float mitKd;
float V_max; // maximum rotation speed. Unit is Rad/s.
float P_max; // maximum Position. Unit is Rad.
float T_max; // maximum Torque. Unit is N*m.
float initial_encoder_angle;
motor_mode_t mode;
float kp_min;
float kp_max;
float kd_min;
float kd_max;
};

class DamiaoMotorCFG{
public:
enum MotorName{
DART_LOADER,
MOTOR_COUNT,
}motor_usage_t;
/***
* @brief Motor Configuration
* The unit of all velocity is Rad/s, unit of P_MAX must be exactly identical to the value with that of the Damiao Offical tool,
* whose unit is by defualt Rad. All unit of torque is N * m. The unit of @param initial_encoder_angle is degree.
*/
static constexpr DamiaoMotorBase motorCfg[MOTOR_COUNT] = {
{can_channel_2,0x00,0x01,1.0,0.3,30,3.141953,10.0,
-124.74,POS_VEL_MODE,0.0,500.0,0.0,5.0},
};
};

#endif //META_DAMIAO_MOTOR_CFG_H
14 changes: 10 additions & 4 deletions dev/application/vehicles/dart/main_dart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "user_dart.h"
#include "led.h"
#include "referee_interface.h"
#include "damiao_motor_controller.h"

CANInterface can1(&CAND1);
CANInterface can2(&CAND2);
Expand All @@ -34,16 +35,21 @@ int main(){
Remote::start();

LED::led_on(1); // led 1 turned on now
can1.start(NORMALPRIO+3);
can2.start(NORMALPRIO+4);
can1.start(HIGHPRIO-1);
can2.start(HIGHPRIO-2);
LED::led_on(2);// led 2 turned on now

/// Setup Referee
Referee::init();

UserDart::start(NORMALPRIO-1);
CANMotorController::start(HIGHPRIO-1,HIGHPRIO-2,&can1,&can2);
chThdSleepSeconds(5);
CANMotorController::start(HIGHPRIO-3,HIGHPRIO-4,&can1,&can2);
DamiaoMotorController::start(NORMALPRIO+3,NORMALPRIO+2,&can1,&can2);
DamiaoMotorController::motor_enable(DamiaoMotorCFG::DART_LOADER);
DamiaoMotorController::shell_display(DamiaoMotorCFG::DART_LOADER,true);
DamiaoMotorController::set_target_POSVEL(DamiaoMotorCFG::DART_LOADER,0,2);
//
// DamiaoMotorController::set_target_POSVEL(DamiaoMotorCFG::DART_LOADER,0,2);

#if CH_CFG_NO_IDLE_THREAD // see chconf.h for what this #define means
// ChibiOS idle thread has been disabled, main() should implement infinite loop
Expand Down
11 changes: 8 additions & 3 deletions dev/application/vehicles/dart/user_dart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Created by Wu Feiyang on 2023/7/11.
//
#include "user_dart.h"
#include "damiao_motor_controller.h"
UserDart::UserThread UserDart::userThread;
bool UserDart::UserThread::start_flag;
bool UserDart::timer_started;
Expand Down Expand Up @@ -43,7 +44,7 @@ void UserDart::UserThread::main(){


while(!shouldTerminate()){

// DamiaoMotorController::set_target_POSVEL(DamiaoMotorCFG::DART_LOADER,90,2);
if(Remote::rc.s1 == Remote::S_UP){ // Forced Relax Mode
chSysLock();
for(uint i=0;i<CANMotorCFG::MOTOR_COUNT;i++){
Expand Down Expand Up @@ -105,12 +106,16 @@ void UserDart::UserThread::main(){
CANMotorController::set_target_angle(CANMotorCFG::STORE_ENERGY_LEFT,0);
CANMotorController::set_target_angle(CANMotorCFG::STORE_ENERGY_RIGHT,0);

}else if(current_time -start_time >= 4000 && current_time - start_time < 6000){
}else if(current_time - start_time >= 4000 && current_time -start_time < 8000){
DamiaoMotorController::set_target_POSVEL(DamiaoMotorCFG::DART_LOADER,90,3);
chThdSleepSeconds(1);
DamiaoMotorController::set_target_POSVEL(DamiaoMotorCFG::DART_LOADER,0,3);
}else if(current_time -start_time >= 8000 && current_time - start_time < 10000){
rudder1.set_rudder_angle(0);
chThdSleepMilliseconds(900);
rudder1.set_rudder_angle(180);
chThdSleepMilliseconds(900);
}else if(current_time > 8000){
}else if(current_time > 10000){
start_flag = false;
timer_started = false;
}
Expand Down
2 changes: 1 addition & 1 deletion dev/interface/damiao_motor/damiao_motor_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @file can_motor_scheduler.h
* @brief Class to drive can motors.
*
* @addtogroup CAN Driver
* @addtogroup Damiao Driver
* @{
*/

Expand Down
2 changes: 1 addition & 1 deletion dev/interface/damiao_motor/damiao_motor_feedback.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

//#include "damiao_motor_interface.h"
#include "common_macro.h"
#include "application/unit_tests/ut_damiao_motor/damiao_motor_config.h"
#include "damiao_motor_config.h"

/**
* @author Wu Feiyang
Expand Down
17 changes: 5 additions & 12 deletions dev/interface/damiao_motor/damiao_motor_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,11 @@ void DamiaoMotorIF::init(CANInterface *can1_, CANInterface *can2_) {
void DamiaoMotorIF::start(DamiaoMotorCFG::MotorName motorProfile) {
DamiaoMotorIF::set_mode(motorProfile,DamiaoMotorCFG::motorCfg[motorProfile].mode);
chThdSleepMilliseconds(1500);
for(int i=10;i > 0;i--){
chSysLock();
motors_can_tx_frame[motorProfile].DLC = 0x08;
motors_can_tx_frame[motorProfile].data64[0] = start_cmd;
chSysUnlock();
if(DamiaoMotorCFG::motorCfg[motorProfile].can_driver == &CAND1){
can1->send_msg(&motors_can_tx_frame[motorProfile]);
}else if(DamiaoMotorCFG::motorCfg[motorProfile].can_driver == &CAND2){
can2->send_msg(&motors_can_tx_frame[motorProfile]);
}
chThdSleepMilliseconds(10);
}
chSysLock();
motors_can_tx_frame[motorProfile].DLC = 0x08;
motors_can_tx_frame[motorProfile].data64[0] = start_cmd;
chSysUnlock();
chThdSleepMilliseconds(500);
chSysLock();
if(motors_mode[motorProfile] != VEL_MODE){
motors_can_tx_frame[motorProfile].DLC = 0x08;
Expand Down

1 comment on commit 165cee6

@QuokeCola
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the file dev/interface/damiao_motor/damiao_motor_interface.cpp at lines 36-37, it appears that the init function registers components in the CAN network from SID 0x000 to 0x500 for the damiao callback. This range overlaps with other common components, specifically RM motors (0x200-0x20B). This means that if the damiao interface is initialized before the can motor interface, feedback frames from common RM motors such as M3508, M2006, and GM6020 will inadvertently trigger the damiao interface callback function, leading to unforeseen errors. Please ensure you thoroughly review the code before integrating damiao motors with other CAN components.

Please sign in to comment.