Skip to content

Commit

Permalink
added: radians cmd (#17)
Browse files Browse the repository at this point in the history
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
	- Introduced options to set motor targets in both radians and degrees.
	- Added a delay feature for monitoring motor performance.
- **Improvements**
	- Enhanced motor limit settings for better performance.
- Improved initial setup for motor, driver, and current sensor
configurations.
- **Refactor**
	- Renamed target setting function for clarity.
- **Chores**
	- Adjusted serial communication commands for setting motor targets.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
hcd-bdltd authored Feb 20, 2024
2 parents 70cbf17 + 56c057b commit 2f22784
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ void doA(){ hall_sensor.handleA(); }
void doB(){ hall_sensor.handleB(); }
void doC(){ hall_sensor.handleC(); }

// monitor delay
#define MONITOR_DELAY_MS (20)
unsigned long monitor_delay = 0;
// angle set point variable
float target = 0.0f;
// instantiate the commander
Commander commander = Commander(Serial);
void doMotor(char* cmd) { commander.motor(&motor, cmd); }
void doTarget(char* cmd) {
void doRadians(char* cmd) { commander.scalar(&target, cmd); };
void doDegrees(char* cmd) {
float angle_degrees;
commander.scalar(&angle_degrees, cmd);
target = angle_degrees * (PI / 180.0);
Expand Down Expand Up @@ -51,7 +55,8 @@ void setup() {
// initialize the driver
driver.voltage_power_supply = 24.0f;
rc = driver.init();
checkRC(rc); SIMPLEFOC_DEBUG("Driver ready.");
checkRC(rc);
SIMPLEFOC_DEBUG("Driver ready.");
// link the current sensor and the driver
current_sensor.linkDriver(&driver);
// link the motor and the driver
Expand All @@ -60,7 +65,8 @@ void setup() {
// initialize the current sensor
current_sensor.skip_align = true;
rc = current_sensor.init();
checkRC(rc); SIMPLEFOC_DEBUG("Current sensor ready.");
checkRC(rc);
SIMPLEFOC_DEBUG("Current sensor ready.");
// link the current sensor to the motor
motor.linkCurrentSense(&current_sensor);

Expand All @@ -84,13 +90,13 @@ void setup() {
motor.P_angle.P = 20.0f;

// motor limits
motor.voltage_limit = 12.0f;
motor.velocity_limit = 4.0f;
motor.current_limit = 2.0f;
motor.voltage_limit = 24.0f;
motor.velocity_limit = 200.0f;
motor.current_limit = 1.0f;

// motion and monitoring settings
motor.motion_downsample = 0;
motor.monitor_downsample = 10000;
motor.monitor_downsample = 1;

// use monitoring with serial
motor.monitor_separator = '\t';
Expand All @@ -102,16 +108,19 @@ void setup() {
motor.init();
// align sensor and start FOC
rc = motor.initFOC();
checkRC(rc); SIMPLEFOC_DEBUG("Motor ready.");
checkRC(rc);
SIMPLEFOC_DEBUG("Motor ready.");

// initialize serial communication
commander.verbose = VerboseMode::user_friendly;
commander.add('M',doMotor,"motor");
commander.add('T', doTarget, "target [deg]");
commander.add('R', doRadians, "target [rad]");
commander.add('D', doDegrees, "target [deg]");

SIMPLEFOC_DEBUG("Setup ready.");
SIMPLEFOC_DEBUG("Set the target angle using serial terminal:");
_delay(1000);
monitor_delay = millis();
}

void loop() {
Expand All @@ -127,9 +136,12 @@ void loop() {
// You can also use motor.move() and set the motor.target in the code
motor.move(target);

// function intended to be used with serial plotter to monitor motor variables
// significantly slowing the execution down!!!!
motor.monitor();
if (millis() - monitor_delay > MONITOR_DELAY_MS) {
monitor_delay = millis();
// function intended to be used with serial plotter to monitor motor variables
// significantly slowing the execution down!!!!
motor.monitor();
}

// user communication
commander.run();
Expand Down

0 comments on commit 2f22784

Please sign in to comment.