Skip to content

Commit 1ebdcad

Browse files
committed
Issue #14-2: Adding PIDRegulator.cpp
1 parent 453020f commit 1ebdcad

File tree

9 files changed

+136
-12
lines changed

9 files changed

+136
-12
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#ifndef PIDREGULATOR_H
2+
#define PIDREGULATOR_H
3+
4+
#pragma once
5+
6+
#include <chrono>
7+
#include <array>
8+
9+
#define SECONDSTOMINUTE 60
10+
#define MILISECONDSTOSECOND 1000
11+
#define SPEEDINDEXMULTIPLIER 100
12+
13+
class PIDRegulator {
14+
public:
15+
// PIDRegulator(vector<float> _coeficients);
16+
void Init(std::array<float, 3> _coeficients, int _loopsTicks, int _maxRPM);
17+
int Run(int setpoint, int ticksValue);
18+
19+
private:
20+
float kp_;
21+
float ki_;
22+
float kd_;
23+
int previousError_ = 0;
24+
int integral_ = 0;
25+
const int integralLimit_ = 26500;
26+
const int outputLimit_ = 26500;
27+
int loopsTicks_;
28+
int maxRPM_;
29+
30+
std::chrono::time_point<std::chrono::high_resolution_clock> lastTimePoint_;
31+
};
32+
33+
#endif

src/kpi_rover_ecu/include/motor.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
#ifndef MOTOR_H
22
#define MOTOR_H
33

