From e27c7214b3f1ee98b7e7671e16b8eae18e5da56d Mon Sep 17 00:00:00 2001 From: "Landon Wheeler'ls.git config --global user.email lwheeler836647@gmail.comgit config --global user.name Landon" <144401916+lwheeler3@users.noreply.github.com> Date: Fri, 27 Sep 2024 18:44:59 -0600 Subject: [PATCH 1/7] Created Platformio Project --- .DS_Store | Bin 0 -> 6148 bytes FlatCarMock/.gitignore | 5 +++ FlatCarMock/.vscode/extensions.json | 10 ++++++ FlatCarMock/include/README | 39 +++++++++++++++++++++++ FlatCarMock/lib/README | 46 ++++++++++++++++++++++++++++ FlatCarMock/platformio.ini | 14 +++++++++ FlatCarMock/src/main.cpp | 18 +++++++++++ FlatCarMock/test/README | 11 +++++++ 8 files changed, 143 insertions(+) create mode 100644 .DS_Store create mode 100644 FlatCarMock/.gitignore create mode 100644 FlatCarMock/.vscode/extensions.json create mode 100644 FlatCarMock/include/README create mode 100644 FlatCarMock/lib/README create mode 100644 FlatCarMock/platformio.ini create mode 100644 FlatCarMock/src/main.cpp create mode 100644 FlatCarMock/test/README diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..34efa5798d64ac841082e990bc8ef96baba5509d GIT binary patch literal 6148 zcmeHK!Ab)$5S_HuZYe?!iXH=A3$`HI;$>;8H!rT}L8b26qD$9JX}9)J3VYQb@=yF8 zXOgT~Y7bsS$_z~2WHPfMFH0r?0MVNC>Hrl0a8L0;i@x!=3(g|U&k0H0$Vcb*GmYT%9vCj3(fKzfx z-OBE4)~Fv<<#BUfm9s|kuqvC4=6qgqcJ>cWF1my0IaaTRQGq|7mMx1JyrHpTaWCE| zj#T`J5m|PY#mEdW1I)nMGhh!sr@ZznLiPW2_4|J@iF?cdGq6?+h(gEfv~Wqbwk|D>YOO@QM THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/FlatCarMock/platformio.ini b/FlatCarMock/platformio.ini new file mode 100644 index 0000000..a13d034 --- /dev/null +++ b/FlatCarMock/platformio.ini @@ -0,0 +1,14 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:teensy41] +platform = teensy +board = teensy41 +framework = arduino diff --git a/FlatCarMock/src/main.cpp b/FlatCarMock/src/main.cpp new file mode 100644 index 0000000..cb9fbba --- /dev/null +++ b/FlatCarMock/src/main.cpp @@ -0,0 +1,18 @@ +#include + +// put function declarations here: +int myFunction(int, int); + +void setup() { + // put your setup code here, to run once: + int result = myFunction(2, 3); +} + +void loop() { + // put your main code here, to run repeatedly: +} + +// put function definitions here: +int myFunction(int x, int y) { + return x + y; +} \ No newline at end of file diff --git a/FlatCarMock/test/README b/FlatCarMock/test/README new file mode 100644 index 0000000..9b1e87b --- /dev/null +++ b/FlatCarMock/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Test Runner and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html From ea59e46514b145af5289daf1f204b4d2e1f20019 Mon Sep 17 00:00:00 2001 From: "Landon Wheeler'ls.git config --global user.email lwheeler836647@gmail.comgit config --global user.name Landon" <144401916+lwheeler3@users.noreply.github.com> Date: Fri, 27 Sep 2024 18:53:17 -0600 Subject: [PATCH 2/7] Created Platformio project --- FlatCarMock/.gitignore | 5 ----- FlatCarMock/.vscode/extensions.json | 10 ---------- {FlatCarMock/include => include}/README | 0 {FlatCarMock/lib => lib}/README | 0 FlatCarMock/platformio.ini => platformio.ini | 0 {FlatCarMock/src => src}/main.cpp | 0 {FlatCarMock/test => test}/README | 0 7 files changed, 15 deletions(-) delete mode 100644 FlatCarMock/.gitignore delete mode 100644 FlatCarMock/.vscode/extensions.json rename {FlatCarMock/include => include}/README (100%) rename {FlatCarMock/lib => lib}/README (100%) rename FlatCarMock/platformio.ini => platformio.ini (100%) rename {FlatCarMock/src => src}/main.cpp (100%) rename {FlatCarMock/test => test}/README (100%) diff --git a/FlatCarMock/.gitignore b/FlatCarMock/.gitignore deleted file mode 100644 index 89cc49c..0000000 --- a/FlatCarMock/.gitignore +++ /dev/null @@ -1,5 +0,0 @@ -.pio -.vscode/.browse.c_cpp.db* -.vscode/c_cpp_properties.json -.vscode/launch.json -.vscode/ipch diff --git a/FlatCarMock/.vscode/extensions.json b/FlatCarMock/.vscode/extensions.json deleted file mode 100644 index 080e70d..0000000 --- a/FlatCarMock/.vscode/extensions.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - // See http://go.microsoft.com/fwlink/?LinkId=827846 - // for the documentation about the extensions.json format - "recommendations": [ - "platformio.platformio-ide" - ], - "unwantedRecommendations": [ - "ms-vscode.cpptools-extension-pack" - ] -} diff --git a/FlatCarMock/include/README b/include/README similarity index 100% rename from FlatCarMock/include/README rename to include/README diff --git a/FlatCarMock/lib/README b/lib/README similarity index 100% rename from FlatCarMock/lib/README rename to lib/README diff --git a/FlatCarMock/platformio.ini b/platformio.ini similarity index 100% rename from FlatCarMock/platformio.ini rename to platformio.ini diff --git a/FlatCarMock/src/main.cpp b/src/main.cpp similarity index 100% rename from FlatCarMock/src/main.cpp rename to src/main.cpp diff --git a/FlatCarMock/test/README b/test/README similarity index 100% rename from FlatCarMock/test/README rename to test/README From e74ed7961274a5456cf7e331bbb492c0bbfd3127 Mon Sep 17 00:00:00 2001 From: "Landon Wheeler'ls.git config --global user.email lwheeler836647@gmail.comgit config --global user.name Landon" <144401916+lwheeler3@users.noreply.github.com> Date: Tue, 15 Oct 2024 17:41:07 -0600 Subject: [PATCH 3/7] Concept_Dev Push --- .gitignore | 5 + .vscode/extensions.json | 10 ++ include/FlatCar.h | 63 ++++++++++ src/FlatCar.cpp | 260 ++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 22 ++-- 5 files changed, 350 insertions(+), 10 deletions(-) create mode 100644 .gitignore create mode 100644 .vscode/extensions.json create mode 100644 include/FlatCar.h create mode 100644 src/FlatCar.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..080e70d --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,10 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/include/FlatCar.h b/include/FlatCar.h new file mode 100644 index 0000000..6cdc3b6 --- /dev/null +++ b/include/FlatCar.h @@ -0,0 +1,63 @@ +#ifndef FLATCAR_H +#define FLATCAR_H + + +class FlatCar { + +private: + +protected: + +public: + +}; + +char VAL; +float SPEED; +float RPS = SPEED * 0.350140874802; // Conversion factor from MPH to Rotations per second +int BRAKE_VAL; +int THROTTLE_VAL; +float TARGET_RUN_TIME; // In minutes when selected by the user +unsigned long RUN_TIME = TARGET_RUN_TIME * 60000; // Target run time of user in millis +unsigned long CURRENT_TIME; +unsigned long DIGITAL_UPDATE; // How often Digital sensors should be updated +unsigned long DIGITAL_ELAPSED; +unsigned long DIGITAL_LAST; +unsigned long WHEEL_UPDATE; +unsigned long WHEEL_ELAPSED; +unsigned long WHEEL_LAST; +unsigned long ANALOG_UPDATE; +unsigned long ANALOG_ELAPSED; +unsigned long ANALOG_LAST; +long RANDOM_ANALOG; +unsigned long CAN_UPDATE; +unsigned long CAN_ELAPSED; +unsigned long CAN_LAST; + +int j; // Used to store current wheel sensor status + + + +FlatCar car = FlatCar(); + +const int WHEEL_SPEED_PINS[] = {2, 3, 4, 5}; +constexpr int START_SWITCH_PIN = 6; +constexpr int BRAKE_1_PIN = A0; +constexpr int TRACTIVE_PIN = 9; +constexpr int THROTTLE_1_PIN = A2; +constexpr int THROTTLE_2_PIN = A3; + +const int NUM_WHEEL_SPEED_PINS = sizeof(WHEEL_SPEED_PINS) / sizeof(WHEEL_SPEED_PINS[0]); + + +char menuSelect(); +void canSetup(); +void pinSetup(); +void menuInit(); +char runPrograms(); +void staticTest(); +void variableTest(); +void randomTest(); +void simTest(); + +#endif diff --git a/src/FlatCar.cpp b/src/FlatCar.cpp new file mode 100644 index 0000000..351ef97 --- /dev/null +++ b/src/FlatCar.cpp @@ -0,0 +1,260 @@ +#include "FlatCar.h" +#include + +FlexCAN_T4 can1; +// Define defintions here + + +void canSetup() { + Serial.begin(9600); + can1.begin(); + can1.setBaudRate(500000); + Serial.println("CAN BUS INITALIZED"); +} + + +void pinSetup() { + for (int i = 0; i < NUM_WHEEL_SPEED_PINS; i++) { + pinMode(WHEEL_SPEED_PINS[i], OUTPUT); + Serial.print("WHEEL SPEED PIN "); + Serial.print(WHEEL_SPEED_PINS[i]); + Serial.println(" INITIALIZED"); + } + + pinMode(START_SWITCH_PIN, OUTPUT); + Serial.print("SWITCH PIN: "); + Serial.print(START_SWITCH_PIN); + Serial.println(" INITIALIZED"); + + pinMode(BRAKE_1_PIN, OUTPUT); + Serial.print("BRAKE 1 PIN "); + Serial.print(BRAKE_1_PIN); + Serial.println(" INITIALIZED"); + + pinMode(TRACTIVE_PIN, OUTPUT); + Serial.print("TRACTIVE PIN "); + Serial.print(TRACTIVE_PIN); + Serial.println(" INITIALIZED"); + + pinMode(THROTTLE_1_PIN, OUTPUT); + Serial.print("THROTTLE 1 PIN "); + Serial.print(THROTTLE_1_PIN); + Serial.println(" INITIALIZED"); + + pinMode(THROTTLE_2_PIN, OUTPUT); + Serial.print("THROTTLE 2 PIN "); + Serial.print(THROTTLE_2_PIN); + Serial.println(" INITIALIZED"); + + Serial.println("ALL PINS INITIALIZED"); +} + +void menuInit() { + Serial.println("WELCOME TO FLATCAR"); + Serial.println("START MENU:"); + Serial.println("1) STATIC VALUE TEST"); + Serial.println("2) VARIABLE VALUE TEST"); + Serial.println("3) RANDOM VALUE TEST"); + Serial.println("4) RUN SIM"); + Serial.println("5) EXIT"); +} + +char menuSelect() { + menuInit(); + Serial.println("WAITING FOR INPUT"); + Serial.print("SELECTION: "); + while (!Serial.available()) { + // Wait for serial input + } + char VAL = Serial.read(); + Serial.print("TARGET RUN TIME (MIN): "); + while (!Serial.available()) { + // Wait for serial input + } + float RUN_TIME = Serial.read(); + switch (VAL) { + case '1': + Serial.println("SELECTED: STATIC VALUE TEST"); + Serial.print("TARGET RUN TIME (MIN): "); + Serial.print(RUN_TIME); + break; + case '2': + Serial.println("SELECTED: VARIABLE VALUE TEST"); + Serial.print("TARGET RUN TIME (MIN): "); + Serial.print(RUN_TIME); + break; + case '3': + Serial.println("SELECTED: RANDOM VALUE TEST"); + Serial.print("TARGET RUN TIME (MIN): "); + Serial.print(RUN_TIME); + break; + case '4': + Serial.println("SELECTED: RUN SIM"); + Serial.print("TARGET RUN TIME (MIN): "); + Serial.print(RUN_TIME); + break; + case '5': + Serial.println("EXITING..."); + break; + default: + Serial.println("INVALID CHOICE"); + break; + } +} + +char runPrograms() { + switch (VAL) { + case '1': + staticTest(); + case '2': + variableTest(); + case '3': + randomTest(); + case '4': + simTest(); + } +} + +void startSequence() { + digitalWrite(START_SWITCH_PIN, HIGH); + digitalWrite(TRACTIVE_PIN, HIGH); +} + +void staticTest() { + startSequence(); + for (int i = 0; i < 4; i++) { + digitalWrite(WHEEL_SPEED_PINS[i], HIGH); + } + analogWrite(BRAKE_1_PIN, 127); + analogWrite(THROTTLE_1_PIN, 127); + analogWrite(THROTTLE_2_PIN, 127); + + Serial.println("STATIC TEST COMPLETE"); + while (Serial.available()) { + // wait for Serial input + } +} + +void updateTimes() { + // Update all times to get current elapse times + CURRENT_TIME = millis(); + DIGITAL_ELAPSED = CURRENT_TIME - DIGITAL_LAST; + ANALOG_ELAPSED = CURRENT_TIME - ANALOG_LAST; + WHEEL_ELAPSED = CURRENT_TIME - WHEEL_LAST; + CAN_ELAPSED = CURRENT_TIME - CAN_LAST; + +} + +void variableTest() { + startSequence(); + // DIGITAL_UPDATE = 100; + ANALOG_UPDATE = 100; + // WHEEL_UPDATE = SPEED (ADD CONSTANT TO CONVERT FROM MPH TO DIGITAL READ RATE) + CAN_UPDATE = 100; + BRAKE_VAL = 0; + THROTTLE_VAL = 0; + + while (CURRENT_TIME < RUN_TIME) { + + updateTimes(); + + // if (DIGITAL_ELAPSED > DIGITAL_UPDATE) { + + // DIGITAL_LAST = millis(); + // } + if (ANALOG_ELAPSED > ANALOG_UPDATE) { + // Writes values to pins + analogWrite(BRAKE_1_PIN, BRAKE_VAL); + analogWrite(THROTTLE_1_PIN, THROTTLE_VAL); + analogWrite(THROTTLE_2_PIN, 255 - THROTTLE_VAL); + + // Incriment +1 each cycle + BRAKE_VAL++; + THROTTLE_VAL++; + + // Resets values at 255 + if (BRAKE_VAL = 255) { + BRAKE_VAL = 0; + } + if (THROTTLE_VAL = 255) { + THROTTLE_VAL = 0; + } + + // Updates last rut time + ANALOG_LAST = millis(); + } + if (WHEEL_ELAPSED > WHEEL_UPDATE) { + if (j = 0) { + for (int i = 0; i < 4; i++) { + digitalWrite(WHEEL_SPEED_PINS[i], HIGH); + } + j = 1; + } + if (j = 1) { + for (int i = 0; i < 4; i++) { + digitalWrite(WHEEL_SPEED_PINS[i], LOW); + } + j = 0; + } + WHEEL_LAST = millis(); + } + if (CAN_ELAPSED > CAN_UPDATE) { + + CAN_LAST = millis(); + } + else { + CURRENT_TIME = millis(); + } + } +} + +void randomTest() { + startSequence(); + while (CURRENT_TIME < RUN_TIME) { + + updateTimes(); + + if (DIGITAL_ELAPSED > DIGITAL_UPDATE) { + + DIGITAL_LAST = millis(); + } + if (ANALOG_ELAPSED > ANALOG_UPDATE) { + RANDOM_ANALOG = random(0, 255); + + analogWrite(BRAKE_1_PIN, RANDOM_ANALOG); + + RANDOM_ANALOG = random(0, 255); + analogWrite(THROTTLE_1_PIN, RANDOM_ANALOG); + analogWrite(THROTTLE_2_PIN, 255 - RANDOM_ANALOG); + + ANALOG_LAST = millis(); + } + if (WHEEL_ELAPSED > WHEEL_UPDATE) { + if (j = 0) { + for (int i = 0; i < 4; i++) { + digitalWrite(WHEEL_SPEED_PINS[i], HIGH); + } + j = 1; + } + if (j = 1) { + for (int i = 0; i < 4; i++) { + digitalWrite(WHEEL_SPEED_PINS[i], LOW); + } + j = 0; + } + WHEEL_LAST = millis(); + } + if (CAN_ELAPSED > CAN_UPDATE) { + + CAN_LAST = millis(); + } + else { + CURRENT_TIME = millis(); + } + } +} + +void simTest() { + startSequence(); + return; +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index cb9fbba..fb103be 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,18 +1,20 @@ #include - -// put function declarations here: -int myFunction(int, int); +#include +#include "FlatCar.h" void setup() { - // put your setup code here, to run once: - int result = myFunction(2, 3); + return; + Serial.begin(9600); + Serial.println("Serial Port Intialized"); + canSetup(); + pinSetup(); } void loop() { - // put your main code here, to run repeatedly: -} -// put function definitions here: -int myFunction(int x, int y) { - return x + y; + menuSelect(); + runPrograms(); + + return; + // Cycle through an array of pins writing according to various functions } \ No newline at end of file From 66c86cef43b1525300a43f3ed99688286f6b3916 Mon Sep 17 00:00:00 2001 From: "Landon Wheeler'ls.git config --global user.email lwheeler836647@gmail.comgit config --global user.name Landon" <144401916+lwheeler3@users.noreply.github.com> Date: Sat, 19 Oct 2024 17:27:20 -0600 Subject: [PATCH 4/7] Finalized intial development, ready for testing --- include/FlatCar.h | 17 +---------------- src/FlatCar.cpp | 46 ++++++++++++++++++++++++++++++++++++---------- src/main.cpp | 1 - 3 files changed, 37 insertions(+), 27 deletions(-) diff --git a/include/FlatCar.h b/include/FlatCar.h index 6cdc3b6..509eb86 100644 --- a/include/FlatCar.h +++ b/include/FlatCar.h @@ -1,24 +1,13 @@ #ifndef FLATCAR_H #define FLATCAR_H - -class FlatCar { - -private: - -protected: - -public: - -}; - char VAL; float SPEED; float RPS = SPEED * 0.350140874802; // Conversion factor from MPH to Rotations per second int BRAKE_VAL; int THROTTLE_VAL; float TARGET_RUN_TIME; // In minutes when selected by the user -unsigned long RUN_TIME = TARGET_RUN_TIME * 60000; // Target run time of user in millis +unsigned long RUN_TIME; // Target run time of user in millis unsigned long CURRENT_TIME; unsigned long DIGITAL_UPDATE; // How often Digital sensors should be updated unsigned long DIGITAL_ELAPSED; @@ -36,10 +25,6 @@ unsigned long CAN_LAST; int j; // Used to store current wheel sensor status - - -FlatCar car = FlatCar(); - const int WHEEL_SPEED_PINS[] = {2, 3, 4, 5}; constexpr int START_SWITCH_PIN = 6; constexpr int BRAKE_1_PIN = A0; diff --git a/src/FlatCar.cpp b/src/FlatCar.cpp index 351ef97..5631566 100644 --- a/src/FlatCar.cpp +++ b/src/FlatCar.cpp @@ -1,15 +1,19 @@ #include "FlatCar.h" #include -FlexCAN_T4 can1; +FlexCAN_T4 CORE_CAN; +FlexCAN_T4 AUX_CAN; // Define defintions here void canSetup() { - Serial.begin(9600); - can1.begin(); - can1.setBaudRate(500000); - Serial.println("CAN BUS INITALIZED"); + CORE_CAN.begin(); + CORE_CAN.setBaudRate(500000); + Serial.println("CORE CAN BUS INITALIZED"); + + AUX_CAN.begin(); + AUX_CAN.setBaudRate(500000); + Serial.println("AUX CAN BUS INITIALIZED"); } @@ -120,6 +124,14 @@ void startSequence() { digitalWrite(TRACTIVE_PIN, HIGH); } +void motorCAN() { + return; +} + +void BMSCAN() { + return; +} + void staticTest() { startSequence(); for (int i = 0; i < 4; i++) { @@ -147,21 +159,35 @@ void updateTimes() { void variableTest() { startSequence(); - // DIGITAL_UPDATE = 100; + RUN_TIME = TARGET_RUN_TIME * 60000; + DIGITAL_UPDATE = 100; ANALOG_UPDATE = 100; - // WHEEL_UPDATE = SPEED (ADD CONSTANT TO CONVERT FROM MPH TO DIGITAL READ RATE) + WHEEL_UPDATE = SPEED; CAN_UPDATE = 100; BRAKE_VAL = 0; THROTTLE_VAL = 0; + SPEED = 10; while (CURRENT_TIME < RUN_TIME) { updateTimes(); - // if (DIGITAL_ELAPSED > DIGITAL_UPDATE) { + if (CURRENT_TIME % 1000 == 0) { + SPEED++; + RPS = (SPEED * 1.4667) / (3.141592653589793 * 1.33333333); + WHEEL_UPDATE = RPS / 10; + if (SPEED == 120) { + SPEED == 0; + } + } + + if (DIGITAL_ELAPSED > DIGITAL_UPDATE) { + digitalWrite(TRACTIVE_PIN, HIGH); + digitalWrite(START_SWITCH_PIN, HIGH); + + DIGITAL_LAST = millis(); + } - // DIGITAL_LAST = millis(); - // } if (ANALOG_ELAPSED > ANALOG_UPDATE) { // Writes values to pins analogWrite(BRAKE_1_PIN, BRAKE_VAL); diff --git a/src/main.cpp b/src/main.cpp index fb103be..313fd11 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,5 +16,4 @@ void loop() { runPrograms(); return; - // Cycle through an array of pins writing according to various functions } \ No newline at end of file From 82ff77a8eff451ed94fba24dc146974e8996699f Mon Sep 17 00:00:00 2001 From: "Landon Wheeler'ls.git config --global user.email lwheeler836647@gmail.comgit config --global user.name Landon" <144401916+lwheeler3@users.noreply.github.com> Date: Sat, 19 Oct 2024 17:28:24 -0600 Subject: [PATCH 5/7] Added additional changes that weren't previously added --- include/FlatCar.h | 7 ++-- src/FlatCar.cpp | 91 +++++++++++++++++++++++++++++++++++++---------- src/main.cpp | 4 --- 3 files changed, 78 insertions(+), 24 deletions(-) diff --git a/include/FlatCar.h b/include/FlatCar.h index 509eb86..c8c69a6 100644 --- a/include/FlatCar.h +++ b/include/FlatCar.h @@ -1,9 +1,11 @@ #ifndef FLATCAR_H #define FLATCAR_H +bool CAN_SNIFF; char VAL; float SPEED; -float RPS = SPEED * 0.350140874802; // Conversion factor from MPH to Rotations per second +float RPS; +unsigned long SPEED_UPDATE; int BRAKE_VAL; int THROTTLE_VAL; float TARGET_RUN_TIME; // In minutes when selected by the user @@ -23,7 +25,7 @@ unsigned long CAN_UPDATE; unsigned long CAN_ELAPSED; unsigned long CAN_LAST; -int j; // Used to store current wheel sensor status +bool j; // Used to store current wheel sensor status const int WHEEL_SPEED_PINS[] = {2, 3, 4, 5}; constexpr int START_SWITCH_PIN = 6; @@ -38,6 +40,7 @@ const int NUM_WHEEL_SPEED_PINS = sizeof(WHEEL_SPEED_PINS) / sizeof(WHEEL_SPEED_P char menuSelect(); void canSetup(); void pinSetup(); +void canSniff(); void menuInit(); char runPrograms(); void staticTest(); diff --git a/src/FlatCar.cpp b/src/FlatCar.cpp index 5631566..40cc214 100644 --- a/src/FlatCar.cpp +++ b/src/FlatCar.cpp @@ -53,6 +53,34 @@ void pinSetup() { Serial.println("ALL PINS INITIALIZED"); } +void canSniff() { + CAN_message_t msg; + + if (CORE_CAN.read(msg)) { + Serial.print("[CORE CAN] ID: "); + Serial.print(msg.id); + Serial.print(" LEGNTH: "); + Serial.print(msg.len); + Serial.print(" DATA: "); + for (uint8_t i = 0; i < msg.len; i++) { + Serial.print(msg.buf[i]); + Serial.print(" "); + } + } + + if (AUX_CAN.read(msg)) { + Serial.print("[AUX CAN] ID: "); + Serial.print(msg.id); + Serial.print(" LEGNTH: "); + Serial.print(msg.len); + Serial.print(" DATA: "); + for (uint8_t i = 0; i < msg.len; i++) { + Serial.print(msg.buf[i]); + Serial.print(" "); + } + } +} + void menuInit() { Serial.println("WELCOME TO FLATCAR"); Serial.println("START MENU:"); @@ -65,17 +93,26 @@ void menuInit() { char menuSelect() { menuInit(); + Serial.println("WAITING FOR INPUT"); Serial.print("SELECTION: "); while (!Serial.available()) { // Wait for serial input } char VAL = Serial.read(); + Serial.print("TARGET RUN TIME (MIN): "); - while (!Serial.available()) { + while (!Serial.available()) { // Wait for serial input } float RUN_TIME = Serial.read(); + + Serial.print("TOGGLE CAN SNIFF"); + while (!Serial.available()) { + // Wait for serial input + } + bool CAN_SNIFF = Serial.read(); + switch (VAL) { case '1': Serial.println("SELECTED: STATIC VALUE TEST"); @@ -157,28 +194,38 @@ void updateTimes() { } -void variableTest() { - startSequence(); +void updateRates() { RUN_TIME = TARGET_RUN_TIME * 60000; - DIGITAL_UPDATE = 100; + DIGITAL_UPDATE = 1000; ANALOG_UPDATE = 100; - WHEEL_UPDATE = SPEED; + WHEEL_UPDATE = 350; // Wheel update value for 10 mph initial start CAN_UPDATE = 100; BRAKE_VAL = 0; THROTTLE_VAL = 0; SPEED = 10; + SPEED_UPDATE = 5000; +} + +void variableTest() { + startSequence(); + updateRates(); while (CURRENT_TIME < RUN_TIME) { updateTimes(); - if (CURRENT_TIME % 1000 == 0) { + if (CAN_SNIFF) { + canSniff(); + } + + if (CURRENT_TIME >= SPEED_UPDATE) { SPEED++; RPS = (SPEED * 1.4667) / (3.141592653589793 * 1.33333333); WHEEL_UPDATE = RPS / 10; if (SPEED == 120) { SPEED == 0; } + SPEED_UPDATE += 5000; } if (DIGITAL_ELAPSED > DIGITAL_UPDATE) { @@ -224,23 +271,31 @@ void variableTest() { } WHEEL_LAST = millis(); } - if (CAN_ELAPSED > CAN_UPDATE) { + // if (CAN_ELAPSED > CAN_UPDATE) { - CAN_LAST = millis(); - } - else { - CURRENT_TIME = millis(); - } + // CAN_LAST = millis(); + // } + // else { + // CURRENT_TIME = millis(); + // } } } void randomTest() { startSequence(); + updateRates(); + while (CURRENT_TIME < RUN_TIME) { updateTimes(); + if (CAN_SNIFF) { + canSniff(); + } + if (DIGITAL_ELAPSED > DIGITAL_UPDATE) { + digitalWrite(TRACTIVE_PIN, HIGH); + digitalWrite(START_SWITCH_PIN, HIGH); DIGITAL_LAST = millis(); } @@ -270,13 +325,13 @@ void randomTest() { } WHEEL_LAST = millis(); } - if (CAN_ELAPSED > CAN_UPDATE) { + // if (CAN_ELAPSED > CAN_UPDATE) { - CAN_LAST = millis(); - } - else { - CURRENT_TIME = millis(); - } + // CAN_LAST = millis(); + // } + // else { + // CURRENT_TIME = millis(); + // } } } diff --git a/src/main.cpp b/src/main.cpp index 313fd11..d23ba43 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,7 +3,6 @@ #include "FlatCar.h" void setup() { - return; Serial.begin(9600); Serial.println("Serial Port Intialized"); canSetup(); @@ -11,9 +10,6 @@ void setup() { } void loop() { - menuSelect(); runPrograms(); - - return; } \ No newline at end of file From 5d969966767b7b9a5e6ad7a926fd2d60292cab4c Mon Sep 17 00:00:00 2001 From: "Landon Wheeler'ls.git config --global user.email lwheeler836647@gmail.comgit config --global user.name Landon" <144401916+lwheeler3@users.noreply.github.com> Date: Tue, 29 Oct 2024 13:19:17 -0600 Subject: [PATCH 6/7] Pushing code that actually compiles for intial test --- include/FlatCar.h | 66 ++++++++++++++++++++++----------------------- src/FlatCar.cpp | 69 ++++++++++++++++++++++++++++++++++------------- 2 files changed, 84 insertions(+), 51 deletions(-) diff --git a/include/FlatCar.h b/include/FlatCar.h index c8c69a6..b7e01a9 100644 --- a/include/FlatCar.h +++ b/include/FlatCar.h @@ -1,48 +1,48 @@ #ifndef FLATCAR_H #define FLATCAR_H -bool CAN_SNIFF; -char VAL; -float SPEED; -float RPS; -unsigned long SPEED_UPDATE; -int BRAKE_VAL; -int THROTTLE_VAL; -float TARGET_RUN_TIME; // In minutes when selected by the user -unsigned long RUN_TIME; // Target run time of user in millis -unsigned long CURRENT_TIME; -unsigned long DIGITAL_UPDATE; // How often Digital sensors should be updated -unsigned long DIGITAL_ELAPSED; -unsigned long DIGITAL_LAST; -unsigned long WHEEL_UPDATE; -unsigned long WHEEL_ELAPSED; -unsigned long WHEEL_LAST; -unsigned long ANALOG_UPDATE; -unsigned long ANALOG_ELAPSED; -unsigned long ANALOG_LAST; -long RANDOM_ANALOG; -unsigned long CAN_UPDATE; -unsigned long CAN_ELAPSED; -unsigned long CAN_LAST; +extern bool CAN_SNIFF; +extern char USER_INPUT; +extern float SPEED; +extern float RPS; +extern unsigned long SPEED_UPDATE; +extern int BRAKE_VAL; +extern int THROTTLE_VAL; +extern float TARGET_RUN_TIME; // In minutes when selected by the user +extern unsigned long RUN_TIME; // Target run time of user in millis +extern unsigned long CURRENT_TIME; +extern unsigned long DIGITAL_UPDATE; // How often Digital sensors should be updated +extern unsigned long DIGITAL_ELAPSED; +extern unsigned long DIGITAL_LAST; +extern unsigned long WHEEL_UPDATE; +extern unsigned long WHEEL_ELAPSED; +extern unsigned long WHEEL_LAST; +extern unsigned long ANALOG_UPDATE; +extern unsigned long ANALOG_ELAPSED; +extern unsigned long ANALOG_LAST; +extern long RANDOM_ANALOG; +extern unsigned long CAN_UPDATE; +extern unsigned long CAN_ELAPSED; +extern unsigned long CAN_LAST; -bool j; // Used to store current wheel sensor status +extern bool WHEEL_STATUS; // Used to store current wheel sensor status -const int WHEEL_SPEED_PINS[] = {2, 3, 4, 5}; -constexpr int START_SWITCH_PIN = 6; -constexpr int BRAKE_1_PIN = A0; -constexpr int TRACTIVE_PIN = 9; -constexpr int THROTTLE_1_PIN = A2; -constexpr int THROTTLE_2_PIN = A3; +extern const int WHEEL_SPEED_PINS[]; +extern int START_SWITCH_PIN; +extern int BRAKE_1_PIN; +extern int TRACTIVE_PIN; +extern int THROTTLE_1_PIN; +extern int THROTTLE_2_PIN; -const int NUM_WHEEL_SPEED_PINS = sizeof(WHEEL_SPEED_PINS) / sizeof(WHEEL_SPEED_PINS[0]); +extern const int NUM_WHEEL_SPEED_PINS; -char menuSelect(); +void menuSelect(); void canSetup(); void pinSetup(); void canSniff(); void menuInit(); -char runPrograms(); +void runPrograms(); void staticTest(); void variableTest(); void randomTest(); diff --git a/src/FlatCar.cpp b/src/FlatCar.cpp index 40cc214..3c0bd8b 100644 --- a/src/FlatCar.cpp +++ b/src/FlatCar.cpp @@ -3,7 +3,40 @@ FlexCAN_T4 CORE_CAN; FlexCAN_T4 AUX_CAN; -// Define defintions here + + +bool CAN_SNIFF = false; +char USER_INPUT = 0; +float SPEED = 0.0; +float RPS = 0.0; +unsigned long SPEED_UPDATE = 0; +int BRAKE_VAL = 0; +int THROTTLE_VAL = 0; +float TARGET_RUN_TIME = 0.0; +unsigned long RUN_TIME = 0; +unsigned long CURRENT_TIME = 0; +unsigned long DIGITAL_UPDATE = 0; +unsigned long DIGITAL_ELAPSED = 0; +unsigned long DIGITAL_LAST = 0; +unsigned long WHEEL_UPDATE = 0; +unsigned long WHEEL_ELAPSED = 0; +unsigned long WHEEL_LAST = 0; +unsigned long ANALOG_UPDATE = 0; +unsigned long ANALOG_ELAPSED = 0; +unsigned long ANALOG_LAST = 0; +long RANDOM_ANALOG = 0; +unsigned long CAN_UPDATE = 0; +unsigned long CAN_ELAPSED = 0; +unsigned long CAN_LAST = 0; +bool WHEEL_STATUS = false; + +const int WHEEL_SPEED_PINS[] = {2, 3, 4, 5}; +int START_SWITCH_PIN = 6; +int BRAKE_1_PIN = 14; +int TRACTIVE_PIN = 9; +int THROTTLE_1_PIN = 15; +int THROTTLE_2_PIN = 16; +const int NUM_WHEEL_SPEED_PINS = sizeof(WHEEL_SPEED_PINS) / sizeof(WHEEL_SPEED_PINS[0]); void canSetup() { @@ -91,7 +124,7 @@ void menuInit() { Serial.println("5) EXIT"); } -char menuSelect() { +void menuSelect() { menuInit(); Serial.println("WAITING FOR INPUT"); @@ -99,7 +132,7 @@ char menuSelect() { while (!Serial.available()) { // Wait for serial input } - char VAL = Serial.read(); + char USER_INPUT = Serial.read(); Serial.print("TARGET RUN TIME (MIN): "); while (!Serial.available()) { @@ -113,7 +146,7 @@ char menuSelect() { } bool CAN_SNIFF = Serial.read(); - switch (VAL) { + switch (USER_INPUT) { case '1': Serial.println("SELECTED: STATIC VALUE TEST"); Serial.print("TARGET RUN TIME (MIN): "); @@ -143,8 +176,8 @@ char menuSelect() { } } -char runPrograms() { - switch (VAL) { +void runPrograms() { + switch (USER_INPUT) { case '1': staticTest(); case '2': @@ -221,9 +254,9 @@ void variableTest() { if (CURRENT_TIME >= SPEED_UPDATE) { SPEED++; RPS = (SPEED * 1.4667) / (3.141592653589793 * 1.33333333); - WHEEL_UPDATE = RPS / 10; + WHEEL_UPDATE = 1 / (RPS / 10); if (SPEED == 120) { - SPEED == 0; + SPEED = 0; } SPEED_UPDATE += 5000; } @@ -246,10 +279,10 @@ void variableTest() { THROTTLE_VAL++; // Resets values at 255 - if (BRAKE_VAL = 255) { + if (BRAKE_VAL == 255) { BRAKE_VAL = 0; } - if (THROTTLE_VAL = 255) { + if (THROTTLE_VAL == 255) { THROTTLE_VAL = 0; } @@ -257,17 +290,17 @@ void variableTest() { ANALOG_LAST = millis(); } if (WHEEL_ELAPSED > WHEEL_UPDATE) { - if (j = 0) { + if (WHEEL_STATUS == 0) { for (int i = 0; i < 4; i++) { digitalWrite(WHEEL_SPEED_PINS[i], HIGH); } - j = 1; + WHEEL_STATUS = 1; } - if (j = 1) { + if (WHEEL_STATUS == 1) { for (int i = 0; i < 4; i++) { digitalWrite(WHEEL_SPEED_PINS[i], LOW); } - j = 0; + WHEEL_STATUS = 0; } WHEEL_LAST = millis(); } @@ -311,17 +344,17 @@ void randomTest() { ANALOG_LAST = millis(); } if (WHEEL_ELAPSED > WHEEL_UPDATE) { - if (j = 0) { + if (WHEEL_STATUS == 0) { for (int i = 0; i < 4; i++) { digitalWrite(WHEEL_SPEED_PINS[i], HIGH); } - j = 1; + WHEEL_STATUS = 1; } - if (j = 1) { + if (WHEEL_STATUS == 1) { for (int i = 0; i < 4; i++) { digitalWrite(WHEEL_SPEED_PINS[i], LOW); } - j = 0; + WHEEL_STATUS = 0; } WHEEL_LAST = millis(); } From 8ae066ab747cd68c7d9fab6f4c7401e17ee5591a Mon Sep 17 00:00:00 2001 From: "Landon Wheeler'ls.git config --global user.email lwheeler836647@gmail.comgit config --global user.name Landon" <144401916+lwheeler3@users.noreply.github.com> Date: Tue, 29 Oct 2024 19:35:04 -0600 Subject: [PATCH 7/7] Updates to for correct functionality after initial testing --- include/FlatCar.h | 39 ++++---- src/FlatCar.cpp | 242 +++++++++++++++++++++++++++------------------- 2 files changed, 163 insertions(+), 118 deletions(-) diff --git a/include/FlatCar.h b/include/FlatCar.h index b7e01a9..0b2a2f0 100644 --- a/include/FlatCar.h +++ b/include/FlatCar.h @@ -1,31 +1,34 @@ #ifndef FLATCAR_H #define FLATCAR_H -extern bool CAN_SNIFF; -extern char USER_INPUT; +extern bool canSniffState; +extern int USER_INPUT; extern float SPEED; extern float RPS; -extern unsigned long SPEED_UPDATE; -extern int BRAKE_VAL; -extern int THROTTLE_VAL; -extern float TARGET_RUN_TIME; // In minutes when selected by the user -extern unsigned long RUN_TIME; // Target run time of user in millis -extern unsigned long CURRENT_TIME; +extern unsigned long speedUpdate; +extern int brakeVal; +extern int throttleVal; +extern float targetRunTime; // In minutes when selected by the user +extern unsigned long runTime; // Target run time of user in millis +extern unsigned long currentTime; extern unsigned long DIGITAL_UPDATE; // How often Digital sensors should be updated -extern unsigned long DIGITAL_ELAPSED; -extern unsigned long DIGITAL_LAST; +extern unsigned long digitalElapsed; +extern unsigned long digitalLast; extern unsigned long WHEEL_UPDATE; -extern unsigned long WHEEL_ELAPSED; -extern unsigned long WHEEL_LAST; +extern unsigned long wheelElapsed; +extern unsigned long wheelLast; extern unsigned long ANALOG_UPDATE; -extern unsigned long ANALOG_ELAPSED; -extern unsigned long ANALOG_LAST; -extern long RANDOM_ANALOG; +extern unsigned long analogElapsed; +extern unsigned long analogLast; +extern long randomAnalog; extern unsigned long CAN_UPDATE; -extern unsigned long CAN_ELAPSED; -extern unsigned long CAN_LAST; +extern unsigned long canElapsed; +extern unsigned long canLast; +extern unsigned long CAN_SNIFF_UPDATE; +extern unsigned long canSniffElapsed; +extern unsigned long canSniffLast; -extern bool WHEEL_STATUS; // Used to store current wheel sensor status +extern bool wheelStatus; // Used to store current wheel sensor status extern const int WHEEL_SPEED_PINS[]; extern int START_SWITCH_PIN; diff --git a/src/FlatCar.cpp b/src/FlatCar.cpp index 3c0bd8b..8e13d2e 100644 --- a/src/FlatCar.cpp +++ b/src/FlatCar.cpp @@ -5,48 +5,48 @@ FlexCAN_T4 CORE_CAN; FlexCAN_T4 AUX_CAN; -bool CAN_SNIFF = false; -char USER_INPUT = 0; +bool canSniffState = false; +int USER_INPUT = 0; float SPEED = 0.0; float RPS = 0.0; -unsigned long SPEED_UPDATE = 0; -int BRAKE_VAL = 0; -int THROTTLE_VAL = 0; -float TARGET_RUN_TIME = 0.0; -unsigned long RUN_TIME = 0; -unsigned long CURRENT_TIME = 0; +unsigned long speedUpdate = 0; +int brakeVal = 0; +int throttleVal = 0; +float targetRunTime = 0.0; +unsigned long runTime = 0; +unsigned long currentTime = 0; unsigned long DIGITAL_UPDATE = 0; -unsigned long DIGITAL_ELAPSED = 0; -unsigned long DIGITAL_LAST = 0; +unsigned long digitalElapsed = 0; +unsigned long digitalLast = 0; unsigned long WHEEL_UPDATE = 0; -unsigned long WHEEL_ELAPSED = 0; -unsigned long WHEEL_LAST = 0; +unsigned long wheelElapsed = 0; +unsigned long wheelLast = 0; unsigned long ANALOG_UPDATE = 0; -unsigned long ANALOG_ELAPSED = 0; -unsigned long ANALOG_LAST = 0; -long RANDOM_ANALOG = 0; +unsigned long analogElapsed = 0; +unsigned long analogLast = 0; +long randomAnalog = 0; unsigned long CAN_UPDATE = 0; -unsigned long CAN_ELAPSED = 0; -unsigned long CAN_LAST = 0; -bool WHEEL_STATUS = false; +unsigned long canElapsed = 0; +unsigned long canLast = 0; +unsigned long CAN_SNIFF_UPDATE = 0; +unsigned long canSniffElapsed = 0; +unsigned long canSniffLast = 0; +bool wheelStatus = false; const int WHEEL_SPEED_PINS[] = {2, 3, 4, 5}; -int START_SWITCH_PIN = 6; -int BRAKE_1_PIN = 14; +int START_SWITCH_PIN = 26; +int BRAKE_1_PIN = 27; int TRACTIVE_PIN = 9; -int THROTTLE_1_PIN = 15; -int THROTTLE_2_PIN = 16; +int THROTTLE_1_PIN = 37; +int THROTTLE_2_PIN = 38; const int NUM_WHEEL_SPEED_PINS = sizeof(WHEEL_SPEED_PINS) / sizeof(WHEEL_SPEED_PINS[0]); void canSetup() { CORE_CAN.begin(); - CORE_CAN.setBaudRate(500000); - Serial.println("CORE CAN BUS INITALIZED"); - + CORE_CAN.setBaudRate(250000); AUX_CAN.begin(); - AUX_CAN.setBaudRate(500000); - Serial.println("AUX CAN BUS INITIALIZED"); + AUX_CAN.setBaudRate(250000); } @@ -88,30 +88,32 @@ void pinSetup() { void canSniff() { CAN_message_t msg; - if (CORE_CAN.read(msg)) { Serial.print("[CORE CAN] ID: "); Serial.print(msg.id); - Serial.print(" LEGNTH: "); + Serial.print(" LENGTH: "); Serial.print(msg.len); Serial.print(" DATA: "); for (uint8_t i = 0; i < msg.len; i++) { Serial.print(msg.buf[i]); Serial.print(" "); } + Serial.println(); } if (AUX_CAN.read(msg)) { Serial.print("[AUX CAN] ID: "); Serial.print(msg.id); - Serial.print(" LEGNTH: "); + Serial.print(" LENGTH: "); Serial.print(msg.len); Serial.print(" DATA: "); for (uint8_t i = 0; i < msg.len; i++) { Serial.print(msg.buf[i]); Serial.print(" "); } + Serial.println(); } + canSniffLast = millis(); } void menuInit() { @@ -132,42 +134,45 @@ void menuSelect() { while (!Serial.available()) { // Wait for serial input } - char USER_INPUT = Serial.read(); + USER_INPUT = Serial.read() - '0'; + Serial.println(USER_INPUT); Serial.print("TARGET RUN TIME (MIN): "); while (!Serial.available()) { // Wait for serial input } - float RUN_TIME = Serial.read(); + targetRunTime = Serial.parseFloat(); + Serial.println(targetRunTime); - Serial.print("TOGGLE CAN SNIFF"); + Serial.print("TOGGLE CAN SNIFF (1/0): "); while (!Serial.available()) { // Wait for serial input } - bool CAN_SNIFF = Serial.read(); + canSniffState = (Serial.read() == '1'); + Serial.println(canSniffState); switch (USER_INPUT) { - case '1': + case 1: Serial.println("SELECTED: STATIC VALUE TEST"); Serial.print("TARGET RUN TIME (MIN): "); - Serial.print(RUN_TIME); + Serial.println(targetRunTime); break; - case '2': + case 2: Serial.println("SELECTED: VARIABLE VALUE TEST"); Serial.print("TARGET RUN TIME (MIN): "); - Serial.print(RUN_TIME); + Serial.println(targetRunTime); break; - case '3': + case 3: Serial.println("SELECTED: RANDOM VALUE TEST"); Serial.print("TARGET RUN TIME (MIN): "); - Serial.print(RUN_TIME); + Serial.println(targetRunTime); break; - case '4': + case 4: Serial.println("SELECTED: RUN SIM"); Serial.print("TARGET RUN TIME (MIN): "); - Serial.print(RUN_TIME); + Serial.println(targetRunTime); break; - case '5': + case 5: Serial.println("EXITING..."); break; default: @@ -178,14 +183,21 @@ void menuSelect() { void runPrograms() { switch (USER_INPUT) { - case '1': + case 1: + Serial.println(">>>STARTING STATIC VALUE TEST<<<"); staticTest(); - case '2': + break; + case 2: + Serial.println(">>>STARTING VARIABLE VALUE TEST<<<"); variableTest(); - case '3': + break; + case 3: + Serial.println(">>>STARTING RANDOM VALUE TEST<<<"); randomTest(); - case '4': + break; + case 4: simTest(); + break; } } @@ -211,7 +223,7 @@ void staticTest() { analogWrite(THROTTLE_1_PIN, 127); analogWrite(THROTTLE_2_PIN, 127); - Serial.println("STATIC TEST COMPLETE"); + Serial.println("<<>>"); while (Serial.available()) { // wait for Serial input } @@ -219,92 +231,106 @@ void staticTest() { void updateTimes() { // Update all times to get current elapse times - CURRENT_TIME = millis(); - DIGITAL_ELAPSED = CURRENT_TIME - DIGITAL_LAST; - ANALOG_ELAPSED = CURRENT_TIME - ANALOG_LAST; - WHEEL_ELAPSED = CURRENT_TIME - WHEEL_LAST; - CAN_ELAPSED = CURRENT_TIME - CAN_LAST; + currentTime = millis(); + digitalElapsed = currentTime - digitalLast; + analogElapsed = currentTime - analogLast; + wheelElapsed = currentTime - wheelLast; + canElapsed = currentTime - canLast; + canSniffElapsed = currentTime - canSniffLast; } void updateRates() { - RUN_TIME = TARGET_RUN_TIME * 60000; DIGITAL_UPDATE = 1000; ANALOG_UPDATE = 100; WHEEL_UPDATE = 350; // Wheel update value for 10 mph initial start CAN_UPDATE = 100; - BRAKE_VAL = 0; - THROTTLE_VAL = 0; + brakeVal = 0; + throttleVal = 0; SPEED = 10; - SPEED_UPDATE = 5000; + speedUpdate = 5000; + CAN_SNIFF_UPDATE = 15; } void variableTest() { startSequence(); updateRates(); + runTime = currentTime + (targetRunTime * 60000); + + updateTimes(); + + Serial.println("VARIABLE TEST INITALIZED"); + Serial.print("TARGET_RUN_TIME: "); + Serial.println(targetRunTime); + Serial.print("RUN_TIME: "); + Serial.println(runTime); + Serial.print("CURRENT_TIME: "); + Serial.println(currentTime); - while (CURRENT_TIME < RUN_TIME) { + while (currentTime < runTime) { updateTimes(); - if (CAN_SNIFF) { + if (canSniffElapsed >= CAN_SNIFF_UPDATE && canSniffState) { canSniff(); + canLast = millis(); } - if (CURRENT_TIME >= SPEED_UPDATE) { + if (currentTime >= speedUpdate) { SPEED++; RPS = (SPEED * 1.4667) / (3.141592653589793 * 1.33333333); WHEEL_UPDATE = 1 / (RPS / 10); if (SPEED == 120) { SPEED = 0; } - SPEED_UPDATE += 5000; + speedUpdate += 5000; } - if (DIGITAL_ELAPSED > DIGITAL_UPDATE) { + if (digitalElapsed > DIGITAL_UPDATE) { digitalWrite(TRACTIVE_PIN, HIGH); digitalWrite(START_SWITCH_PIN, HIGH); - DIGITAL_LAST = millis(); + digitalLast = millis(); } - if (ANALOG_ELAPSED > ANALOG_UPDATE) { + if (analogElapsed > ANALOG_UPDATE) { // Writes values to pins - analogWrite(BRAKE_1_PIN, BRAKE_VAL); - analogWrite(THROTTLE_1_PIN, THROTTLE_VAL); - analogWrite(THROTTLE_2_PIN, 255 - THROTTLE_VAL); + analogWrite(BRAKE_1_PIN, brakeVal); + analogWrite(THROTTLE_1_PIN, throttleVal); + analogWrite(THROTTLE_2_PIN, 255 - throttleVal); // Incriment +1 each cycle - BRAKE_VAL++; - THROTTLE_VAL++; + brakeVal++; + throttleVal++; // Resets values at 255 - if (BRAKE_VAL == 255) { - BRAKE_VAL = 0; + if (brakeVal == 255) { + brakeVal = 0; } - if (THROTTLE_VAL == 255) { - THROTTLE_VAL = 0; + if (throttleVal == 255) { + throttleVal = 0; } - // Updates last rut time - ANALOG_LAST = millis(); + // Updates last run time + analogLast = millis(); } - if (WHEEL_ELAPSED > WHEEL_UPDATE) { - if (WHEEL_STATUS == 0) { + if (wheelElapsed > WHEEL_UPDATE) { + if (wheelStatus == 0) { for (int i = 0; i < 4; i++) { digitalWrite(WHEEL_SPEED_PINS[i], HIGH); } - WHEEL_STATUS = 1; + wheelStatus = 1; } - if (WHEEL_STATUS == 1) { + if (wheelStatus == 1) { for (int i = 0; i < 4; i++) { digitalWrite(WHEEL_SPEED_PINS[i], LOW); } - WHEEL_STATUS = 0; + wheelStatus = 0; } - WHEEL_LAST = millis(); + wheelLast = millis(); } - // if (CAN_ELAPSED > CAN_UPDATE) { + + // if (canElapsed > CAN_UPDATE) { // CAN_LAST = millis(); // } @@ -317,48 +343,64 @@ void variableTest() { void randomTest() { startSequence(); updateRates(); + runTime = currentTime + (targetRunTime * 60000); - while (CURRENT_TIME < RUN_TIME) { + updateTimes(); + + Serial.println("VARIABLE TEST INITALIZED"); + Serial.print("TARGET_RUN_TIME: "); + Serial.println(targetRunTime); + Serial.print("RUN_TIME: "); + Serial.println(runTime); + Serial.print("CURRENT_TIME: "); + Serial.println(currentTime); + + while (currentTime < runTime) { updateTimes(); - if (CAN_SNIFF) { + if (canSniffElapsed >= CAN_SNIFF_UPDATE && canSniffState) { canSniff(); + Serial.println("CAN SNIFFED!"); + canLast = millis(); } - if (DIGITAL_ELAPSED > DIGITAL_UPDATE) { + if (digitalElapsed > DIGITAL_UPDATE) { digitalWrite(TRACTIVE_PIN, HIGH); digitalWrite(START_SWITCH_PIN, HIGH); - DIGITAL_LAST = millis(); + digitalLast = millis(); } - if (ANALOG_ELAPSED > ANALOG_UPDATE) { - RANDOM_ANALOG = random(0, 255); + if (analogElapsed > ANALOG_UPDATE) { + randomAnalog = random(0, 255); - analogWrite(BRAKE_1_PIN, RANDOM_ANALOG); + analogWrite(BRAKE_1_PIN, randomAnalog); - RANDOM_ANALOG = random(0, 255); - analogWrite(THROTTLE_1_PIN, RANDOM_ANALOG); - analogWrite(THROTTLE_2_PIN, 255 - RANDOM_ANALOG); + randomAnalog = random(0, 255); + analogWrite(THROTTLE_1_PIN, randomAnalog); + analogWrite(THROTTLE_2_PIN, 255 - randomAnalog); - ANALOG_LAST = millis(); + analogLast = millis(); } - if (WHEEL_ELAPSED > WHEEL_UPDATE) { - if (WHEEL_STATUS == 0) { + if (wheelElapsed > WHEEL_UPDATE) { + if (wheelStatus == 0) { for (int i = 0; i < 4; i++) { digitalWrite(WHEEL_SPEED_PINS[i], HIGH); } - WHEEL_STATUS = 1; + wheelStatus = 1; } - if (WHEEL_STATUS == 1) { + if (wheelStatus == 1) { for (int i = 0; i < 4; i++) { digitalWrite(WHEEL_SPEED_PINS[i], LOW); } - WHEEL_STATUS = 0; + wheelStatus = 0; } - WHEEL_LAST = millis(); + wheelLast = millis(); } - // if (CAN_ELAPSED > CAN_UPDATE) { + Serial.println("<<>>"); + Serial.println(currentTime / 1000); + + // if (canElapsed > CAN_UPDATE) { // CAN_LAST = millis(); // }