forked from trcbots/linda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotor_velocity_controller.h
70 lines (61 loc) · 2.21 KB
/
motor_velocity_controller.h
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
class MotorVelocityController
{
public:
MotorVelocityController(String my_name, SabertoothSimplified *motor_interface,
int motor_id, WheelEncoderLS7366 *encoder_interface, int motor_max_power,
double Kp, double Ki, double Kd);
void SetTargetVelocity(double target_vel);
private:
String my_name_;
SabertoothSimplified *motor_interface_;
WheelEncoderLS7366 *encoder_interface_;
int motor_id_;
int feedback_pin_;
double Kp_;
double Kd_;
double Ki_;
int motor_max_power_;
};
MotorVelocityController::MotorVelocityController(String my_name, SabertoothSimplified *motor_interface,
int motor_id, WheelEncoderLS7366 *encoder_interface, int motor_max_power,
double Kp = 0.5, double Ki = 0.0, double Kd = 0.0)
{
// init the motor controller here
this->my_name_ = my_name;
this->motor_id_ = motor_id;
this->motor_interface_ = motor_interface;
this->encoder_interface_ = encoder_interface;
this->motor_max_power_ = motor_max_power;
this->Kp_ = Kp;
this->Ki_ = Ki;
this->Kd_ = Kd;
}
void MotorVelocityController::SetTargetVelocity(double target_vel)
{
// Implementation of a PID controller
// TODO: add make P and D terms work properly
double current_vel = encoder_interface_->get_update().velocity;
Serial.print(", current_vel=");
Serial.print(current_vel);
double pTerm = current_vel - target_vel;
double iTerm = 0.0;
double dTerm = 0.0;
double output = int(Kp_ * pTerm + Ki_ * iTerm + Kd_ * dTerm);
if ( output < -1 * motor_max_power_ ) {
output = -1 * motor_max_power_;
} else if ( output > motor_max_power_ ) {
output = motor_max_power_;
}
Serial.println("");
Serial.print(my_name_);
Serial.print(", motor ID: ");
Serial.print(motor_id_);
Serial.print(", output=");
Serial.print(output);
Serial.print(", target_vel=");
Serial.print(target_vel);
if (abs(output) > 10)
{
motor_interface_->motor(motor_id_, output);
}
}