Wyatt  1.0.1
DriveMotorRPM.cpp
1 //
2 // Created by Tucker Haydon on 4/27/17.
3 //
4 
5 #include "../../include/commands/DriveMotorRPM.h"
6 #include <iostream>
7 #include <unistd.h>
8 
9 DriveMotorRPM::DriveMotorRPM(Communicator* comm, Hardware motorHardware, EncoderSensor* encoder, int desiredRPM, int direction) : Command() {
10  this->comm = comm;
11  this->motorHardware = motorHardware;
12  this->encoder = encoder;
13  this->direction = direction;
14  this->desiredRPM = desiredRPM;
15  this->setpoint = 0.0;
16 }
17 
19  // Nothing
20 }
21 
23 
24  double currentRPM = encoder->getRPM();
25  double diff = this->desiredRPM - currentRPM;
26 
27  double pd = this->k_p * diff + this->k_d * (diff - lastDiff);
28  this->setpoint = this->setpoint + pd;
29 
30  // Ensure that the control input is bounded
31  if(this->setpoint > FULL_FORWARD) {
32  this->setpoint = FULL_FORWARD;
33  }
34  if(this->setpoint < FULL_BACKWARD) {
35  this->setpoint = FULL_BACKWARD;
36  }
37 
38  // Compose the motor message
39  IMessage* msg = new MotorMessage(this->motorHardware, (int)(this->setpoint * this->direction));
40 
41  // Send the message to the communicator
42  comm->queueMessage(msg);
43 
44  // Log the lastDiff
45  this->lastDiff = diff;
46 
47  // Keep the PID running until this command is cancelled.
48  return true;
49 }
50 
51 bool DriveMotorRPM::cleanup(bool canceled) {
52  return true;
53 }
DriveMotorRPM(Communicator *comm, Hardware motorHardware, EncoderSensor *encoder, int desiredRPM, int direction)
bool execute() override
bool cleanup(bool canceled) override
void queueMessage(IMessage *message)
Definition: Command.h:6