/* Teensy 4.0
CAN2 using SN65HVD230 can tranciever
*/
// #include <EEPROM.h>
#include <FlexCAN_T4.h>
FlexCAN_T4<CAN2, RX_SIZE_256, TX_SIZE_256> myCan;
// CAN_message_t msg, rxmsg;
/******************* ADDED FOR DEBUGGING ***********************/
// CAN1 used to ACK messages sent by myCAN
FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_256> myCanDEBUG;
/******************* END DEBUGGING ***********************/
// #define FEED 3 // oval gear flow meter hall effect digital input pin
// #define RETURN 4 // oval gear flow meter hall effect digital input pin
// #define Calibrate 5 // calibrate button
// boolean EEPROM_INIT = 0;
// unsigned long prevMillis;
// unsigned long txIntervalMillis = 20; // send once per second
// byte Data[8];
// volatile unsigned long FLOWIN_COUNT = 0;
// volatile unsigned long FLOWOUT_COUNT = 0;
// unsigned long CURRENT_millis = 0;
// unsigned long LAST_millis = 0;
// unsigned long LAST_eeprom = 0;
// int PPLIN;
// int PPLOUT;
// float UPDATE = 50.00; // ms between update cycles
// float PPL = 1448.00;
// float PPLin = 1448.00; // pulses per litre
// float PPLout = 1448.00; // pulses per litre
// float LPM = 0;
// float FLOW_IN = 0;
// float FLOW_RET = 0;
// float FLOW_USED = 0;
// float LASTFLOW = 0;
// float LPMFACTOR;
// boolean CALIBRATION_MODE = false;
// boolean CAL = false;
// float per_diff;
// int PPLI1, PPLI2, PPLO1, PPLO2;
// int AVE_COUNT = 0;
// boolean AVE_RUNNING = false;
void setup()
{
// pinMode(FEED, INPUT); // yellow
// pinMode(RETURN, INPUT); // blue
// pinMode(Calibrate, INPUT_PULLUP);
// pinMode(1, OUTPUT);
// digitalWrite(1, LOW);
Serial.begin(115200);
// attachInterrupt(digitalPinToInterrupt(FEED), FLOW_INP, FALLING); // FEED LINE FLOW METER INTERRUPT
// attachInterrupt(digitalPinToInterrupt(RETURN), FLOW_OUT, FALLING); // FEED LINE FLOW METER INTERRUPT
// LPMFACTOR = ((PPL / 60.00) / 1000.00); // L/M = ((PULSES PER LITRE / 60 SEC) /1000ms) ==> pulses per ms for 1 L/min flow
// Serial.println("SimpleRxAckPayload Starting");
myCan.begin();
myCan.setBaudRate(500000);
myCan.mailboxStatus();
/******************* ADDED FOR DEBUGGING ***********************/
// CAN1 used to ACK messages sent by myCAN
myCanDEBUG.begin();
myCanDEBUG.setBaudRate(500000);
/******************* END DEBUGGING *****************************/
// if (EEPROM_INIT == true)
// {
// EEPROM_INITIALISE();
// }
// READ_EEPROM();
}
// void READ_EEPROM()
// {
// PPLIN = EEPROM.read(0) << 8;
// PPLIN |= EEPROM.read(1);
// PPLin = PPLIN / 10.00;
// PPLOUT = EEPROM.read(2) << 8;
// PPLOUT |= EEPROM.read(3);
// PPLout = PPLOUT / 10.00;
// Serial.print("PPLin = ");
// Serial.print(PPLin);
// Serial.print(" ");
// Serial.println(PPLIN);
// Serial.print("PPLout = ");
// Serial.print(PPLout);
// Serial.print(" ");
// Serial.println(PPLOUT);
// }
// void EEPROM_INITIALISE()
// { // enable in setup to write initial values to eeprom
// PPLIN = 1448.00;
// PPLOUT = 1448.00;
// PPLI1 = (byte)((PPLIN >> 8) & 0xFFu); //split pulse per litre input into msb
// PPLI2 = (byte)(PPLIN & 0xFFu); //split pulse per litre input into lsb
// PPLO1 = (byte)((PPLOUT >> 8) & 0xFFu); //split pulse per litre output into msb
// PPLO2 = (byte)(PPLOUT & 0xFFu); //split pulse per litre output into lsb
// EEPROM.write(0, PPLI1); // Write values to EEPROM
// EEPROM.write(1, PPLI2); // Write values to EEPROM
// EEPROM.write(2, PPLO1); // Write values to EEPROM
// EEPROM.write(3, PPLO2); // Write values to EEPROM
// }
// void WRITE_EEPROM()
// {
// PPLIN = PPLin * 10;
// PPLOUT = PPLout * 10;
// PPLI1 = (byte)((PPLIN >> 8) & 0xFFu); //msb
// PPLI2 = (byte)(PPLIN & 0xFFu); //lsb
// PPLO1 = (byte)((PPLOUT >> 8) & 0xFFu); //msb
// PPLO2 = (byte)(PPLOUT & 0xFFu); //lsb
// EEPROM.write(0, PPLI1);
// EEPROM.write(1, PPLI2);
// EEPROM.write(2, PPLO1);
// EEPROM.write(3, PPLO2);
// }
void loop()
{
// if (digitalRead(Calibrate) == LOW && CALIBRATION_MODE == false)
// {
// AVE_RUNNING = true;
// CALIBRATION_MODE = true;
// }
// CURRENT_millis = millis();
// if (CURRENT_millis - LAST_millis > UPDATE)
// {
// LPM = FLOWIN_COUNT - LASTFLOW; // calculate how many pulses have occured since last calculation
// CURRENT_millis = millis(); // update millis
// LPM = LPM / ((CURRENT_millis - LAST_millis) / 10.00); // divide by elapsed milliseconds
// LPM = LPM / LPMFACTOR; // divide by the 1 L/min factor
// FLOW_IN = FLOWIN_COUNT / PPLin; // TOTAL FLOW = #OF PULSES / PULSES PER LITRE
// FLOW_RET = FLOWOUT_COUNT / PPLout; // TOTAL RETURN = #OF PULSES / PULSES PER LITRE
// FLOW_USED = FLOW_IN - FLOW_RET; // USED = in - out count
// per_diff = abs(((FLOW_RET / FLOW_IN) * 100.00));
// Serial.print(" FLOW DIFF PERCENTAGE = ");
// Serial.print(per_diff);
// int LPMSEND = LPM * 100;
// int FLOWSEND = FLOW_USED * 100;
// //prepare data for can send . split into 2 bytes of most and least significant bits
// Data[0] = (byte)((LPMSEND >> 8) & 0xFFu); // Litres per minute msb
// Data[1] = (byte)(LPMSEND & 0xFFu); // Litres per minute lsb
// Data[2] = (byte)((FLOWSEND >> 8) & 0xFFu); // flow msb
// Data[3] = (byte)(FLOWSEND & 0xFFu); // flow lsb
// Data[4] = 0;
// Data[5] = 0;
// Data[6] = 0;
// Serial.print(" FLOWIN_COUNT = ");
// Serial.print(FLOWIN_COUNT);
// Serial.print(" FLOWOUT_COUNT = ");
// Serial.print(FLOWOUT_COUNT);
// Serial.print(" FLOW_IN = ");
// Serial.print(FLOW_IN);
// Serial.print(" FLOW_RET = ");
// Serial.print(FLOW_RET);
// Serial.print(" FLOW_USED = ");
// Serial.print(FLOW_USED);
// Serial.print(" LPM = ");
// Serial.print(LPM);
// Serial.print(" LPMfactor = ");
// Serial.println(LPMFACTOR);
// LAST_millis = millis();
// LASTFLOW = FLOWIN_COUNT;
// if (AVE_RUNNING == true) // runs with pumps on engine off to zero differential reading
// {
// if (AVE_COUNT < 1000)
// {
// PPLout = PPLout * (per_diff / 100);
// Serial.print("PPLout = ");
// Serial.println(PPLout);
// AVE_COUNT++;
// }
// else if (AVE_COUNT == 100)
// {
// WRITE_EEPROM(); // save new factor for calculations
// AVE_RUNNING = false; // disable function
// }
// }
// }
// if (CURRENT_millis - prevMillis >= txIntervalMillis)
// {
SEND_CAN();
// }
static uint32_t timeoutMBStatus = millis();
if ( millis() - timeoutMBStatus > 2000 )
{
myCan.mailboxStatus();
timeoutMBStatus = millis();
}
}
void SEND_CAN()
{
static uint32_t timeout;
if (millis() - timeout > 200)
{ // send random frame every 1000ms
CAN_message_t msg;
msg.id = 0x236; // address ID 566
msg.buf[0] = lowByte(millis());
// msg.buf[1] = Data[1];
// msg.buf[2] = Data[2];
// msg.buf[3] = Data[3];
// msg.buf[4] = Data[4];
// msg.buf[5] = Data[5];
// msg.buf[6] = Data[6];
// msg.buf[7] = Data[7];
// msg.len = 8;
// msg.flags.extended = 0;
// msg.flags.remote = 0;
myCan.write(msg);
timeout = millis();
Serial.print("SEND: ");
Serial.print(" ID: ");
Serial.print(msg.id);
Serial.print(" Buffer: ");
for (uint8_t i = 0; i < msg.len; i++)
{
Serial.print(msg.buf[i]);
Serial.print(" ");
}
Serial.println();
}
}
// void FLOW_INP()
// {
// FLOWIN_COUNT++;
// }
// void FLOW_OUT()
// {
// FLOWOUT_COUNT++;
// }