Skip to content

Commit

Permalink
Optimized
Browse files Browse the repository at this point in the history
  • Loading branch information
WonITKorea committed Feb 1, 2024
1 parent 5b40ffe commit 471841a
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .vscode/arduino.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"sketch": "code\\DAQ_System\\DAQ_System.ino",
"board": "rp2040:rp2040:rpipico",
"port": "COM12",
"port": "COM18",
"output": "../build",
"intelliSenseGen": "global",
"configuration": "flash=2097152_0,freq=133,opt=Optimize,rtti=Disabled,stackprotect=Enabled,exceptions=Disabled,dbgport=Disabled,dbglvl=All,usbstack=picosdk,ipbtstack=ipv4only,uploadmethod=default"
Expand Down
76 changes: 47 additions & 29 deletions code/DAQ_System/DAQ_System.ino
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//
// GP4 - ADXL345 SDA Pin
// GP5 - ADXL345 SCL Pin
// GP6 - DIAG Mode Interrupt
// GP7 - DIAG Mode Interrupt
// GP10 - DHT Temp/Humid Sensor
// GP12 - MOSFET Upshift
// GP13 - MOSFET Downshift
Expand Down Expand Up @@ -177,8 +177,8 @@ int picoTemp = analogReadTemp(); // Pi Pico On-Board Temp Sensor
int obled = LED_BUILTIN; // On-Board LED for Basic Error Indication
ADCInput RPulse(A0); // For stereo/dual mikes, could use this line instead
// ADCInput(A0, A1);
#define DiagEN = 6 // Toggle Switch for OBD2 Simulation
int DIAGENB = 0; //
#define DiagEN 7 // Toggle Switch for OBD2 Simulation
int DIAGENB = 0; //

// ——————————————————————————————————————————————————————————————————————————————
// MOSFET Switch Module Configuration
Expand All @@ -199,12 +199,25 @@ void setup()
Serial.begin(115200);
dht.begin(); // DHT Sensor Begin
RPulse.begin(8000); // CPS Pulse A/D Converter Begin
if (!accel.begin())
{
/* There was a problem detecting the ADXL345 ... check your connections */
Serial.println("Ooops, no ADXL345 detected ... Check your wiring!");
while (1)
;
}
//accel.setRange(ADXL345_RANGE_16_G);
// accel.setRange(ADXL345_RANGE_8_G);
// accel.setRange(ADXL345_RANGE_4_G);
accel.setRange(ADXL345_RANGE_2_G);
Serial.println("-----ADXL345 STATUS-----");
/* Display some basic information on this sensor */
displaySensorDetails();
/* Display additional settings (outside the scope of sensor_t) */
displayDataRate();
displayRange();
Serial.println("");

while (CAN.begin(CAN_500KBPS))
{ // init can bus : baudrate = 500k
Serial.println("CAN BUS Shield init fail");
Expand All @@ -213,18 +226,7 @@ void setup()
delay(3000);
digitalWrite(obled, LOW);
}
if (!accel.begin())
{
/* There was a problem detecting the ADXL345 ... check your connections */
Serial.println("Ooops, no ADXL345 detected ... Check your wiring!");
while (1)
;
}
accel.setRange(ADXL345_RANGE_16_G);
// accel.setRange(ADXL345_RANGE_8_G);
// accel.setRange(ADXL345_RANGE_4_G);
// accel.setRange(ADXL345_RANGE_2_G);
Serial.println("-----ADXL345 STATUS-----");
delay(3000);
}
// ——————————————————————————————————————————————————————————————————————————————
// Main(Loop) Area
Expand Down Expand Up @@ -256,16 +258,16 @@ void loop()
accel.getEvent(&event);

// Display the results (acceleration is measured in m/s^2)
Serial.print("X: ");
Serial.print(event.acceleration.x);
Serial.print(" ");
Serial.print("Y: ");
Serial.print(event.acceleration.y);
Serial.print(" ");
Serial.print("Z: ");
Serial.print(event.acceleration.z);
Serial.print(" ");
Serial.println("m/s^2 ");
// Serial.print("X: ");
// Serial.print(event.acceleration.x);
// Serial.print(" ");
// Serial.print("Y: ");
// Serial.print(event.acceleration.y);
// Serial.print(" ");
// Serial.print("Z: ");
// Serial.print(event.acceleration.z);
// Serial.print(" ");
// Serial.println("m/s^2 ");

// CAN Area

Expand All @@ -283,8 +285,12 @@ void loop()
char ControlHumidity = h;

// Normal CAN Message Configuration
unsigned char SupportedPID[8] = {1, 2, 3, 4, 5, 6, 7, 8};
CAN.sendMsgBuf(0x7E8, 0, 8, SupportedPID);
unsigned char normal1MSG[8] = {rndCoolantTemp, rndRPM, rndSpeed, rndIAT, rndMAF, ControlAirTemp, ControlHumidity, 0};
unsigned char normal2MSG[4] = {acelx, acely, acelz, 1};
CAN.sendMsgBuf(canId, 0, 8, normal1MSG);
delay(200);
CAN.sendMsgBuf(0x1, 0, 4, normal2MSG);
delay(200);

// GENERAL Sensor ROUTINE
unsigned char SupportedPID[8] = {1, 2, 3, 4, 5, 6, 7, 8};
Expand All @@ -304,10 +310,15 @@ void loop()
unsigned char dAcelzdir[7] = {4, 65, 79, acelz, 224, 185, 147};
unsigned char dAcelall[7] = {4, 65, 80, acelx, acely, acelz, 147};


