Compare commits
2 Commits
4876d31648
...
160ee1ce2f
| Author | SHA1 | Date | |
|---|---|---|---|
| 160ee1ce2f | |||
| 0a56a1ec1c |
@@ -1,72 +0,0 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Unlicense OR CC0-1.0
|
||||
*/
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/semphr.h"
|
||||
|
||||
#include "esp_log.h"
|
||||
#include "driver/uart.h"
|
||||
|
||||
#include "esp_lcd_panel_ops.h"
|
||||
#include "driver/spi_common.h"
|
||||
#include "esp_lcd_panel_io.h"
|
||||
#include "esp_lcd_panel_commands.h"
|
||||
#include "esp_lcd_ili9341.h"
|
||||
#include "lvgl.h"
|
||||
|
||||
#include "dshot_esc_encoder.h"
|
||||
#include "display.h"
|
||||
#include "ui.h"
|
||||
#include "motor.h"
|
||||
|
||||
|
||||
static const char *TAG = "spincoat-plater-firmware";
|
||||
|
||||
static const uint16_t throttle = 100;
|
||||
|
||||
static const uint16_t OUTPUT_MIN = 0;
|
||||
static const uint16_t OUTPUT_MAX = 150;
|
||||
|
||||
static const float KP = 0.12; // Proportional gain
|
||||
static const float KI = 0.0003; // Integral gain
|
||||
static const float KD = 0; // Derivative gain
|
||||
|
||||
extern "C" void app_main(void) {
|
||||
srand((unsigned int)esp_timer_get_time());
|
||||
|
||||
esc_telemetry_t telemetry;
|
||||
uint16_t real_rpm = 0;
|
||||
|
||||
AutoPID myPID(&real_rpm, &throttle, OUTPUT_MIN, OUTPUT_MAX, KP, KI, KD);
|
||||
|
||||
init_display();
|
||||
|
||||
build_ui();
|
||||
|
||||
init_motor();
|
||||
update_throttle(throttle);
|
||||
|
||||
while(1) {
|
||||
send_dshot_packet();
|
||||
|
||||
uint8_t length = 0;
|
||||
ESP_ERROR_CHECK(uart_get_buffered_data_len(ESC_UART_NUM, (size_t*)&length));
|
||||
|
||||
if(length >= 10) {
|
||||
if(parse_telemetry(&telemetry)) {
|
||||
real_rpm = telemetry.rpm / (uint16_t) CONFIG_MOTOR_POLECOUNT;
|
||||
|
||||
myPID.run();
|
||||
//ESP_LOGI(TAG, "eRPM returned is: %d\n", telemetry.rpm);
|
||||
//ESP_LOGI(TAG, "real RPM returned is: %d\n", real_rpm);
|
||||
update_rpm_readout(real_rpm);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -29,7 +29,6 @@ extern "C" {
|
||||
#include "motor.h"
|
||||
}
|
||||
|
||||
|
||||
static const char *TAG = "spincoat-plater-firmware";
|
||||
|
||||
static double throttle = 0;
|
||||
@@ -52,20 +51,21 @@ extern "C" void app_main(void) {
|
||||
srand((unsigned int)esp_timer_get_time());
|
||||
|
||||
esc_telemetry_t telemetry;
|
||||
double target = 250;
|
||||
double target = 100;
|
||||
double real_rpm = 0;
|
||||
|
||||
AutoPID myPID(&real_rpm, &target, &throttle, OUTPUT_MIN, OUTPUT_MAX, KP, KI, KD);
|
||||
myPID.setTimeStep(100); // don't set too low or you'll blow the DC-DC converter
|
||||
myPID.setBangBang(400);
|
||||
myPID.setTimeStep(100);
|
||||
myPID.setBangBang(400); // if you mess with this, you may melt your circuit
|
||||
|
||||
init_display();
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
build_ui();
|
||||
|
||||
|
||||
init_motor();
|
||||
update_throttle(throttle);
|
||||
|
||||
|
||||
while(1) {
|
||||
send_dshot_packet();
|
||||
|
||||
|
||||
@@ -9,52 +9,112 @@
|
||||
|
||||
#define BTN_INCREMENT 100
|
||||
|
||||
#define MAX_RPM_SETTING 200
|
||||
#define MAX_COAT_TIME_SETTING 30
|
||||
|
||||
#define MIN_RPM_SETTING 50
|
||||
#define MIN_COAT_TIME_SETTING 5
|
||||
|
||||
#define RPM_STEPSIZE 25
|
||||
#define COAT_TIME_STEPSIZE 5
|
||||
|
||||
static const char *TAG = "spincoat-plater-firmware/ui";
|
||||
|
||||
static lv_obj_t * rpm_label = NULL;
|
||||
static lv_obj_t * coat_time_label = NULL;
|
||||
static lv_obj_t * run_stop_label = NULL;
|
||||
|
||||
static uint16_t rpm_setting = 100; // rpm
|
||||
static uint16_t coat_time_setting = 5; // seconds
|
||||
|
||||
static coat_state motor_state = STOPPED;
|
||||
|
||||
/**
|
||||
* Callback for pressing the top button
|
||||
* Callback for pressing the rpm increase button
|
||||
*/
|
||||
void top_cb(lv_event_t * e) {
|
||||
update_throttle(get_throttle() + BTN_INCREMENT);
|
||||
//lv_label_set_text_fmt(rpm_label, "RPM: %d", get_throttle());
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback for pressing the bottom button
|
||||
*/
|
||||
void bottom_cb(lv_event_t * e) {
|
||||
uint16_t throttle = get_throttle();
|
||||
if(throttle >= BTN_INCREMENT) {
|
||||
update_throttle(get_throttle() - BTN_INCREMENT);
|
||||
//lv_label_set_text_fmt(rpm_label, "RPM: %d", get_throttle());
|
||||
void rpm_increase_cb(lv_event_t * e) {
|
||||
ESP_LOGI(TAG, "RPM increase callback called.");
|
||||
if(rpm_setting < MAX_RPM_SETTING) {
|
||||
rpm_setting += RPM_STEPSIZE;
|
||||
lv_lock();
|
||||
lv_label_set_text_fmt(rpm_label, "%d", rpm_setting);
|
||||
lv_unlock();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback for pressing the rpm decrease button
|
||||
*/
|
||||
void rpm_decrease_cb(lv_event_t * e) {
|
||||
ESP_LOGI(TAG, "RPM decrease callback called.");
|
||||
if(rpm_setting > MIN_RPM_SETTING) {
|
||||
rpm_setting -= RPM_STEPSIZE;
|
||||
lv_lock();
|
||||
lv_label_set_text_fmt(rpm_label, "%d", rpm_setting);
|
||||
lv_unlock();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback for pressing the coat time increase button
|
||||
*/
|
||||
|
||||
void coat_increase_cb(lv_event_t * e) {
|
||||
ESP_LOGI(TAG, "Coat time increase callback called.");
|
||||
if(coat_time_setting < MAX_COAT_TIME_SETTING) {
|
||||
coat_time_setting += COAT_TIME_STEPSIZE;
|
||||
lv_lock();
|
||||
lv_label_set_text_fmt(coat_time_label, "%d", coat_time_setting);
|
||||
lv_unlock();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback for pressing the coat time decrease button
|
||||
*/
|
||||
void coat_decrease_cb(lv_event_t * e) {
|
||||
ESP_LOGI(TAG, "Coat time decrease callback called.");
|
||||
if(coat_time_setting > MIN_COAT_TIME_SETTING) {
|
||||
coat_time_setting -= COAT_TIME_STEPSIZE;
|
||||
lv_lock();
|
||||
lv_label_set_text_fmt(coat_time_label, "%d", coat_time_setting);
|
||||
lv_unlock();
|
||||
}
|
||||
}
|
||||
|
||||
void run_stop_cb(lv_event_t * e) {
|
||||
ESP_LOGI(TAG, "Run/stop callback called.");
|
||||
if(run_stop_label == NULL) return;
|
||||
lv_label_set_text(run_stop_label, "Stop");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Factory function that creates a "numberstack" widget and returns the label for it
|
||||
*/
|
||||
void build_numberstack(lv_obj_t * parent,
|
||||
lv_obj_t * build_numberstack(lv_obj_t * parent,
|
||||
const char * label_text,
|
||||
lv_obj_t ** label_value,
|
||||
ns_btn_cb_t top_btn_cb,
|
||||
ns_btn_cb_t bottom_btn_cb) {
|
||||
ns_btn_cb_t bottom_btn_cb,
|
||||
uint16_t start_value,
|
||||
uint16_t stepsize) {
|
||||
|
||||
lv_obj_t * container = lv_obj_create(parent);
|
||||
lv_obj_set_size(container , lv_pct(100), lv_pct(100));
|
||||
lv_obj_set_flex_flow(container, LV_FLEX_FLOW_COLUMN);
|
||||
lv_obj_set_flex_align(container,
|
||||
LV_FLEX_ALIGN_START,
|
||||
LV_FLEX_ALIGN_CENTER,
|
||||
LV_FLEX_ALIGN_CENTER);
|
||||
|
||||
lv_obj_set_style_pad_all(container, 10, 0);
|
||||
lv_obj_set_style_pad_gap(container, 8, 0);
|
||||
lv_obj_set_style_pad_all(container, 0, 0);
|
||||
|
||||
/* -------- Top label -------- */
|
||||
lv_obj_t * label_cont = lv_obj_create(container);
|
||||
|
||||
lv_obj_remove_flag(label_cont, LV_OBJ_FLAG_SCROLLABLE);
|
||||
|
||||
lv_obj_set_flex_grow(label_cont, 1);
|
||||
lv_obj_set_width(label_cont, lv_pct(100));
|
||||
|
||||
@@ -63,40 +123,44 @@ void build_numberstack(lv_obj_t * parent,
|
||||
|
||||
lv_obj_set_style_bg_opa(label, LV_OPA_COVER, 0);
|
||||
|
||||
lv_obj_set_style_text_font(label, &lv_font_montserrat_14, 0);
|
||||
lv_obj_set_style_text_font(label, &lv_font_montserrat_12, 0);
|
||||
lv_obj_center(label);
|
||||
|
||||
/* -------- Top button -------- */
|
||||
lv_obj_t * btn_top = lv_button_create(container);
|
||||
lv_obj_t * lbl_top = lv_label_create(btn_top);
|
||||
lv_label_set_text(lbl_top, "+100");
|
||||
lv_obj_add_event_cb(btn_top, top_cb, LV_EVENT_CLICKED, (void *) 1);
|
||||
lv_label_set_text_fmt(lbl_top, "+%d", stepsize);
|
||||
lv_obj_add_event_cb(btn_top, top_btn_cb, LV_EVENT_CLICKED, (void *) 1);
|
||||
lv_obj_set_flex_grow(btn_top, 1);
|
||||
lv_obj_set_width(btn_top, lv_pct(100));
|
||||
lv_obj_center(lbl_top);
|
||||
|
||||
/* -------- Center label -------- */
|
||||
lv_obj_t * label2_cont = lv_obj_create(container);
|
||||
|
||||
lv_obj_remove_flag(label2_cont, LV_OBJ_FLAG_SCROLLABLE);
|
||||
|
||||
lv_obj_set_flex_grow(label2_cont, 1);
|
||||
lv_obj_set_width(label2_cont, lv_pct(100));
|
||||
|
||||
* label_value = lv_label_create(label2_cont);
|
||||
lv_label_set_text(*label_value, "");
|
||||
lv_label_set_text_fmt(*label_value, "%d", start_value);
|
||||
|
||||
lv_obj_set_style_bg_opa(*label_value, LV_OPA_COVER, 0);
|
||||
|
||||
lv_obj_set_style_text_font(*label_value, &lv_font_montserrat_14, 0);
|
||||
lv_obj_set_style_text_font(*label_value, &lv_font_montserrat_12, 0);
|
||||
lv_obj_center(*label_value);
|
||||
|
||||
/* -------- Bottom button -------- */
|
||||
lv_obj_t * btn_bottom = lv_button_create(container);
|
||||
lv_obj_t * lbl_bottom = lv_label_create(btn_bottom);
|
||||
lv_label_set_text(lbl_bottom, "-100");
|
||||
lv_obj_add_event_cb(btn_bottom, bottom_cb, LV_EVENT_CLICKED, (void *) 2);
|
||||
lv_label_set_text_fmt(lbl_bottom, "-%d", stepsize);
|
||||
lv_obj_add_event_cb(btn_bottom, bottom_btn_cb, LV_EVENT_CLICKED, (void *) 2);
|
||||
lv_obj_set_flex_grow(btn_bottom, 1);
|
||||
lv_obj_set_width(btn_bottom, lv_pct(100));
|
||||
lv_obj_center(lbl_bottom);
|
||||
|
||||
return container;
|
||||
}
|
||||
|
||||
void update_rpm_readout(uint16_t rpm) {
|
||||
@@ -106,7 +170,48 @@ void update_rpm_readout(uint16_t rpm) {
|
||||
};
|
||||
|
||||
void build_ui(void) {
|
||||
rpm_label = NULL;
|
||||
build_numberstack(lv_screen_active(), "RPM", &rpm_label, top_cb, bottom_cb);
|
||||
lv_obj_t * container = lv_obj_create(lv_screen_active());
|
||||
lv_obj_set_size(container, lv_pct(100), lv_pct(100));
|
||||
lv_obj_set_flex_flow(container, LV_FLEX_FLOW_COLUMN);
|
||||
lv_obj_set_flex_align(container,
|
||||
LV_FLEX_ALIGN_START,
|
||||
LV_FLEX_ALIGN_CENTER,
|
||||
LV_FLEX_ALIGN_CENTER);
|
||||
|
||||
lv_obj_t * numberstacks_container = lv_obj_create(container);
|
||||
lv_obj_set_size(numberstacks_container , lv_pct(100), lv_pct(70));
|
||||
lv_obj_set_flex_flow(numberstacks_container, LV_FLEX_FLOW_ROW);
|
||||
lv_obj_set_flex_align(numberstacks_container,
|
||||
LV_FLEX_ALIGN_SPACE_EVENLY,
|
||||
LV_FLEX_ALIGN_CENTER,
|
||||
LV_FLEX_ALIGN_CENTER);
|
||||
|
||||
lv_obj_t * rpm_container = build_numberstack(numberstacks_container, "RPM", &rpm_label,
|
||||
rpm_increase_cb, rpm_decrease_cb, rpm_setting, RPM_STEPSIZE);
|
||||
lv_obj_t * coat_time_container = build_numberstack(numberstacks_container, "Coat Time",
|
||||
&coat_time_label, coat_increase_cb, coat_decrease_cb, coat_time_setting, COAT_TIME_STEPSIZE);
|
||||
|
||||
lv_obj_set_size(rpm_container, lv_pct(48), lv_pct(96));
|
||||
lv_obj_set_size(coat_time_container, lv_pct(48), lv_pct(96));
|
||||
|
||||
lv_obj_set_style_pad_all(container, 0, 0);
|
||||
lv_obj_set_style_pad_all(numberstacks_container, 0, 0);
|
||||
lv_obj_set_style_pad_all(rpm_container, 0, 0);
|
||||
lv_obj_set_style_pad_all(coat_time_container, 0, 0);
|
||||
|
||||
/* -------- Run/Stop button -------- */
|
||||
lv_obj_t * btn_run_stop = lv_button_create(container);
|
||||
lv_obj_t * lbl_run_stop = lv_label_create(btn_run_stop);
|
||||
|
||||
run_stop_label = lbl_run_stop;
|
||||
|
||||
lv_label_set_text(lbl_run_stop, "Run");
|
||||
|
||||
lv_obj_center(lbl_run_stop);
|
||||
|
||||
lv_obj_add_event_cb(btn_run_stop, run_stop_cb, LV_EVENT_CLICKED, NULL);
|
||||
|
||||
lv_obj_set_size(btn_run_stop, lv_pct(96), lv_pct(18));
|
||||
lv_obj_set_style_pad_all(btn_run_stop, 0, 0);
|
||||
}
|
||||
|
||||
|
||||
@@ -12,12 +12,22 @@ typedef struct {
|
||||
ns_btn_cb_t bottom_cb;
|
||||
} ns_widget_ctx_t; /** Numberstack widget context type */
|
||||
|
||||
void build_numberstack(lv_obj_t * parent,
|
||||
typedef enum {
|
||||
STOPPED,
|
||||
SPINUP,
|
||||
COATING,
|
||||
SPINDOWN
|
||||
} coat_state;
|
||||
|
||||
lv_obj_t * build_numberstack(lv_obj_t * parent,
|
||||
const char * label_text,
|
||||
lv_obj_t ** label_value,
|
||||
ns_btn_cb_t top_btn_cb,
|
||||
ns_btn_cb_t bottom_btn_cb);
|
||||
ns_btn_cb_t bottom_btn_cb,
|
||||
uint16_t start_value,
|
||||
uint16_t stepsize);
|
||||
|
||||
void update_rpm_readout(uint16_t rpm);
|
||||
|
||||
void build_ui(void);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user