#include <math.h>
using namespace std;
double map(
double x,
double in_min,
double in_max,
double out_min,
double out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
if(!
myRIO_init()) {cout <<
"Error initializing myRIO";
return -1;}
const long sampleTimeUs = 500;
double motorLeftSpeed = static_cast<char>((data&0xFF00)>>8);
double motorRightSpeed = static_cast<char>(data&0xFF);
if(motorLeftSpeed > 127) motorLeftSpeed -= 255;
if(motorRightSpeed > 127) motorRightSpeed -= 255;
motorLeftSpeed =
map(motorLeftSpeed, -128, 127, -850, 850);
motorRightSpeed =
map(motorRightSpeed, -128, 127, -850, 850);
if(abs((int)ceil(motorLeftSpeed))<50) motorLeftSpeed = 0;
if(abs((int)ceil(motorRightSpeed))<50) motorRightSpeed = 0;
motorLeftPid.setSetpoint(motorLeftSpeed);
motorRightPid.setSetpoint(motorRightSpeed);
if(data&0x08000000)
buzzer.setDutyCycle(50);
else
buzzer.setDutyCycle(0);
});
while(!w.isConnected());
while(1) {
long encL = motorLeft.getEncoderPulses();
if(motorLeft.getDefaultDirection()==
CCW) encL = - encL;
double correctedL = motorLeftPid.compute(encL);
motorLeft.setAngularSpeedAndDirection(correctedL);
long encR = motorRight.getEncoderPulses();
if(motorRight.getDefaultDirection()==
CCW) encR = - encR;
double correctedR = motorRightPid.compute(encR);
motorRight.setAngularSpeedAndDirection(correctedR);
}
return 0;
}