Refactor code

This commit is contained in:
2021-05-13 16:03:07 +03:00
parent 9f3ffaba30
commit b4adcc4dee
10 changed files with 347 additions and 243 deletions

View File

@@ -1,4 +1,4 @@
#define SEALEVELPRESSURE_HPA (1013.25)
#include <constants.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <ArduinoJson.h>
@@ -15,14 +15,16 @@
bool deployed = 0;
void deploy() {
digitalWrite(PWM, HIGH);
delay(1000);
digitalWrite(PWM, LOW);
delay(1000);
void deploy()
{
digitalWrite(PWM, HIGH);
delay(1000);
digitalWrite(PWM, LOW);
delay(1000);
}
void TaskDeploycode( void * pvParameters ){
void TaskDeploycode(void *pvParameters)
{
deploy();
deployed = 1;
}
@@ -36,7 +38,8 @@ bool save = false;
float previous_height;
bool altitude = false;
void setup(){
void setup()
{
Serial.begin(115200);
Wire.setClock(800000);
setup_sdcard();
@@ -50,20 +53,23 @@ void setup(){
Serial.println("Configuring access point...");
connectToWiFi(networkName, networkPswd);
ESP_ERROR_CHECK(esp_wifi_set_protocol (WIFI_IF_STA, WIFI_PROTOCOL_11B));
ESP_ERROR_CHECK(esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_11B));
wifi_country_t country_info = {"JP", 1, 14, WIFI_COUNTRY_POLICY_MANUAL};
ESP_ERROR_CHECK(esp_wifi_set_country(&country_info));
ESP_ERROR_CHECK(esp_wifi_set_country(&country_info));
ESP_ERROR_CHECK(esp_wifi_set_max_tx_power(80));
ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_NONE));
pinMode(PWM, OUTPUT);
uint8_t buffer[50] = "test.txt";
while(1) {
if (connected) {
bmp.performReading();
original_height = bmp.readAltitude(SEALEVELPRESSURE_HPA);
while (1)
{
if (connected)
{
bmp.performReading();
original_height = bmp.readAltitude(SEALEVELPRESSURE_HPA);
udp.parsePacket();
if(udp.read(buffer, 50) > 0){
if (udp.read(buffer, 50) > 0)
{
Serial.print("Server to client: ");
Serial.println((char *)buffer);
start = millis();
@@ -78,10 +84,11 @@ void setup(){
Serial.println(original_height);
}
void loop(){
void loop()
{
DynamicJsonDocument doc(1024);
JsonObject obj = doc.to<JsonObject>();
obj["seconds"] = millis()/1000;
obj["seconds"] = millis() / 1000;
obj["deployed"] = deployed;
obj["save"] = save;
obj["flight"] = flight;
@@ -95,32 +102,37 @@ void loop(){
file.print('\n');
file.flush();
//only send data when connected
if(connected){
if (connected)
{
//Send a packet
udp.beginPacket(udpAddress,udpPort);
udp.beginPacket(udpAddress, udpPort);
serializeJson(doc, udp);
udp.println();
udp.endPacket();
}
if (!save & !deployed & ((obj["bmp388"]["altitude"].as<float>() - original_height) > 50.0)) {
if (!save & !deployed & ((obj["bmp388"]["altitude"].as<float>() - original_height) > ALTITUDE_THRESHOLD))
{
Serial.println("Deploy is save");
save = true;
}
if (save & !deployed & (((obj["bmp388"]["altitude"].as<float>() - previous_height) * 5) < 3.0)) {
if (save & !deployed & (((obj["bmp388"]["altitude"].as<float>() - previous_height) * 5) < PRESSURE_THRESHOLD))
{
Serial.println("Start deploy altitude");
altitude = true;
deployed = true;
digitalWrite(PWM, HIGH);
}
if (!flight & (max(max(abs(obj["bno055"]["linear_accel_data"]["x"].as<float>()),
abs(obj["bno055"]["linear_accel_data"]["y"].as<float>())),
max(abs(obj["bno055"]["linear_accel_data"]["y"].as<float>()),
abs(obj["bno055"]["linear_accel_data"]["z"].as<float>()))) > 5.0)) {
abs(obj["bno055"]["linear_accel_data"]["y"].as<float>())),
max(abs(obj["bno055"]["linear_accel_data"]["y"].as<float>()),
abs(obj["bno055"]["linear_accel_data"]["z"].as<float>()))) > START_ACCELERATION_THRESHOLD))
{
Serial.println("Timer trigger");
start = millis();
flight = true;
}
if(flight & ((millis()-start) > 8*1000) & !deployed) { // Вот таймер
if (flight & ((millis() - start) > TIME_TO_DEPLOY) & !deployed)
{ // Вот таймер
Serial.println("Start deploy timeout");
deployed = 1;
digitalWrite(PWM, HIGH);