DIAGENB = digitalRead(DIAGEN); // When Diag Pin toggled to on
DIAGENB = digitalRead(DiagEN); // When Diag Pin toggled to on

if (DIAGENB == 1)
{
Serial.println("OBD-2 Diag Mode Enabled");
}
while (DIAGENB == 1)
{

if (CAN_MSGAVAIL == CAN.checkReceive())
{
CAN.readMsgBuf(&len, buf);
Expand All @@ -330,6 +341,10 @@ void loop()
if (BuildMessage == "2,1,1,0,0,0,0,0,")
{
CAN.sendMsgBuf(0x7E8, 0, 7, MilCleared);
Serial.println("OBD-2 Diag Mode Cleared");
delay(100);
BuildMessage = "";
break;
}

// SEND SENSOR STATUSES
Expand All @@ -339,10 +354,12 @@ void loop()
}
if (BuildMessage == "2,1,12,0,0,0,0,0,")
{
Serial.println(rndRPM);
CAN.sendMsgBuf(0x7E8, 0, 7, drpm);
}
if (BuildMessage == "2,1,13,0,0,0,0,0,")
{
Serial.println(rndSpeed);
CAN.sendMsgBuf(0x7E8, 0, 7, dvspeed);
}
if (BuildMessage == "2,1,15,0,0,0,0,0,")
Expand Down Expand Up @@ -382,6 +399,7 @@ void loop()
}
delay(100);
}
DIAGENB = 0;
delay(100);
}

Expand Down
55 changes: 32 additions & 23 deletions code/DIAG_DAQ/DIAG_DAQ.ino
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @maintainer Lim Chae Won
* @version V1.0
* @date 2024-01-20
* @url
* @url
* */
#include "DFRobot_MCP2515.h"

Expand All @@ -41,6 +41,7 @@ void setup()
Serial.println("DFROBOT's CAN BUS Shield init ok!\n");

attachInterrupt(20, MCP2515_ISR, FALLING); // start interrupt
delay(3000);
}

void MCP2515_ISR()
Expand All @@ -59,62 +60,63 @@ unsigned char Axdir[8] = {2, 1, 77, 0, 0, 0, 0, 0};
unsigned char Aydir[8] = {2, 1, 78, 0, 0, 0, 0, 0};
unsigned char Azdir[8] = {2, 1, 79, 0, 0, 0, 0, 0};
unsigned char AAll[8] = {2, 1, 80, 0, 0, 0, 0, 0};

void loop()
{

char Order = Serial.read();
if (Order == 'chk')
long Order = Serial.read();
if (Order == 'c')
{
CAN.sendMsgBuf(0x02, 0, 8, pidchk);
Order = 0;
Order = '0';
}
else if (Order == 'clr')
else if (Order == '0')
{
CAN.sendMsgBuf(0x02, 0, 8, milClr);
Order = 0;
Order = '0';
}
else if (Order == 'ct')
else if (Order == 'l')
{
CAN.sendMsgBuf(0x02, 0, 8, coolTemp);
Order = 0;
Order = '0';
}
else if (Order == 'r')
{
CAN.sendMsgBuf(0x02, 0, 8, rpmm);
Order = 0;
Order = '0';
}
else if (Order == 't')
{
CAN.sendMsgBuf(0x02, 0, 8, ambtemp);
Order = 0;
Order = '0';
}
else if (Order == 'h')
{
CAN.sendMsgBuf(0x02, 0, 8, obhumidity);
Order = 0;
Order = '0';
}
else if (Order == 'x')
{
CAN.sendMsgBuf(0x02, 0, 8, Axdir);
Order = 0;
Order = '0';
}
else if (Order == 'y')
{
CAN.sendMsgBuf(0x02, 0, 8, Aydir);
Order = 0;
Order = '0';
}
else if (Order == 'z')
{
CAN.sendMsgBuf(0x02, 0, 8, Azdir);
Order = 0;
Order = '0';
}
else if (Order == 'allaxis')
else if (Order == 'a')
{
CAN.sendMsgBuf(0x02, 0, 8, AAll);
Order = 0;
Order = '0';
}

else if (Order == 0)
else if (Order == '0')
{
}
if (flagRecv)
Expand All @@ -131,6 +133,7 @@ void loop()
// read data, len: data length, buf: data buf
CAN.readMsgBuf(&len, buf);
canId = CAN.getCanId();

if (canId == 0x7E8)
{
if (buf[0] == 4 && buf[1] == 65)
Expand All @@ -148,8 +151,12 @@ void loop()
Serial.print("TachoMeter: ");
Serial.println(buf[3], OCT);
break;
case 12:
Serial.print("TachoMeter: ");
case 13:
Serial.print("Speed: ");
Serial.println(buf[3], OCT);
break;
case 15:
Serial.print("IAT Temp: ");
Serial.println(buf[3], OCT);
break;
case 70:
Expand Down Expand Up @@ -184,15 +191,17 @@ void loop()
}
}
}
else if (canId)
else
{
// print the data
for (int i = 0; i < len; i++)
{
BuildMessage = BuildMessage + buf[i] + ",";
}
Serial.println(BuildMessage);
BuildMessage = "";
Serial.println(" ");
Serial.println(BuildMessage);
BuildMessage = "";
Serial.println(" ");
}
}
}
}

0 comments on commit 471841a

Please sign in to comment.