ADD: SimpleFOC native library and port arduino core to project #15 #7

STAGING

REBORN NEW ARDUINO CORE

ADD: simple FOC, AS5040 driver, commands, work with gui
This commit is contained in:
vanyabeat 2024-02-20 11:26:25 +03:00 committed by Igor Brylyov
parent bd7f049b75
commit 1d998ea45b
42 changed files with 265 additions and 5509 deletions

View file

@ -0,0 +1,36 @@
#include "AS5040.h"
AS5040Sensor::AS5040Sensor(uint16_t cs, uint16_t mosi, uint16_t miso, uint16_t sck) {
cs_ = cs;
mosi_ = mosi;
miso_ = miso;
sck_ = sck;
}
void AS5040Sensor::init(SPIClass *_spi) {
spi = _spi;
// 1MHz clock (AMS should be able to accept up to 10MHz)
settings = SPISettings(1000000, MSBFIRST, SPI_MODE0);
//setup pins
pinMode(cs_, OUTPUT);
pinMode(miso_, INPUT);
pinMode(mosi_, OUTPUT);
pinMode(sck_, OUTPUT);
spi->setMISO(miso_);
spi->setMOSI(mosi_);
spi->setSCLK(sck_);
spi->begin();
this->Sensor::init(); // call base class init
}
float AS5040Sensor::getSensorAngle() {
//SPI - begin transaction
spi->beginTransaction(settings);
//Send the command
digitalWrite(cs_, LOW);
uint16_t data = spi->transfer16(0x3FFF);
digitalWrite(cs_,HIGH);
//SPI - end transaction
spi->endTransaction();
data = (data) >> 5;
return (float)data * _2PI / cpr;
}

View file

@ -0,0 +1,26 @@
#pragma once
#include "SimpleFOC.h"
class AS5040Sensor : public Sensor {
public:
AS5040Sensor(uint16_t cs, uint16_t mosi, uint16_t miso, uint16_t sck);
/** sensor initialise pins */
void init(SPIClass *_spi = &SPI);
// implementation of abstract functions of the Sensor class
/** get current angle (rad) */
float getSensorAngle() override;
private:
uint16_t cs_ = 0;
uint16_t mosi_ = 0;
uint16_t miso_ = 0;
uint16_t sck_ = 0;
float cpr = 1024;
SPISettings settings;
SPIClass *spi;
};

46
firmware/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