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

feat(monitor): reduce motor.monitor() to send every MONITOR_WAIT_MS #13

Merged
merged 1 commit into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ dkms.conf
.clang_complete
.gcc-flags.json
.pio
compile_commands.json

### VisualStudioCode ###
.vscode/*
Expand Down
20 changes: 14 additions & 6 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

#define _MON_ALL (0b1111111)
#define POLE_PAIRS (7)
#define MONITOR_WAIT_MS (50)

uint32_t last_monitor_ms = 0;

// BLDC motor & driver instance
BLDCMotor motor = BLDCMotor(POLE_PAIRS);
Expand All @@ -33,7 +36,6 @@ void doTarget(char* cmd) {

void setup() {
Serial.begin(921600);
delay(2000);

// initialize hall sensor hardware
sensor.init();
Expand Down Expand Up @@ -83,6 +85,7 @@ void setup() {
motor.velocity_limit = 4;

// use monitoring with serial
motor.monitor_downsample = 1;
motor.monitor_separator = '\t';
motor.monitor_decimals = 2;
motor.monitor_variables = _MON_ALL;
Expand All @@ -98,13 +101,15 @@ void setup() {

Serial.println(F("Motor ready."));
Serial.println(F("Set the target angle using serial terminal:"));

last_monitor_ms = millis();
}

void loop() {
// main FOC algorithm function
// the faster you run this function the better
// Arduino UNO loop ~1kHz
// Bluepill loop ~10kHz
// Arduino UNO loop ~1kHz (16 Mhz)
// Bluepill loop ~10kHz (72Mhz)
motor.loopFOC();

// Motion control function
Expand All @@ -113,9 +118,12 @@ void loop() {
// You can also use motor.move() and set the motor.target in the code
motor.move(target_angle);

// function intended to be used with serial plotter to monitor motor variables
// significantly slowing the execution down!!!!
motor.monitor();
if (millis() - last_monitor_ms > MONITOR_WAIT_MS) {
last_monitor_ms = millis();
// function intended to be used with serial plotter to monitor motor variables
// significantly slowing the execution down!!!!
motor.monitor();
}
Comment on lines +121 to +126
Copy link

Choose a reason for hiding this comment

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

The conditional check and update of last_monitor_ms in the loop() function are correctly implemented to ensure monitoring data is sent at intervals defined by MONITOR_WAIT_MS. However, consider extracting the monitoring logic into a separate function for better modularity and readability.

void monitorMotor() {
    if (millis() - last_monitor_ms > MONITOR_WAIT_MS) {
        last_monitor_ms = millis();
        motor.monitor();
    }
}

void loop() {
    motor.loopFOC();
    motor.move(target_angle);
+   monitorMotor();
    command.run();
}

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
if (millis() - last_monitor_ms > MONITOR_WAIT_MS) {
last_monitor_ms = millis();
// function intended to be used with serial plotter to monitor motor variables
// significantly slowing the execution down!!!!
motor.monitor();
}
void monitorMotor() {
if (millis() - last_monitor_ms > MONITOR_WAIT_MS) {
last_monitor_ms = millis();
motor.monitor();
}
}
void loop() {
motor.loopFOC();
motor.move(target_angle);
monitorMotor();
command.run();
}


// user communication
command.run();
Expand Down
Loading