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

Add PID control #2

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
5462dae
f(pid_for_speed_control): Add PID code
kimanimichael May 13, 2024
671fd57
f(scale_certification): Set raw motor speed
kimanimichael May 26, 2024
5cabb77
f(scale_certification): Use extern in the header file
kimanimichael May 26, 2024
fa802de
f(scale_certification): Use _ for class members
kimanimichael May 26, 2024
1f9a6e8
f(scale_certification): Use _ for class members and initialize them i…
kimanimichael May 26, 2024
068ed05
f(scale_certification): Add set raw speed motor functions to driver f…
kimanimichael May 26, 2024
e4f0ae9
f(scale_certification): Check the status of the car and calculate the…
kimanimichael May 26, 2024
9ee06de
f(pid_for_speed_control): Vary motor speed wrt to the PID output
kimanimichael May 26, 2024
c203cc1
f(pid_for_speed_control): Add PID app and controller files to CMakeLists
kimanimichael May 26, 2024
136c00f
f(scale_certification): Track previous output to discard huge changes…
kimanimichael Jun 7, 2024
c9a1151
f(f/pid_for_speed_control): Make control kinematics return a value
kimanimichael Jun 7, 2024
c4c919a
f(f/pid_for_speed_control): Tune PID on bench
kimanimichael Jun 7, 2024
d7a56e9
f(f/pid_for_speed_control): Add function to validate motor speed and …
kimanimichael Jun 7, 2024
95eebb9
f(f/pid_for_speed_control): Clamp integral and PID output
kimanimichael Jun 7, 2024
13201ba
f(f/pid_for_speed_control): Set motor speeds based on PID output
kimanimichael Jun 7, 2024
d0eb600
f(f/pid_for_speed_control): Change to use PID source files for drive
kimanimichael Jun 7, 2024
52f08f4
f(pid_for_speed_control): Remove integral component
kimanimichael Jul 10, 2024
03282ea
f(pid_for_speed_control): Vary min and max speeds
kimanimichael Jul 10, 2024
ceac1c1
f(pid_for_speed_control): Vary pid gains
kimanimichael Jul 10, 2024
0ece094
f(pid_for_speed_control): Vary left and right motor base speeds to ov…
kimanimichael Jul 10, 2024
1a809ff
f(pid_for_speed_control): Ignore clion cmake files
kimanimichael Jul 10, 2024
a1dd0d4
f(pid_for_speed_control): Tweak params. PC switch lol
kimanimichael Aug 7, 2024
9418891
f(pid_for_speed_control): Add missing interfaces to mbed dc_motor
kimanimichael Nov 30, 2024
de32a6d
f(pid_for_speed_control): Customize pwm values for mbed pwm driver
kimanimichael Nov 30, 2024
0b63d54
f(pid_for_speed_control): Change of folders due to rebase
kimanimichael Nov 30, 2024
f46d1d2
f(pid_for_speed_control): Suppress cmake warning
kimanimichael Nov 30, 2024
bbcf6f3
f(pid_for_speed_control): Remove max PID checks
kimanimichael Dec 25, 2024
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ build /
cmake-build-debug-gcc_arm/
cmake_build/
cmake_build_clion/
cmake_build/
cmake-build-debug/
9 changes: 7 additions & 2 deletions cmake/app.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ set(APP_COMMON_INCLUDE_DIRS
"${CMAKE_SOURCE_DIR}/include/bsp/drivers/actuators"
"${CMAKE_SOURCE_DIR}/include/bsp/drivers/sensors"

"${CMAKE_SOURCE_DIR}/include/math_utils/pid"

"${CMAKE_SOURCE_DIR}/include/apps/car_app"

"${CMAKE_SOURCE_DIR}/include/services/kinematics"
Expand All @@ -17,7 +19,10 @@ set(APP_COMMON_INCLUDE_DIRS

set(APP_COMMON_SOURCES
"${CMAKE_SOURCE_DIR}/src/RB19.cpp"
"${CMAKE_SOURCE_DIR}/src/apps/car_app/car_line_following.cpp"
"${CMAKE_SOURCE_DIR}/src/services/kinematics.cpp"
"${CMAKE_SOURCE_DIR}/src/math_utils/pid/pid.cpp"
# "${CMAKE_SOURCE_DIR}/src/apps/car_app/car_line_following.cpp"
"${CMAKE_SOURCE_DIR}/src/apps/car_app/car_line_following_pid.cpp"
# "${CMAKE_SOURCE_DIR}/src/services/kinematics.cpp"
"${CMAKE_SOURCE_DIR}/src/services/kinematics_pid.cpp"
"${CMAKE_SOURCE_DIR}/src/services/steering.cpp"
)
15 changes: 15 additions & 0 deletions include/apps/car_app/car_line_following_pid.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef __CAR_LF__PID
#define __CAR_LF__PID

#include "pid.h"
#include "bsp.h"

extern double PIDOutput;

extern int cornerSpeedDiff;

void carAppInit();

void carAppRun();

#endif
5 changes: 5 additions & 0 deletions include/bsp/drivers/actuators/bsp_dc_motor.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ class DcMotor {
* @author @MK
*/
virtual void set_speed(BSP::motorSpeeds speed) = 0;

/**
* @brief Sets a motor's raw speed
*/
virtual void set_raw_speed(unsigned int speed) = 0;
};

}
Expand Down
33 changes: 33 additions & 0 deletions include/math_utils/pid/pid.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef __PID__
#define __PID__
class PID
{

public:
PID(double kp, double ki, double kd);
// ~pid();
public:

double output;
double _dt;

double calculateOutput(double setPoint, double processVariable);

private:
/* data */
double _setPoint;
double _processVariable;
double _error;
double _previousError;
double _integral;
double _derivative;
double _kp;
double _ki;
double _kd;
double _previousOutput;

};

#endif


3 changes: 2 additions & 1 deletion include/services/kinematics/kinematics.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include "bsp_dc_motor.h"


enum carKinematicsStates {
idling,
drivingLeft,
Expand All @@ -17,6 +16,8 @@ enum carKinematicsStates {
reversingRight
};

extern carKinematicsStates RB19Kinematics;


/**
* @brief Control vehicular kinematics: braking and accelerating
Expand Down
12 changes: 12 additions & 0 deletions include/services/kinematics/kinematics_pid.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef __KINEMATICS_PID__
#define __KINEMATICS_PID__

#include "car_line_following_pid.h"

/**
* @brief Control vehicular kinematics: braking and accelerating
*/
double controlCarKinematics();


#endif
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ class ESP32DCMotorFtSmc : public BSP::DcMotor {
void run() override;

void stop() override;

/**
* @brief Sets a motor's raw speed
*/
void set_raw_speed(unsigned int speed) override;

/**
* @brief Validate the set motor speed value
*/
unsigned int validate_motor_speed(unsigned int speed);
private:
gpio_num_t _signal;
unsigned int _pwmDuty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ class ESP32DcMotorL298N: public BSP::DcMotor {
*/
void set_speed(BSP::motorSpeeds speed) override;

/**
* @brief Sets a motor's raw speed
*/
void set_raw_speed(unsigned int speed) override;



};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void ESP32DCMotorFtSmc::init() {
_clockWiseSpeedMap[BSP::motorSpeeds::ZERO] = 77;
_clockWiseSpeedMap[BSP::motorSpeeds::SUPERSLOW] = 78;
_clockWiseSpeedMap[BSP::motorSpeeds::LOW] = 79;
_clockWiseSpeedMap[BSP::motorSpeeds::INTERMEDIATE] = 80;
_clockWiseSpeedMap[BSP::motorSpeeds::INTERMEDIATE] = 81;
_clockWiseSpeedMap[BSP::motorSpeeds::MEDIUM] = 93;
_clockWiseSpeedMap[BSP::motorSpeeds::HIGH] = 101;

Expand All @@ -30,7 +30,7 @@ void ESP32DCMotorFtSmc::init() {
void ESP32DCMotorFtSmc::configure_pwm(gpio_num_t signal) {
ledc_timer_config_t pwmTimer = {
.speed_mode = LEDC_LOW_SPEED_MODE,
.duty_resolution = LEDC_TIMER_10_BIT,
.duty_resolution = LEDC_TIMER_20_BIT,
.timer_num = LEDC_TIMER_0,
.freq_hz = 50,
.clk_cfg = LEDC_AUTO_CLK
Expand Down Expand Up @@ -121,6 +121,60 @@ void ESP32DCMotorFtSmc::set_speed(BSP::motorSpeeds speed) {
printf("Motor speed set to %u\n", _pwmDuty);
}

void ESP32DCMotorFtSmc::set_raw_speed(unsigned int speed) {
_pwmDuty = validate_motor_speed(speed);
ledc_set_duty(LEDC_LOW_SPEED_MODE, _motorChannel, _pwmDuty);
ledc_update_duty(LEDC_LOW_SPEED_MODE, _motorChannel);
// printf("Motor speed set to %u\n", _pwmDuty);
}
#define MAX_CW_PWM_VALUE 88000
#define MIN_CW_PWM_VALUE 80000

#define MAX_CCW_PWM_VALUE 77
#define MIN_CCW_PWM_VALUE 73

unsigned int ESP32DCMotorFtSmc::validate_motor_speed(unsigned int speed) {
unsigned int validatedSpeed;
switch (_direction)
{
case BSP::motorDirections::CLOCKWISE:
/* code */
if(speed > MAX_CW_PWM_VALUE) {
validatedSpeed = MAX_CW_PWM_VALUE;
return validatedSpeed;
}
else if (speed < MIN_CW_PWM_VALUE) {
validatedSpeed = MIN_CW_PWM_VALUE;
return validatedSpeed;
}
else {
validatedSpeed = speed;
return validatedSpeed;
}
break;
case BSP::motorDirections::COUNTERCLOCKWISE:
/* code */
if(speed > MAX_CCW_PWM_VALUE) {
validatedSpeed = MAX_CCW_PWM_VALUE;
return validatedSpeed;
}
else if (speed < MIN_CCW_PWM_VALUE) {
validatedSpeed = MIN_CCW_PWM_VALUE;
return validatedSpeed;
}
else {
validatedSpeed = speed;
return validatedSpeed;
}
break;

default:
validatedSpeed = 0;
return validatedSpeed;
break;
}
}

void ESP32DCMotorFtSmc::run() {

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ void ESP32DcMotorL298N::stop() {

void ESP32DcMotorL298N::set_speed(BSP::motorSpeeds speed) {

}

void ESP32DcMotorL298N::set_raw_speed(unsigned int speed) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ class MbedDCMotor: public BSP::DcMotor {

void stop() override;

/**
* @brief Validate the set motor speed value
*/
float validate_motor_speed(unsigned int speed);

/**
* @brief Sets a motor's raw speed
*/
void set_raw_speed(unsigned int speed) override;

private:
PwmOut _motor_signal_pin;
BSP::motorDirections _direction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,59 @@ void MbedDCMotor::set_speed(const BSP::motorSpeeds speed) {
printf("Motor speed set to %.2f\n", _pwmDuty);
}

#define MAX_CW_PWM_VALUE 88000
#define MIN_CW_PWM_VALUE 80000

#define MAX_CCW_PWM_VALUE 77
#define MIN_CCW_PWM_VALUE 73

float MbedDCMotor::validate_motor_speed(unsigned int speed) {
float validatedSpeed;
switch (_direction)
{
case BSP::motorDirections::CLOCKWISE:
/* code */
if(speed > MAX_CW_PWM_VALUE) {
validatedSpeed = MAX_CW_PWM_VALUE;
return validatedSpeed;
}
else if (speed < MIN_CW_PWM_VALUE) {
validatedSpeed = MIN_CW_PWM_VALUE;
return validatedSpeed;
}
else {
validatedSpeed = static_cast<float>(speed);
return validatedSpeed;
}
break;
case BSP::motorDirections::COUNTERCLOCKWISE:
/* code */
if(speed > MAX_CCW_PWM_VALUE) {
validatedSpeed = MAX_CCW_PWM_VALUE;
return validatedSpeed;
}
else if (speed < MIN_CCW_PWM_VALUE) {
validatedSpeed = MIN_CCW_PWM_VALUE;
return validatedSpeed;
}
else {
validatedSpeed = static_cast<float>(speed);
return validatedSpeed;
}
break;

default:
validatedSpeed = 0;
return validatedSpeed/1048576.0f;
break;
}
}

void MbedDCMotor::set_raw_speed(unsigned int speed) {
_pwmDuty = validate_motor_speed(speed);
// printf("Motor speed set to %u\n", _pwmDuty);
}

void MbedDCMotor::run() {
_motor_signal_pin.write(_pwmDuty);
}
Expand Down
11 changes: 7 additions & 4 deletions src/RB19.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include <stdio.h>
#include "bsp.h"
#include "car.h"
#include "car_line_following.h"
#include "kinematics.h"
#include "car_line_following_pid.h"
#include "kinematics_pid.h"
#include "steering.h"
// #include <freertos/FreeRTOS.h>
// #include <freertos/task.h>
Expand All @@ -24,15 +24,18 @@ int main()
// steerCar();
// controlCarKinematics();
// }
BSP::BSP_Delay(10000);
BSP::BSP_Delay(20000);
BSP::BSPInit();
carAppInit();
// steerCar();
controlCarKinematics();
double carPIDOutput;
while(1) {
carAppRun();
// steerCar();
controlCarKinematics();

carPIDOutput = controlCarKinematics();
printf("PID Output: %.2f\n", carPIDOutput);
BSP::BSP_Delay(100);
// vTaskDelay(500/portTICK_PERIOD_MS);
}
Expand Down
1 change: 0 additions & 1 deletion src/apps/car_app/car_line_following.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ Infrared5Channel* infraredSensor;
Infrared5Channel::detectedChannels sensorDetectedChannels;

carSteerStates RB19Steer;
carKinematicsStates RB19Kinematics;

void carAppInit() {
RB19Kinematics = idling;
Expand Down
Loading