Skip to content

Commit

Permalink
working on LCD implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
derlucas committed Oct 16, 2017
1 parent bb82de1 commit 64880df
Show file tree
Hide file tree
Showing 5 changed files with 339 additions and 8 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ CSRC = $(STARTUPSRC) \
flash_helper.c \
mc_interface.c \
mcpwm_foc.c \
s_lcd3.c \
$(HWSRC) \
$(APPSRC) \
$(NRFSRC)
Expand Down
2 changes: 1 addition & 1 deletion appconf/appconf_default.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@
#define APPCONF_EV_RAMP_TIME_NEG 0.0
#endif
#ifndef APPCONF_EV_UPDATE_RATE_HZ
#define APPCONF_EV_UPDATE_RATE_HZ 500
#define APPCONF_EV_UPDATE_RATE_HZ 100
#endif

// UART app
Expand Down
42 changes: 35 additions & 7 deletions applications/app_ev.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "timeout.h"
#include "utils.h"
#include "hw.h"
#include "s_lcd3.h"
#include <math.h>
#include <stdio.h>

Expand All @@ -50,32 +51,42 @@ static volatile float read_voltage2 = 0.0;
static volatile bool stop_now = true;
static volatile bool is_running = false;
static volatile bool throttle_released_after_brake = true;
static volatile int runmode = 0;
static volatile int runmode = 1;


float get_mode_value(void);





void app_ev_configure(ev_config *conf) {
config = *conf;
ms_without_power = 0.0;
}

void app_ev_start() {
stop_now = false;

chThdCreateStatic(ev_thread_wa, sizeof(ev_thread_wa), NORMALPRIO, ev_thread, NULL);

s_lcd3_start();
}

void app_ev_stop(void) {
stop_now = true;

s_lcd3_stop();

if(is_running) {
servodec_stop();
}

while (is_running) {
chThdSleepMilliseconds(1);
}


}

