Skip to content

Commit

Permalink
Merge branch 'release/v1.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
matteocarnelos committed Feb 17, 2022
2 parents 39d1539 + 73752a3 commit 8abfc09
Show file tree
Hide file tree
Showing 8 changed files with 315 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,7 @@ $RECYCLE.BIN/
*.lnk

# End of https://www.toptal.com/developers/gitignore/api/c++,macos,linux,windows,clion+all,platformio,visualstudiocode

### VisualStudioCode Patch ###
# Ignore .vscode folder
.vscode
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "lib/sailtrack-sensor"]
path = lib/sailtrack-sensor
url = https://github.com/metis-vela-unipd/sailtrack-sensor.git
39 changes: 39 additions & 0 deletions include/README
Original file line number Diff line number Diff line change
@@ -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 changes: 46 additions & 0 deletions lib/README
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions lib/sailtrack-sensor
Submodule sailtrack-sensor added at b92eb9
23 changes: 23 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
; 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:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200
lib_deps = PubSubClient, bblanchon/ArduinoJson

[env:T7]
platform = espressif32
board = ttgo-t7-v14-mini32
framework = arduino
monitor_speed = 115200
lib_deps = PubSubClient, bblanchon/ArduinoJson
188 changes: 188 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
#include <Arduino.h>

#include <ArduinoJson.h>

#include <freertos/queue.h>

#include <wifi/s_wifi.h>
#include <mqtt/s_mqtt.h>

#define ECHO1 35
#define ECHO2 33
#define ECHO3 34

#define TRIGGER1 18
#define TRIGGER2 19
#define TRIGGER3 23

#define DEBUG 15

#define LENGTH .475 //[m]

#define SENSOR_N 3
#define FILTER_N 100

#define SPD(m1, m2) .5 * LENGTH * 1e6 * (FILTER_N / m1 - FILTER_N / m2)


void measureTask(void *parameter);
static void ICACHE_RAM_ATTR changeISR();

double m_carr1[FILTER_N] = {0};
double m_carr2[FILTER_N] = {0};
double m_carr3[FILTER_N] = {0};
double m_carr4[FILTER_N] = {0};
double m_carr5[FILTER_N] = {0};
double m_carr6[FILTER_N] = {0};


int i = 0;

int cpu_freq = 0;

// ISR variables
volatile unsigned long cpuTimeRising = 0;
volatile unsigned long elapsedCpuTime = 0;
volatile unsigned long cpuTimePlaceholder = 0;
volatile unsigned long ToF = 0;
volatile bool evalFlag = false;

QueueHandle_t raw_measure_queue, measure_queue;

esp_mqtt_client_handle_t cl;


const int capacity = JSON_OBJECT_SIZE(3);
StaticJsonDocument<capacity> doc;

void setup()
{
// Enable serial communication
Serial.begin(115200);

setup_wifi();
cl = setup_mqtt();

// Get cpu frequency
cpu_freq = ESP.getCpuFreqMHz();
Serial.println(cpu_freq);

// Setp PINs and interrupts
pinMode(TRIGGER1, OUTPUT);
pinMode(TRIGGER2, OUTPUT);
pinMode(TRIGGER3, OUTPUT);
pinMode(ECHO1, INPUT);
pinMode(ECHO2, INPUT);
pinMode(ECHO3, INPUT);

pinMode(DEBUG, OUTPUT);

// Create queues
raw_measure_queue = xQueueCreate(1, sizeof(unsigned long));
measure_queue = xQueueCreate(1, sizeof(double[3][3]));

// Create tasks
xTaskCreate(measureTask, "measureTask", 10000, NULL, 1, NULL);
}

