From eaf2bb62bdbba476c5abbdbf86dfd6d4d35dd6fb Mon Sep 17 00:00:00 2001 From: NMSTEC <45252464+NMSTEC@users.noreply.github.com> Date: Wed, 24 Aug 2022 10:42:16 -0700 Subject: [PATCH] Add files via upload --- include/README | 39 +++++++++++++ lib/README | 46 +++++++++++++++ platformio.ini | 15 +++++ src/main.cpp | 106 +++++++++++++++++++++++++++++++++++ src/workspace.code-workspace | 12 ++++ test/README | 11 ++++ 6 files changed, 229 insertions(+) create mode 100644 include/README create mode 100644 lib/README create mode 100644 platformio.ini create mode 100644 src/main.cpp create mode 100644 src/workspace.code-workspace create mode 100644 test/README diff --git a/include/README b/include/README new file mode 100644 index 0000000..45496b1 --- /dev/null +++ b/include/README @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..8c9c29c --- /dev/null +++ b/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into executable file. + +The source code of each library should be placed in a an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> 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/platformio.ini b/platformio.ini new file mode 100644 index 0000000..2af6d50 --- /dev/null +++ b/platformio.ini @@ -0,0 +1,15 @@ +; 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:uno] +platform = atmelavr +board = uno +framework = arduino +lib_deps = coryjfowler/mcp_can@^1.5.0 diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..b85866b --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,106 @@ +/* IMPORTANT: USE ARDUINO PIN 3 FOR PWM INPUT. Also make sure your input is protected by at least a resistor, 100kohm, if its a 5v pwm signal. If 12v you may want a level shifter +or at minimum a voltage divider. + +MAKE SURE TO SET OUTPUT MODE ON HALTECH TO DUTY CYCLE. +*/ + +#include "mcp_can.h" +#include + + +MCP_CAN CAN(9); //default for those can bus shields +#define CAN_INT 2 //default for those can bus shields +char msgString[128];//terrible way of comparing data +long unsigned int rxId;//ID of incoming message +long unsigned int hi = 0x1B200002; +unsigned char len = 0;//not used here, since we dont care about contents of message +unsigned char rxBuf[8];//not used here, since we dont care about contents of message +unsigned char keepAlive[8] = {0x00, 0x00, 0x22, 0xE0, 0x41, 0x90, 0x00, 0x00};//counter KA +unsigned char speed[8] = {0xBB, 0x00, 0x3F, 0xFF, 0x06, 0xE0, 0x00, 0x00};//counter KA +unsigned char slowRollTable[4] = { 0x00, 0x40, 0x80, 0xC0}; +unsigned long previousMillis = 0; +const long interval = 15;//how fast we send +int slowCounter; //Which number in array we at +int slowRoll; +int roll;//how many data messages we're sent, 0-30 +volatile unsigned long int pwm_value = 0; +volatile unsigned long int prev_time = 0; +bool oHi = false; +unsigned long calVel; + +void rising() { + attachInterrupt(1, falling, FALLING); + prev_time = micros(); + +} + +void falling() { + attachInterrupt(1, rising, RISING); + pwm_value = micros()-prev_time; + + calVel = map(pwm_value, 0, 4600, 6000, 0);//THIS IS ASSUMING 200HZ + + if (calVel > 6000)//Due to Arduino uno being 8 bit, this causes a quick overflow so we do this ugly little patch + { + calVel = 0; + } + /*USE THESE ONLY FOR WHEN YOUR OUTPUT FREQUENCY IS NOT 200hz. Most haltechs are 200-500, which god knows which one will work. If any issues, contact me. + Serial.print("pwm "); + Serial.println(pwm_value); + Serial.print("mapped "); + Serial.println(calVel);*/ +} + +void setup() +{ + + Serial.begin(115200); + attachInterrupt(1, rising, RISING);//Uno Pin 3 for input + if(CAN.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK) Serial.print("CAN init ok\r\n"); + else Serial.print("CAN init failed\r\n"); + CAN.setMode(MCP_NORMAL); + pinMode(CAN_INT, INPUT); + +} + +void loop() +{ + + unsigned long currentMillis = millis(); +speed[6] = calVel / 256; +speed[7] = calVel; + + if (oHi == true){ + if (currentMillis - previousMillis >= interval) { + previousMillis = currentMillis; + if (roll == 0) + { + CAN.sendMsgBuf(0x1AE0092c, 1, 8, keepAlive);//send keep alive with roll + keepAlive[0] = slowRollTable[slowRoll]; + slowRoll++; + if (slowRoll == 4){slowRoll = 0;}//reset slowroll + } + + CAN.sendMsgBuf(0x2104136, 1, 8, speed); //send speed + roll++; + if (roll == 30){roll = 0;}//reset roll + } + } + + + if (!digitalRead(CAN_INT))//Wait for some Can data to come in + { + CAN.readMsgBuf(&rxId, &len, rxBuf); + + if ((rxId & 0x80000000) == 0x80000000) + sprintf(msgString, "%.8lX", (rxId & 0x1FFFFFFF)); + + if (!strcmp(msgString, "1B200002")){//This is a workaround since extID is 29 bit and arduino is 8 bit. + + oHi = true; + } + + } + + +} \ No newline at end of file diff --git a/src/workspace.code-workspace b/src/workspace.code-workspace new file mode 100644 index 0000000..4efa107 --- /dev/null +++ b/src/workspace.code-workspace @@ -0,0 +1,12 @@ +{ + "folders": [ + { + "path": ".." + }, + { + "name": "Firmware", + "path": "G:\\My Drive\\ECUs\\Older Projects Stand alone\\PRC\\Firmware" + } + ], + "settings": {} +} \ No newline at end of file diff --git a/test/README b/test/README new file mode 100644 index 0000000..b0416ad --- /dev/null +++ b/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