float app_ev_get_decoded_level1(void) {
Expand Down Expand Up @@ -105,16 +116,19 @@ float get_adc_voltage(uint8_t adc_index) {

float get_mode_value() {
switch (runmode) {
case 0: return config.mode_1_current;
case 1: return config.mode_2_current;
case 2: return config.mode_3_current;
case 3: return config.mode_4_current;
case 4: return config.mode_5_current;
case 5: return config.mode_6_current;
case 0: return 0;
case 1: return config.mode_1_current;
case 2: return config.mode_2_current;
case 3: return config.mode_3_current;
case 4: return config.mode_4_current;
case 5: return config.mode_5_current;
case 6: return config.mode_6_current;
default: return config.mode_1_current;
}
}



static THD_FUNCTION(ev_thread, arg) {
(void)arg;

Expand Down Expand Up @@ -214,12 +228,26 @@ static THD_FUNCTION(ev_thread, arg) {

if(is_pedaling) {
pwr = get_mode_value();

}

// Apply PAS ramping (TODO: make this configurable)
static systime_t last_time_pas = 0;
static float pwr_ramp_pas = 0.0;
const float ramp_time_pas = fabsf(pwr) > fabsf(pwr_ramp_pas) ? 5 : 5;

if (ramp_time_pas > 0.01) {
const float ramp_step_pas = (float)ST2MS(chVTTimeElapsedSinceX(last_time_pas)) / (ramp_time_pas * 1000.0);
utils_step_towards(&pwr_ramp_pas, pwr, ramp_step_pas);
last_time_pas = chVTGetSystemTimeX();
pwr = pwr_ramp_pas;
}
}

if(config.use_throttle) {
if(config.use_pas) {
if(is_pedaling) {
// throttle allows giving more power than pas mode would normally be
pwr = utils_max_abs(pwr, throttle);
}
} else {
Expand Down
205 changes: 205 additions & 0 deletions s_lcd3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
/**
Copyright 2017 Lucas Pleß [email protected]
This file is part of the VESC firmware.
The VESC firmware is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The VESC firmware is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
**/

#include "app.h"
#include "ch.h"
#include "hal.h"
#include "hw.h"
#include "s_lcd3.h"
#include "mc_interface.h"

#include <string.h>

// settings
#define BAUDRATE 9600
#define SERIAL_RX_BUFFER_SIZE 48

// Threads
static THD_FUNCTION(packet_process_thread, arg);
static THD_WORKING_AREA(packet_process_thread_wa, 4096);
static thread_t *process_tp = 0;

// Variables
static uint8_t serial_rx_buffer[SERIAL_RX_BUFFER_SIZE];
static int serial_rx_read_pos = 0;
static int serial_rx_write_pos = 0;
static volatile bool is_running = false;

static volatile lcd_rx_data lcd_data_rx;
static volatile lcd_tx_data lcd_data_tx;



/*
* This callback is invoked when a transmission buffer has been completely
* read by the driver.
*/
static void txend1(UARTDriver *uartp) {
(void)uartp;
}

/*
* This callback is invoked when a transmission has physically completed.
*/
static void txend2(UARTDriver *uartp) {
(void)uartp;
}

/*
* This callback is invoked on a receive error, the errors mask is passed
* as parameter.
*/
static void rxerr(UARTDriver *uartp, uartflags_t e) {
(void)uartp;
(void)e;
}

/*
* This callback is invoked when a character is received but the application
* was not ready to receive it, the character is passed as parameter.
*/
static void rxchar(UARTDriver *uartp, uint16_t c) {
(void)uartp;
serial_rx_buffer[serial_rx_write_pos++] = c;

if (serial_rx_write_pos == SERIAL_RX_BUFFER_SIZE) {
serial_rx_write_pos = 0;
}

chSysLockFromISR();
chEvtSignalI(process_tp, (eventmask_t) 1);
chSysUnlockFromISR();
}

/*
* This callback is invoked when a receive buffer has been completely written.
*/
static void rxend(UARTDriver *uartp) {
(void)uartp;
}

/*
* UART driver configuration structure.
*/
static UARTConfig uart_cfg = {
txend1,
txend2,
rxend,
rxchar,
rxerr,
BAUDRATE,
0,
USART_CR2_LINEN,
0
};


void s_lcd3_start() {

const volatile mc_configuration *mcconf = mc_interface_get_configuration();

lcd_data_tx.power = GET_INPUT_VOLTAGE() * mc_interface_get_tot_current_in_filtered();
lcd_data_tx.battery_lvl = BORDER_FLASHING;
lcd_data_tx.error_info = 0;
lcd_data_tx.motor_temperature = 23;
lcd_data_tx.wheel_rotation_period = 0;
lcd_data_tx.show_animated_circle = false;
lcd_data_tx.show_assist = false;
lcd_data_tx.show_cruise_control = false;


serial_rx_read_pos = 0;
serial_rx_write_pos = 0;

if (!is_running) {
chThdCreateStatic(packet_process_thread_wa, sizeof(packet_process_thread_wa),
NORMALPRIO, packet_process_thread, NULL);
is_running = true;
}

uartStart(&HW_UART_DEV, &uart_cfg);
palSetPadMode(HW_UART_TX_PORT, HW_UART_TX_PIN, PAL_MODE_ALTERNATE(HW_UART_GPIO_AF) |
PAL_STM32_OSPEED_HIGHEST | PAL_STM32_PUDR_PULLUP);
palSetPadMode(HW_UART_RX_PORT, HW_UART_RX_PIN, PAL_MODE_ALTERNATE(HW_UART_GPIO_AF) |
PAL_STM32_OSPEED_HIGHEST | PAL_STM32_PUDR_PULLUP);


}

void s_lcd3_stop() {

uartStop(&HW_UART_DEV);
palSetPadMode(HW_UART_TX_PORT, HW_UART_TX_PIN, PAL_MODE_INPUT_PULLUP);
palSetPadMode(HW_UART_RX_PORT, HW_UART_RX_PIN, PAL_MODE_INPUT_PULLUP);

}


const volatile lcd_rx_data* s_lcd3_get_data() {
return &lcd_data_rx;
}

void lcd_set_data(uint16_t wheel_rotation_period, uint8_t error_display,
bool anim_throttle, bool cruise, bool assist) {

lcd_data_tx.wheel_rotation_period = wheel_rotation_period;
lcd_data_tx.error_info = error_display;
lcd_data_tx.show_animated_circle = anim_throttle;
lcd_data_tx.show_assist = assist;
lcd_data_tx.show_cruise_control = cruise;
}



static THD_FUNCTION(packet_process_thread, arg) {
(void)arg;

chRegSetThreadName("s-lcd3 uart process");

process_tp = chThdGetSelfX();

for(;;) {
chEvtWaitAny((eventmask_t) 1);

while (serial_rx_read_pos != serial_rx_write_pos) {

uint8_t byte = serial_rx_buffer[serial_rx_read_pos++];

if(byte == 0x0e) {
//probably last byte. check previous byte
int8_t pos = serial_rx_read_pos-1;
if(pos == -1) {
pos = SERIAL_RX_BUFFER_SIZE-1;
}

if(serial_rx_buffer[pos] == 0x32) {


}

}



if (serial_rx_read_pos == SERIAL_RX_BUFFER_SIZE) {
serial_rx_read_pos = 0;
}
}
}
}
Loading

0 comments on commit 64880df

Please sign in to comment.