4+
#include <array>
5+
#include "PIDRegulator.h"
6+
47
class Motor {
58
public:
69
static constexpr int kMinRpm = 8000;
710
static constexpr int kMaxRpm = 26500;
11+
static constexpr int kLoopTicks = 821;
812

9-
Motor(int assigned_number, bool is_inverted);
13+
Motor(int assigned_number, bool is_inverted, std::array<float, 3> _coeficients);
1014
int MotorGo(int newRPM);
1115
int MotorStop() const;
12-
int GetEncoderCounter() const;
16+
int GetEncoderCounter();
1317

1418
private:
19+
int MotorSet(int inputRPM);
1520
static constexpr int kBrakeTime = 100000; // 100 ms
1621

22+
PIDRegulator pidRegulator_;
23+
int currentRpm_;
1724
int motorNumber_;
1825
bool inverted_;
1926
double currentDutyCycle_;
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
#ifndef MOTOR_CONFIG_H
22
#define MOTOR_CONFIG_H
33

4+
#include <array>
45
#include <cstdint>
56

67
class MotorConfig {
78
public:
8-
MotorConfig(uint8_t assignedNumber, bool invertedStatus);
9+
MotorConfig(uint8_t assignedNumber, bool invertedStatus, const std::array<float, 3>& _coefs);
910

11+
std::array<float, 3> GetPidCoefs() const;
1012
uint8_t GetNumber() const;
1113

1214
bool IsInverted() const;
1315

1416
private:
1517
uint8_t number_;
1618
bool inverted_;
19+
std::array<float, 3> pidCoefs_ = {};
1720
};
1821

1922
#endif // MOTOR_CONFIG_H

src/kpi_rover_ecu/src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ add_library(kpi_rover_ecu_core STATIC
66
TCPTransport.cpp
77
motorConfig.cpp
88
messageQueue.cpp
9+
PIDRegulator.cpp
910
)
1011

1112
# Include public headers
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include "PIDRegulator.h"
2+
3+
#include <array>
4+
#include <chrono>
5+
#include <cmath>
6+
#include <iostream>
7+
#include <vector>
8+
9+
void PIDRegulator::Init(std::array<float, 3> _coeficients, int _loopsTicks, int _maxRPM) {
10+
kp_ = _coeficients[0];
11+
ki_ = _coeficients[1];
12+
kd_ = _coeficients[2];
13+
14+
loopsTicks_ = _loopsTicks;
15+
maxRPM_ = _maxRPM;
16+
lastTimePoint_ = std::chrono::high_resolution_clock::now();
17+
// cout << "PID regulator initialized" << '\n';
18+
}
19+
20+
int PIDRegulator::Run(int setpoint, int ticks_value) {
21+
const auto kCurrentTimePoint = std::chrono::high_resolution_clock::now();
22+
const std::chrono::duration<double, std::milli> kElapsedMilliseconds =
23+
kCurrentTimePoint - lastTimePoint_;
24+
lastTimePoint_ = kCurrentTimePoint;
25+
const auto kTimeDt = static_cast<float>(kElapsedMilliseconds.count());
26+
27+
const float kRevolutions = static_cast<float>(ticks_value) / static_cast<float>(loopsTicks_);
28+
29+
const float kInputPoint = static_cast<float>(
30+
std::round((kRevolutions * SECONDSTOMINUTE * MILISECONDSTOSECOND) / kTimeDt)) *
31+
SPEEDINDEXMULTIPLIER;
32+
const float kError = static_cast<float>(setpoint) - kInputPoint; // get rid of abs() !!!
33+
std::cout << "set point " << setpoint << " current point " << kInputPoint << " error " << kError << '\n';
34+
35+
const float kPTerm = kp_ * kError;
36+
37+
integral_ += kError * kTimeDt;
38+
if (integral_ > integralLimit_) {
39+
integral_ = integralLimit_;
40+
}
41+
if (integral_ < -integralLimit_) {
42+
integral_ = -integralLimit_;
43+
}
44+
45+
const float kITerm = ki_ * integral_;
46+
47+
const float kDerivativePart = (kTimeDt > 0) ? (kError - previousError_) / kTimeDt : 0.0F;
48+
const float kDTerm = kd_ * kDerivativePart;
49+
50+
previousError_ = kError;
51+
52+
return static_cast<int>(kPTerm + kITerm + kDTerm);
53+
}

src/kpi_rover_ecu/src/main.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,12 @@ int main(int argc, char* argv[]) {
5454

5555
MotorController motors_processor;
5656
const uint8_t kMotorNumber = 4;
57-
const std::vector<MotorConfig> kShassisVector = {MotorConfig(3, false), MotorConfig(4, false), MotorConfig(1, true),
58-
MotorConfig(2, true)};
57+
const std::vector<MotorConfig> kShassisVector = {
58+
MotorConfig(3, false, {1.5, 0.056, 1.5}),
59+
MotorConfig(4, false, {1.5, 0.056, 1.5}),
60+
MotorConfig(1, true, {1.5, 0.056, 1.5}),
61+
MotorConfig(2, true, {1.5, 0.056, 1.5})
62+
};
5963

6064
motors_processor.Init(kShassisVector, kMotorNumber);
6165
ProtocolHanlder protocol_handler(&motors_processor);

src/kpi_rover_ecu/src/motor.cpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
#include "motor.h"
2+
#include "PIDRegulator.h"
23

34
#include <rc/encoder.h>
45
#include <rc/motor.h>
56

67
#include <iostream>
78

8-
Motor::Motor(int assigned_number, bool is_inverted)
9-
: motorNumber_(assigned_number), inverted_(is_inverted), currentDutyCycle_(0) {}
9+
Motor::Motor(int assigned_number, bool is_inverted, std::array<float, 3> _coeficients)
10+
: motorNumber_(assigned_number), inverted_(is_inverted), currentDutyCycle_(0), currentRpm_(0) {
11+
pidRegulator_.Init(_coeficients, kLoopTicks, kMaxRpm);
12+
}
1013

1114
int Motor::MotorGo(int newRPM) {
1215
if (newRPM > kMaxRpm) {
@@ -19,7 +22,17 @@ int Motor::MotorGo(int newRPM) {
1922
newRPM = -kMaxRpm;
2023
}
2124

22-
currentDutyCycle_ = GetDC(newRPM);
25+
26+
if (MotorSet(newRPM) == -1) {
27+
return -1;
28+
}
29+
30+
currentRpm_ = newRPM;
31+
return 0;
32+
}
33+
34+
int Motor::MotorSet(int inputRPM) {
35+
currentDutyCycle_ = GetDC(inputRPM);
2336
if (inverted_) {
2437
currentDutyCycle_ = -currentDutyCycle_;
2538
}
@@ -41,9 +54,17 @@ int Motor::MotorStop() const {
4154
return 0;
4255
}
4356

44-
int Motor::GetEncoderCounter() const {
57+
int Motor::GetEncoderCounter() {
4558
const int kEncoderTicks = rc_encoder_read(motorNumber_);
59+
int pid_encoder_ticks = kEncoderTicks;
60+
if (!inverted_) {
61+
pid_encoder_ticks *= -1;
62+
}
63+
64+
const int kPidOutput = pidRegulator_.Run(currentRpm_, pid_encoder_ticks);
4665
rc_encoder_write(motorNumber_, 0);
66+
67+
MotorSet(currentRpm_ + kPidOutput);
4768
return kEncoderTicks;
4869
}
4970

src/kpi_rover_ecu/src/motorConfig.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
#include <cstdint>
44

5-
MotorConfig::MotorConfig(uint8_t assignedNumber, bool invertedStatus)
6-
: number_(assignedNumber), inverted_(invertedStatus) {}
5+
MotorConfig::MotorConfig(uint8_t assignedNumber, bool invertedStatus, const std::array<float, 3>& _coefs)
6+
: number_(assignedNumber), inverted_(invertedStatus), pidCoefs_(_coefs) {}
7+
8+
std::array<float, 3> MotorConfig::GetPidCoefs() const { return pidCoefs_; }
79

810
uint8_t MotorConfig::GetNumber() const { return number_; }
911

src/kpi_rover_ecu/src/motorsController.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ int MotorController::Init(const std::vector<MotorConfig>& _motors, uint8_t _moto
4040
motor_number_ = _motorNumber;
4141

4242
for (int i = 0; i < motor_number_; ++i) {
43-
motors_.emplace_back(_motors[i].GetNumber(), _motors[i].IsInverted());
43+
motors_.emplace_back(_motors[i].GetNumber(), _motors[i].IsInverted(), _motors[i].GetPidCoefs());
4444
}
4545

4646
return 0;

0 commit comments

Comments
 (0)