Completed the code with potentiometer reading and current reading from ACS758. The actual current sensor is on its way. The time to read the GPS is around 65ms. The throttle response is acceptable.
#include <LCDWIKI_GUI.h> //Core graphics library
#include <LCDWIKI_KBV.h> //Hardware-specific library
#include <TinyGPS++.h>
TinyGPSPlus gps; // create gps object
#include <Servo.h>
Servo ESC; // create servo object to control the ESC
float POTValue; // value from the analog pin
LCDWIKI_KBV mylcd(ILI9486,40,38,39,-1,41); //model,cs,cd,wr,rd,reset
//define some colour values
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
long count = 0;
float ACSValue;
float arrACSValue[10];
float AMPS;
void setup()
{
mylcd.Init_LCD();
mylcd.Set_Rotation(2);
mylcd.Set_Text_Mode(0);
mylcd.Set_Text_Back_colour(BLACK);
mylcd.Set_Text_Size(4);
mylcd.Set_Text_colour(GREEN);
mylcd.Fill_Screen(BLACK);
Serial.begin(115200); // connect serial monitor
Serial3.begin(9600); // connect gps sensor on default baud rate
delay(100);
changeFrequency(); // change measurement frequency
delay(100);
changeBaudrate(); // change gps module baud rate
// Attach the ESC on pin 9
ESC.attach(9,1000,2000); // (pin, min pulse width, max pulse width in microseconds)
}
void loop()
{
String strLine;
//Measure current flow through motor
ACSValue = analogRead(A0)*5/1023; //voltage read from ACS758
//ACS758ECB-200B-PFF-T
//Sensitivity = 10 mV/A
//ACS voltage offset = 0.5*Vcc = 2.5 V
float average_voltage = avgACSValue(ACSValue); //Stores the last measurement in array and retrieves average
//Current = (ACSVOffset ā (Arduino measured analog reading)) / Sensitivity
float current=(2.5-average_voltage)/10;
strLine = "CURR: "+String(current,1)+" A ";
Serial.println(strLine); //Output on serial monitor
mylcd.Print_String(strLine, 0, 0); //Output on LCD
//Measure potentiometer position
POTValue = analogRead(A3); // reads the value of the potentiometer (value between 0 and 1023)
POTValue = map(POTValue, 0, 1023, 0, 180); // scale it to use it with the servo library (value between 0 and 180)
ESC.write(POTValue); // Send the signal to the ESC
strLine = "THROT: "+String(100*POTValue/180,1)+" % ";
Serial.println(strLine); //Output on serial monitor
mylcd.Print_String(strLine, 0, 50); //Output on LCD
unsigned long TimeToFix=GPSFix(80);
strLine = "TTF: "+String(TimeToFix)+" ms ";
Serial.println(strLine); //Output on serial monitor
mylcd.Print_String(strLine, 0, 100); //Output on LCD
strLine = "LOCATION:";
Serial.println(strLine); //Output on serial monitor
mylcd.Print_String(strLine, 0, 150); //Output on LCD
strLine = String(gps.location.rawLat().negative ? "S " : "N ")+String(gps.location.lat(),5)+" ";
Serial.println(strLine); //Output on serial monitor
mylcd.Print_String(strLine, 0, 200); //Output on LCD
strLine = String(gps.location.rawLng().negative ? "W " : "E ")+String(gps.location.lng(),5)+" ";
Serial.println(strLine); //Output on serial monitor
mylcd.Print_String(strLine, 0, 250); //Output on LCD
strLine = "CRS: "+String(gps.course.deg(),1)+" deg ";
Serial.println(strLine); //Output on serial monitor
mylcd.Print_String(strLine, 0, 300); //Output on LCD
strLine = "SPD: "+String(gps.speed.mph(),1)+" mph ";
Serial.println(strLine); //Output on serial monitor
mylcd.Print_String(strLine, 0, 350); //Output on LCD
strLine = "N.SATS: "+String(gps.satellites.value())+" ";
Serial.println(strLine); //Output on serial monitor
mylcd.Print_String(strLine, 0, 400); //Output on LCD
}
unsigned long GPSFix(unsigned long ms)
{
//waits a specified number of milliseconds for a fix, returns milliseconds for good fix
uint8_t GPSchar;
Serial.println("Waiting GPS Fix for "+String(ms/1000)+" seconds");
unsigned long startFixTime=millis();
while (millis() < startFixTime + ms)
{
while (Serial3.available() > 0)
{
GPSchar = Serial3.read();
gps.encode(GPSchar);
Serial.write(GPSchar);
}
if (gps.location.isUpdated() && gps.altitude.isUpdated() && gps.date.isUpdated())
{
return millis()-startFixTime;
}
}
return millis()-startFixTime;
}
void changeFrequency() {
byte packet[] = {
0xB5, 0x62, 0x06, 0x08, 0x06, 0x00, 0x64, //Measuring Rate, hex 64 = dec 100 ms
0x00, 0x01, 0x00, 0x01, 0x00, 0x7A, 0x12,
};
sendPacket(packet, sizeof(packet));
delay(100);
}
void changeBaudrate() {
byte baud9600[] = {
0xB5, 0x62, 0x06, 0x00, 0x14, 0x00, 0x01, 0x00, 0x00,
0x00, 0xD0, 0x08, 0x00, 0x00, 0x00, 0x96, 0x00, 0x00,
0x23, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0xAE, 0x6A,
};
byte baud38400[] = {
0xB5, 0x62, 0x06, 0x00, 0x14, 0x00, 0x01, 0x00, 0x00,
0x00, 0xD0, 0x08, 0x00, 0x00, 0xF0, 0x87, 0x00, 0x00,
0x07, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x74, 0x24,
};
byte baud115200[] = {
0xB5, 0x62, 0x06, 0x00, 0x14, 0x00, 0x01, 0x00, 0x00,
0x00, 0xD0, 0x08, 0x00, 0x00, 0x00, 0xC2, 0x01, 0x00,
0x07, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x7E,
};
sendPacket(baud115200, sizeof(baud115200));
delay(100);
Serial3.flush();
Serial3.begin(115200); //set new baud rate on the port connected to gps module
}
void sendPacket(byte *packet, byte len) {
for (byte i = 0; i < len; i++)
{
Serial3.write(packet[i]);
}
}
float avgACSValue(float newValue)
{
double sumACSValue = 0.0;
for(int i = 10 - 1; i > 0; i--)
{
arrACSValue[i] = arrACSValue[i-1];
sumACSValue += arrACSValue[i];
}
arrACSValue[0] = newValue;
return sumACSValue/10; //return average of all elements
}