I have been trying to make an RC car follow a designated heading. I've been using the pid library to try to make the steering smooth and accurate, but I can get it working. I want the car to always go in the direction of 180 degrees. The code I have so far uses a compass module for the PID input, I made the output limits 0-179. Take a look at my code and see what I'm doing wrong.
#include <PID_Beta6.h>
#include <Servo.h>
#include <Wire.h>
int HMC6352Address = 0x42;
Servo steering;
int slaveAddress;
int pos = 0;
byte headingData[2];
int steeringval;
int i, headingValue;
double Input, Output, Setpoint, Bias;
PID pid(&Input, &Output, &Setpoint, &Bias, 3, 4, 0);
void setup()
{
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
digitalWrite(9, LOW);
digitalWrite(8, HIGH);
steering.attach(10);
slaveAddress = HMC6352Address >> 1;
Serial.begin(9600);
pid.SetMode(AUTO);
Bias = 1;
pid.SetOutputLimits(0, 179);
Wire.begin();
}
void loop()
{
Wire.beginTransmission(slaveAddress);
Wire.send("A");
Wire.endTransmission();
delay(10);
Wire.requestFrom(slaveAddress, 2);
i = 0;
while(Wire.available() && i < 2)
{
headingData[i] = Wire.receive();
i++;
}
headingValue = headingData[0]*256 + headingData[1];
headingValue = headingValue / 10;
Serial.println(headingValue, DEC);
Input = headingValue;
Setpoint = 180;
pid.Compute();
steering.write(Output);
Serial.println(headingValue, DEC);
}
Any suggestions?