Skip to content

Commit cec8df0

Browse files
committed
fist implementation of the custom motion control type
1 parent a8e5793 commit cec8df0

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

src/common/base_classes/FOCMotor.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,8 @@ void FOCMotor::loopFOC() {
584584
if (sensor) sensor->update();
585585

586586
// if disabled do nothing
587-
if(!enabled) return;
587+
// or if the motor is not ready (e.g. failed to calibrate or not calibrated yet) do nothing
588+
if(!enabled || motor_status != FOCMotorStatus::motor_ready) return;
588589

589590
// if open-loop do nothing
590591
if( controller==MotionControlType::angle_openloop || controller==MotionControlType::velocity_openloop )
@@ -691,7 +692,8 @@ void FOCMotor::move(float new_target) {
691692
}
692693

693694
// if disabled do nothing
694-
if(!enabled) return;
695+
// and if
696+
if(!enabled || motor_status != FOCMotorStatus::motor_ready) return;
695697

696698

697699
// upgrade the current based voltage limit
@@ -745,6 +747,12 @@ void FOCMotor::move(float new_target) {
745747
// returned values correspond to the voltage_limit and current_limit
746748
current_sp = angleOpenloop(shaft_angle_sp);
747749
break;
750+
case MotionControlType::custom:
751+
// custom control - user provides the function that calculates the current_sp
752+
// based on the target value and the motor state
753+
// user makes sure to use it with appropriate torque control mode
754+
if(customMotionControlCallback)
755+
current_sp = customMotionControlCallback(this, target);
748756
}
749757
}
750758

@@ -768,8 +776,8 @@ int FOCMotor::initFOC() {
768776
SIMPLEFOC_MOTOR_DEBUG("No sensor.");
769777
if ((controller == MotionControlType::angle_openloop || controller == MotionControlType::velocity_openloop)){
770778
exit_flag = 1;
771-
SIMPLEFOC_MOTOR_ERROR("Openloop only!");
772779
}else{
780+
SIMPLEFOC_MOTOR_ERROR("Only openloop allowed!");
773781
exit_flag = 0; // no FOC without sensor
774782
}
775783
}
@@ -781,13 +789,13 @@ int FOCMotor::initFOC() {
781789
if(current_sense){
782790
if (!current_sense->initialized) {
783791
motor_status = FOCMotorStatus::motor_calib_failed;
784-
SIMPLEFOC_MOTOR_ERROR("Init FOC error, current sense not init");
792+
SIMPLEFOC_MOTOR_ERROR("Current sense not init!");
785793
exit_flag = 0;
786794
}else{
787795
exit_flag *= alignCurrentSense();
788796
}
789797
}
790-
else { SIMPLEFOC_MOTOR_ERROR("No current sense"); }
798+
else { SIMPLEFOC_MOTOR_ERROR("No current sense."); }
791799
}
792800

793801
if(exit_flag){

src/common/base_classes/FOCMotor.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ enum MotionControlType : uint8_t {
4848
angle = 0x02, //!< Position/angle motion control
4949
velocity_openloop = 0x03,
5050
angle_openloop = 0x04,
51-
angle_nocascade = 0x05 //!< Position/angle motion control without velocity cascade
51+
angle_nocascade = 0x05, //!< Position/angle motion control without velocity cascade
52+
custom = 0x06 //!< Custom control method - control method added by user
5253
};
5354

5455
/**
@@ -367,6 +368,16 @@ class FOCMotor
367368
*/
368369
float angleOpenloop(float target_angle);
369370

371+
372+
/**
373+
* Function setting a custom motion control method defined by the user
374+
* @note the custom control method has to be defined by the user and should follow the signature: float controlMethod(FOCMotor* motor, float target)
375+
* @param controlMethod - pointer to the custom control method function defined by the user
376+
*/
377+
void setCustomMotionControlMethod(float (*controlMethod)(FOCMotor* motor, float target)){
378+
customMotionControlCallback = controlMethod;
379+
}
380+
370381
protected:
371382

372383
/**
@@ -410,6 +421,8 @@ class FOCMotor
410421
// open loop variables
411422
uint32_t open_loop_timestamp;
412423

424+
// function pointer for custom control method
425+
float (*customMotionControlCallback)(FOCMotor* motor, float target) = nullptr;
413426

414427
};
415428

src/communication/Commander.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ void Commander::motion(FOCMotor* motor, char* user_cmd, char* separator){
439439
break;
440440
default:
441441
// change control type
442-
if(!GET && value >= 0 && (int)value < 6) // if set command
442+
if(!GET && value >= 0 && (int)value < 7) // if set command
443443
motor->updateMotionControlType((MotionControlType)value); // update motion control type
444444
switch(motor->controller){
445445
case MotionControlType::torque:
@@ -460,14 +460,17 @@ void Commander::motion(FOCMotor* motor, char* user_cmd, char* separator){
460460
case MotionControlType::angle_nocascade:
461461
println(F("angle nocascade"));
462462
break;
463+
case MotionControlType::custom:
464+
println(F("custom"));
465+
break;
463466
}
464467
break;
465468
}
466469
break;
467470
case CMD_TORQUE_TYPE:
468471
// change control type
469472
printVerbose(F("Torque: "));
470-
if(!GET && (int8_t)value >= 0 && (int8_t)value < 4)// if set command
473+
if(!GET && (int8_t)value >= 0 && (int8_t)value < 4) // if set command
471474
motor->updateTorqueControlType((TorqueControlType)value); // update torque control type
472475
switch(motor->torque_controller){
473476
case TorqueControlType::voltage:

0 commit comments

Comments
 (0)