-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasses.cpp
More file actions
177 lines (161 loc) · 4.59 KB
/
classes.cpp
File metadata and controls
177 lines (161 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <iostream>
#include "classes.h"
#include <cstdint>
#include <array>
#include <memory>
using namespace std;
//ControlInput
ControlInput::ControlInput(){
cout << "Input is working.\n";
}
int ControlInput::getter() {
return input;
}
void ControlInput::setter(int set) {
input = set;
}
//ControlOutput
ControlOutput::ControlOutput() {
cout << "OutPut is working.\n";
}
int ControlOutput::getter() {
return Output;
}
void ControlOutput::setter(int set) {
Output = set;
}
//VehicleState
VehicleState::VehicleState(): throttle(0), brake(0), steering(0) {
cout << "VehicleState is working.\n";
}
void VehicleState::setThrottle(float t){
throttle = t;
}
void VehicleState::setBrake(float b){
brake = b;
}
void VehicleState::setSteering(float s){
steering = s;
}
float VehicleState::getBrake() {
return brake;
}
float VehicleState::getThrottle() {
return throttle;
}
float VehicleState::getSteering() {
return steering;
}
//VehicleController
VehicleController::VehicleController() {
}
void VehicleController::update(){
}
VehicleState VehicleController::getVehicleState() {
return state;
}
//PID Controller
PIDController::PIDController(double Kp, double Ki, double Kd, double Dt): Kp(Kp),Ki(Ki),Kd(Kd),Dt(Dt),integral(0.0),previous_error(0.0) {
}
double PIDController::compute(double target, double current) {
//error ammount
double error = target - current;
//Proportional term
double p_term = Kp *error;
//integral term
integral += error * Dt;
double i_term = integral * Ki;
//Derivative term
double derivative = (error - previous_error) / Dt;
double d_term = Kd*derivative;
//updating the state of the error;
previous_error = error;
//Output storing to variable
double output = p_term + i_term + d_term;
// Define your limits
const double output_min = 0.0;
const double output_max = 200.0;
// Clamp the output
if (output > output_max) {
output = output_max;
integral = (output - p_term - d_term) / Ki; // Force integral to value that would produce max output
}
else if (output < output_min) {
output = output_min;
integral = (output - p_term - d_term) / Ki;
}
//output
return output;
}
//CANBUS
uint8_t CanManeger::calculateChecksum(uint8_t* data, uint32_t len) {
uint8_t checkSum = 0;
for(int i =0 ; i < len ;i++) {
checkSum ^= data[i];
}
return checkSum;
}
void CanManeger::packingCommandControl(float& throttle, float& brake, float& steering, CanFrame& frame) {
//Data infos
frame.dlc = 8;
frame.id = 0x201;// exp: 0x201
uint32_t packed_data = 0;
//clamping
if (throttle > 100){
throttle = 100;
} else if(throttle < 0) {
throttle = 0;
}
if(brake > 100){
brake = 100;
} else if(brake < 0) {
brake = 0;
}
if(steering > 45){
steering = 45;
} else if(steering < -45) {
steering = -45;
}
//packing-bit the data
packed_data = packed_data | ((uint8_t)throttle & 0x7F);
packed_data = packed_data | ((uint8_t)brake & 0x7F) << 7;
packed_data = packed_data | (((uint8_t)(steering+45.0f)) & 0x7F) << 14;
//clearing the data[]
for(int i = 0 ; i < 8 ; i++){
frame.data[i] = 0;
}
//Maping the data
frame.data[0] = (uint8_t)(packed_data & 0xFF) ;
frame.data[1] = (uint8_t)((packed_data >> 7) & 0xFF);
frame.data[2] = (uint8_t)((packed_data >> 14) & 0xFF) ;
//Rolling attack
frame.data[6] = msg_counter & 0xF;
msg_counter++;
if(msg_counter > 15) msg_counter = 0;
//Checksum for the msg 0-6 bytes
frame.data[7] = calculateChecksum(frame.data, 7);
}
//SafetyManeging
void SafetyManeger::setChecksum_fail(bool status) {
CHECKSUM_FAIL = status;
}
void SafetyManeger::setCounter_fail(bool status) {
COUNTER_FAIL = status;
}
void SafetyManeger::setTimeout_fail(bool status) {
TIMEOUT_FAIL = status;
}
SafetyManeger::SafetyManeger(){
currentState = SystemStates::STATE_NORMAL; //at the beggining of the program
}
SystemStates SafetyManeger::getCurrentState() {
return currentState;
}
void SafetyManeger::update() {
if(CHECKSUM_FAIL || COUNTER_FAIL || TIMEOUT_FAIL) {
currentState = SystemStates::STATE_FAILSAFE;
V_state.setBrake(MAX_BRAKE); // we created an object of the vehicleState
} else {
currentState = SystemStates::STATE_NORMAL;
}
}