void loop()
{
double measure[SENSOR_N*2];
if (xQueueReceive(measure_queue, &measure, portMAX_DELAY) == pdPASS)
{
m_carr1[i] = measure[0];
m_carr2[i] = measure[1];
m_carr3[i] = measure[2];
m_carr4[i] = measure[3];
m_carr5[i] = measure[4];
m_carr6[i] = measure[5];

double s1 = 0, s2 = 0, s3 = 0, s4 = 0, s5 = 0, s6 = 0;

for (size_t j = 0; j < FILTER_N; j++)
{
s1 += m_carr1[j];
s2 += m_carr2[j];
s3 += m_carr3[j];
s4 += m_carr4[j];
s5 += m_carr5[j];
s6 += m_carr6[j];
}

doc["A"] = SPD(s1, s2);
doc["B"] = SPD(s3, s4);
doc["C"] = SPD(s6, s5);

// serializeJson(doc, Serial);

// Serial.printf("{\"Measure A\":%e,\n\"Measure B\":%e,\n\"Measure C:\"%e\n}", SPD(s1, s2), SPD(s3, s4), SPD(s6, s5));

char payload[200];
// sprintf(payload, "{\"Measure A\":%e,\n\"Measure B\":%e,\n\"Measure C:\"%e\n}", SPD(s1, s2), SPD(s3, s4), SPD(s6, s5));
serializeJson(doc, payload);
esp_mqtt_client_publish(cl, "/topic/Measure A:", payload, 0, 0, 0);

i = (i + 1) % FILTER_N;
}
}

void measureTask(void *parameter)
{
TickType_t xLastWakeTime;
const TickType_t xFrequency = 10 / portTICK_RATE_MS;

xLastWakeTime = xTaskGetTickCount();

unsigned long raw_elapsedCpuTime = 0;
double measure[SENSOR_N*2];

unsigned int echo[] = {ECHO1, ECHO2, ECHO3, ECHO2, ECHO1, ECHO3};
unsigned int trig1[] = {TRIGGER1, TRIGGER1, TRIGGER2, TRIGGER2, TRIGGER3, TRIGGER3};
unsigned int trig2[] = {TRIGGER2, TRIGGER2, TRIGGER3, TRIGGER3, TRIGGER1, TRIGGER1};

// unsigned int echo[] = {ECHO1};
// unsigned int trig1[] = {TRIGGER1};
// unsigned int trig2[] = {TRIGGER2};

unsigned int idx = 0;

for (;;)
{
vTaskDelayUntil(&xLastWakeTime, xFrequency);

attachInterrupt(digitalPinToInterrupt(echo[idx]), changeISR, CHANGE);

digitalWrite(trig1[idx], HIGH);
digitalWrite(trig2[idx], HIGH);
delayMicroseconds(20);
digitalWrite(trig1[idx], LOW);
digitalWrite(trig2[idx], LOW);
// Serial.printf("Pulse %i \n", idx);

if (xQueueReceive(raw_measure_queue, &raw_elapsedCpuTime, portMAX_DELAY) == pdPASS)
{
cpuTimeRising = raw_elapsedCpuTime;
}
if (xQueueReceive(raw_measure_queue, &raw_elapsedCpuTime, portMAX_DELAY) == pdPASS)
{
elapsedCpuTime = (raw_elapsedCpuTime - cpuTimeRising);
detachInterrupt(digitalPinToInterrupt(echo[idx]));
// Serial.printf("%d, %e \n", idx, (double) elapsedCpuTime/ (1000 * cpu_freq));
measure[idx] = elapsedCpuTime / cpu_freq; // [us]
}

if (idx == SENSOR_N*2 - 1)
xQueueSendToFront(measure_queue, (void *)&measure, 100 / portTICK_RATE_MS);

idx = (idx + 1) % (SENSOR_N*2);
taskYIELD();
}
}

static void ICACHE_RAM_ATTR changeISR()
{
//gets cpu timing when echo pin changes logic state
cpuTimePlaceholder = ESP.getCycleCount(); //get cpu time before evaluating if statement
// digitalWrite(DEBUG, !digitalRead(DEBUG));
xQueueSendToBackFromISR(raw_measure_queue, (void *)&cpuTimePlaceholder, pdFALSE);
}
11 changes: 11 additions & 0 deletions test/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

This directory is intended for PlatformIO Unit Testing 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/page/plus/unit-testing.html

0 comments on commit 8abfc09

Please sign in to comment.