I have this code running in STM32F469 DISCO KIT: https://github.com/neuberfran/discovery7/blob/main/applications/lvgl/demo/ui/ui_events.cpp
The lib SmartDrive Makes the motor run for 5 seconds (by default). When I want to stop the motor before that time, I click the stop button it works ok. Or else the motor stops by itself in 5s
But, when I want the motor to run for more than 5 seconds I need to put this routine: smd.Run_Unlimited(SmartDrive_Motor_ID_1, SmartDrive_Dir_Reverse, 90); in While - Loop.
At that moment my issue enters because I am not able to stop the motor through the code below. Does anyone have any tips on how to resolve this? Would I have to use interrupt? Would I have to use another task? How to use?
New ui_events.cpp File with issue:
#include <Arduino.h>
#include "ui.h"
#include <SmartDrive.h>
SmartDrive smd = SmartDrive(SmartDrive_DefaultAddress);
bool STOP01 = true;
#define STOP02 0
void run01right(lv_event_t * e)
{
// Your code here
while (STOP01)
{
smd.Run_Unlimited(SmartDrive_Motor_ID_1, SmartDrive_Dir_Reverse, 90);
}
STOP01 = true;
}
void stopmotor01(lv_event_t * e)
{
// Your code here
STOP01 = false;
smd.StopMotor(SmartDrive_Motor_ID_1, SmartDrive_Action_Brake);
STOP01 = true;
}
void run01left(lv_event_t * e)
{
// Your code here
while (STOP01)
{
smd.Run_Unlimited(SmartDrive_Motor_ID_1, SmartDrive_Dir_Forward, 90);
}
STOP01 = true;
}
Note: As the First photo below, it is not possible to implement void loop in this case. 2022-09-09 <-> I was wrong. It is possible to implement void setup() and void loop() and it was done. And so it resolved
In the Arduino Sketch setup()
/loop()
framework it is ill advised to have "busy-loops" withing loop()
. Instread you should us eretained state information to determine what should happen for each loop()
iteration. The real-time behaviour and responsiveness to external events will then be more deterministic.
So you might have:
enum
{
RUN_LEFT,
RUN_RIGHT,
STOP
} motor_state_01, motor_state_02 ;
void run01left(lv_event_t * e)
{
motor_state_01 = RUN_LEFT ;
}
void run01right(lv_event_t * e)
{
motor_state_01 = RUN_RIGHT ;
}
void stopmotor01(lv_event_t * e)
{
motor_state_01 = STOP ;
}
void loop()
{
switch( motor_state_01 )
{
case RUN_RIGHT :
{
smd.Run_Unlimited( SmartDrive_Motor_ID_1,
SmartDrive_Dir_Reverse, 90);
}
break ;
case RUN_RIGHT :
{
smd.Run_Unlimited( SmartDrive_Motor_ID_1,
SmartDrive_Dir_Forward, 90);
}
break ;
case STOP :
{
smd.StopMotor( SmartDrive_Motor_ID_1,
SmartDrive_Action_Brake );
}
break ;
}
}
So that now you have a single non-blocking loop where the actions in each iteration depend on the state information set by the UI event handlers. The UI event handlers themselves do no "busy" work to ensure both the responsiveness of the UI and real-time control of the motor.