Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add blind spot #11

Open
wants to merge 5 commits into
base: v5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions components/blind_spot/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
idf_component_register(
SRCS "src/blind_spot.c"
INCLUDE_DIRS "include"
REQUIRES system driver
)
3 changes: 3 additions & 0 deletions components/blind_spot/idf_component.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version: "0.1.0"
description: "task example"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update description

url: "https://github.com/Parahybaja/firmware"
35 changes: 35 additions & 0 deletions components/blind_spot/include/task/blind_spot.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @file task_blind_spot.h
* @author Flaviano Medeiros da Silva Júnior ([email protected])
* @brief task blind spot
* @version 0.1
* @date 2024-01-22
*
* @copyright Copyright (c) 2024
*
* Version Modified By Date Comments
* ------- ------------- ---------- -----------
* 0.1.0 Flaviano Jr. 22/01/2024 first version
*
*/

#pragma once

#include "system.h"
#include "driver/gpio.h"

#ifdef __cplusplus
extern "C" {
#endif

#define TASK_BLIND_SPOT_SEND_RATE_Hz 1 // RPM task send rate in hertz

#define ACTIVE_LOW false
#define BLIND_SPOT_AVERAGE_POINTS 5
#define THRESHOLD 1 // to be calibrated

void task_blind_spot(void*);

#ifdef __cplusplus
}
#endif
39 changes: 39 additions & 0 deletions components/blind_spot/sketch_jan29a/sketch_jan29a.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <Arduino.h>

#define RX_PIN 3 // GPIO3 (RX0) como RX (pino de recepção)
#define TX_PIN 1 // GPIO1 como TX (pino de transmissão)
#define SERIAL_BAUD 9600 // Taxa de baud para comunicação serial

void setup() {
Serial.begin(SERIAL_BAUD);
}

void loop() {
getDistance();
delay(100); // Adiciona um pequeno atraso entre leituras
}

void getDistance() {
unsigned int distance;
byte startByte, h_data, l_data, sum = 0;
byte buf[3];

// Espera até que pelo menos 3 bytes estejam disponíveis
while (Serial.available() < 3) {
delay(10);
}

Serial.readBytes(buf, 3);
startByte = buf[0];
h_data = buf[1];
l_data = buf[2];
sum = h_data + l_data;

if (startByte == 255 && sum == buf[2]) {
distance = (h_data << 8) + l_data;
Serial.print("Distância [mm]: ");
Serial.println(distance);
} else {
Serial.println("Resultado inválido");
}
}
72 changes: 72 additions & 0 deletions components/blind_spot/src/blind_spot.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include "task/blind_spot.h"

static const char *TAG = "blind_spot";

void task_blind_spot(void *arg){
(void)arg;

const gpio_num_t gpio_pin = (gpio_num_t)arg;

// -----config gpio-----
//zero-initialize the config structure.
gpio_config_t io_conf = {};
io_conf.intr_type = GPIO_INTR_DISABLE;
io_conf.mode = GPIO_MODE_INPUT;
io_conf.pin_bit_mask = (1ULL<<gpio_pin);
io_conf.pull_down_en = false;
io_conf.pull_up_en = false;
ESP_ERROR_CHECK(gpio_config(&io_conf));

const int send_rate_ms = (int)(1000.0 / (float)(TASK_BLIND_SPOT_SEND_RATE_Hz));
uint32_t timer_send_ms;
float sum;
int last_value = 0;
sensor_t blind_sr = {TILT_Z, 0.0};
// sensor_t blind_sl = {TILT_X, 0.0};

task_remaining_space();

timer_send_ms = esp_log_timestamp();

for (;;)
{
if ((esp_log_timestamp() - timer_send_ms) >= send_rate_ms)
{
// -----add to timer-----
timer_send_ms += send_rate_ms;

// -----calculate-----
sum = 0; // clean sum buffer
for (int i=0; i < BLIND_SPOT_AVERAGE_POINTS; i++) {
if (ACTIVE_LOW)
sum += !gpio_get_level(gpio_pin);
else
sum += gpio_get_level(gpio_pin);
}
blind_sr.value = sum / (float)(BLIND_SPOT_AVERAGE_POINTS);

// ----- define high or low level -----
// comment out to send raw data fuel average
if (blind_sr.value <= THRESHOLD){ // low fuel level
blind_sr.value = 0; // active low fuel emergency flag
}
else {
blind_sr.value = 1;
}

// ----- send data just when is changed -----
if (blind_sr.value != last_value) {
// -----send fuel data through esp-now to receiver-----
ESP_LOGD(TAG, "Blind Spot, detected");
printf("Pin Level: %f\n",sum);
// esp_now_send(mac_address_ECU_rear, (uint8_t *) &blind_spot_right, sizeof(blind_spot_right));
// esp_now_send(mac_address_ECU_rear, (uint8_t *) &blind_spot_left, sizeof(blind_spot_left));
};

// ----- update last value -----
last_value = blind_sr.value;
}

vTaskDelay(pdMS_TO_TICKS(10)); // free up the processor
}
}