Add files via upload

This commit is contained in:
NMSTEC 2022-08-24 10:42:16 -07:00 committed by GitHub
commit eaf2bb62bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 229 additions and 0 deletions

39
include/README Normal file
View File

@ -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

46
lib/README Normal file
View File

@ -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 <Foo.h>
#include <Bar.h>
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

15
platformio.ini Normal file
View File

@ -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

106
src/main.cpp Normal file
View File

@ -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 <SPI.h>
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;
}
}
}

View File

@ -0,0 +1,12 @@
{
"folders": [
{
"path": ".."
},
{
"name": "Firmware",
"path": "G:\\My Drive\\ECUs\\Older Projects Stand alone\\PRC\\Firmware"
}
],
"settings": {}
}

11
test/README Normal file
View File

@ -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