Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added: radians cmd #17

Merged
merged 4 commits into from
Feb 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link

Choose a reason for hiding this comment

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

The call to checkRC(rc) after initializing the driver is a good practice for error handling. It ensures that any issues with the driver initialization are caught early. However, it would be beneficial to add comments explaining the expected range of return codes from driver.init() and what conditions might lead to a failure, for better maintainability and clarity for future developers.

Consider adding comments detailing the expected return codes from driver.init() and the conditions that might lead to initialization failure.

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);
Copy link

Choose a reason for hiding this comment

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

Similar to the previous comment on driver.init(), the use of checkRC(rc) after initializing the current sensor is a good error handling practice. Including comments about the expected return codes from current_sensor.init() would enhance code readability and maintainability.

It would be helpful to include comments explaining the expected return codes from current_sensor.init() and under what conditions initialization might fail.

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
Loading