diff --git a/shared/defines.h b/shared/defines.h index 515a35fd..1fd50a73 100644 --- a/shared/defines.h +++ b/shared/defines.h @@ -36,6 +36,7 @@ extern "C" { #define RAD(a) ((a) * 180.0 / M_PI) #define SIGN(a) (((a) < 0.0) ? (-1.0) : (((a) > 0.0) ? (1.0) : (0.0))) #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x))) +#define SCALE(value, high, max) MIN(MAX(((max) - (value)) / ((max) - (high)), 0.0), 1.0) #define MIN(a, b) (((a) < (b)) ? (a) : (b)) #define MAX(a, b) (((a) > (b)) ? (a) : (b)) @@ -47,7 +48,7 @@ extern "C" { #define STRINGIFY_(x) #x #define STRINGIFY(x) STRINGIFY_(x) -//TODO: change type to typeof() +//TODO: change type to typeof(), change initial value to something more useful #define RISING_EDGE(sig)\ ({static float __old_val__ = 0.0; uint8_t ret = (sig) > __old_val__; __old_val__ = (sig); ret;}) diff --git a/shared/version.h b/shared/version.h index 1e5fffbe..362a150e 100644 --- a/shared/version.h +++ b/shared/version.h @@ -2,7 +2,7 @@ #include -struct version_info { +struct version_info {//TODO: typedef // set at link-time by add-version-info.py // uint32_t image_crc; diff --git a/src/comps/adc.comp b/src/comps/adc.comp index 691b239d..738cd57c 100644 --- a/src/comps/adc.comp +++ b/src/comps/adc.comp @@ -40,7 +40,7 @@ RT( float s; float c; - uint32_t *ADC_DMA_Buffer; + volatile uint32_t *ADC_DMA_Buffer; // if(DMA_GetCurrentMemoryTarget(DMA2_Stream0) == 0){ // ADC_DMA_Buffer = ADC_DMA_Buffer1; diff --git a/src/comps/curpid.comp b/src/comps/curpid.comp index 2445005b..91e6f214 100644 --- a/src/comps/curpid.comp +++ b/src/comps/curpid.comp @@ -52,6 +52,7 @@ RT( float kiq = rq * PIN(ki) / lq; float kind = PIN(kind); + //TODO curpid: sqrt(di^2+qi^2) auf max_ac_cur clampen float idc = LIMIT(PIN(id_cmd), PIN(max_cur) * PIN(scale)); float iqc = LIMIT(PIN(iq_cmd), PIN(max_cur) * PIN(scale)); diff --git a/src/comps/fault2.comp b/src/comps/fault2.comp index 22ff4711..693b6771 100644 --- a/src/comps/fault2.comp +++ b/src/comps/fault2.comp @@ -1,8 +1,7 @@ HAL_COMP(fault); -#define SCALE(value, high, max) MIN(MAX(((max) - (value)) / ((max) - (high)), 0.0), 1.0) - HAL_PIN(en) = 0.0; +HAL_PIN(reset) = 0.0; HAL_PIN(state) = 0.0; HAL_PIN(fault) = 0.0; HAL_PIN(en_out) = 0.0; @@ -60,11 +59,13 @@ HAL_PIN(mot_fan) = 0.0; HAL_PIN(phase_with_brake) = 1.0; HAL_PIN(phase_on_start) = 1.0; +HAL_PIN(print) = 0.0; + HAL_PIN(brake_release) = 0.0; -MEM(enum state_t state) = DISABLED; +MEM(state_t state) = DISABLED; -MEM(enum fault_t fault) = NO_ERROR; +MEM(fault_t fault) = NO_ERROR; MEM(uint32_t phased) = 0; @@ -106,6 +107,10 @@ RT( case SOFT_FAULT: if(PIN(en) <= 0.0){ state = DISABLED; + }else if(FALLING_EDGE(PIN(reset)) & (PIN(cmd_ready) > 0.0) & (PIN(fb0_ready) > 0.0) & (PIN(fb1_ready) > 0.0) & (PIN(hv_ready) > 0.0)){ + //TODO: phasing + state = ENABLED; + PIN(start_offset) = minus(PIN(fb), PIN(cmd)); } break; @@ -241,7 +246,74 @@ RT( PIN(mot_brake) = 1.0; } ); +NRT( +if(EDGE(state) || PIN(print) > 0.0){ + PIN(print) = 0.0; + switch((state_t)state){ + case DISABLED: + printf("INFO: Disabled \n"); + break; + case ENABLED: + printf("INFO: Enabled \n"); + break; + + case PHASING: + printf("INFO: Phasing \n"); + break; + + case SOFT_FAULT: + printf("ERROR: Soft fault: "); + switch((fault_t)fault){ + case NO_ERROR: + printf("no error\n"); + break; + + case CMD_ERROR: + printf("CMD error\n"); + break; + + case FB0_ERROR: + printf("FB0 error\n"); + break; + + case FB1_ERROR: + printf("FB1 error\n"); + break; + + case POS_ERROR: + printf("position error\n"); + break; + + case SAT_ERROR: + printf("saturation error\n"); + break; + + case HV_ERROR: + printf("HV error\n"); + break; + + case HV_TEMP_ERROR: + printf("HV overtemperture\n"); + break; + + case HV_VOLT_ERROR: + printf("HV volt error\n"); + break; + + case MOT_TEMP_ERROR: + printf("Motor overtemperture\n"); + break; + } + break; + + case HARD_FAULT: + printf("ERROR: Hard fault: \n"); + break; + + default: + break; + } +} +); ENDCOMP; - -//TODO curpid: sqrt(di^2+qi^2) auf max_ac_cur clampen diff --git a/src/comps/io.comp b/src/comps/io.comp index 5da9d278..06d21eb1 100644 --- a/src/comps/io.comp +++ b/src/comps/io.comp @@ -65,7 +65,7 @@ NRT( uint32_t red = 0; uint32_t green = 0; - switch((int)PIN(state)){ + switch((state_t)PIN(state)){ case DISABLED: green = BLINK(1); red = 0; diff --git a/src/comps/term.comp b/src/comps/term.comp index 06565df5..8bcf2770 100644 --- a/src/comps/term.comp +++ b/src/comps/term.comp @@ -1,60 +1,23 @@ HAL_COMP(term); -HAL_PIN(wave0) = 0.0; -HAL_PIN(gain0) = 10.0; -HAL_PIN(offset0) = 0.0; +#define TERM_NUM_WAVES 8 -HAL_PIN(wave1) = 0.0; -HAL_PIN(gain1) = 10.0; -HAL_PIN(offset1) = 0.0; - -HAL_PIN(wave2) = 0.0; -HAL_PIN(gain2) = 10.0; -HAL_PIN(offset2) = 0.0; - -HAL_PIN(wave3) = 0.0; -HAL_PIN(gain3) = 10.0; -HAL_PIN(offset3) = 0.0; - -HAL_PIN(wave4) = 0.0; -HAL_PIN(gain4) = 10.0; -HAL_PIN(offset4) = 0.0; - -HAL_PIN(wave5) = 0.0; -HAL_PIN(gain5) = 10.0; -HAL_PIN(offset5) = 0.0; - -HAL_PIN(wave6) = 0.0; -HAL_PIN(gain6) = 10.0; -HAL_PIN(offset6) = 0.0; - -HAL_PIN(wave7) = 0.0; -HAL_PIN(gain7) = 10.0; -HAL_PIN(offset7) = 0.0; +HAL_PIN_ARRAY(wave, TERM_NUM_WAVES); +HAL_PIN_ARRAY(gain, TERM_NUM_WAVES); +HAL_PIN_ARRAY(offset, TERM_NUM_WAVES); HAL_PIN(send_step) = 10.0; HAL_PIN(buf_use) = 0.0; -HAL_PIN(state) = 0.0; -HAL_PIN(fault) = 0.0; HAL_PIN(con) = 0.0; HAL_PIN(jogvel) = 10.0; HAL_PIN(jogpos) = 0.0; HAL_PIN(jogging) = 0.0; -HAL_PIN(tim2_rv) = 420.0; - #define TERM_BUF_SIZE 24 -MEM(float w0[TERM_BUF_SIZE]); -MEM(float w1[TERM_BUF_SIZE]); -MEM(float w2[TERM_BUF_SIZE]); -MEM(float w3[TERM_BUF_SIZE]); -MEM(float w4[TERM_BUF_SIZE]); -MEM(float w5[TERM_BUF_SIZE]); -MEM(float w6[TERM_BUF_SIZE]); -MEM(float w7[TERM_BUF_SIZE]); +MEM(float wave_buf[TERM_BUF_SIZE][TERM_NUM_WAVES]); MEM(unsigned int read_pos) = 0; MEM(unsigned int write_pos) = 0; @@ -77,16 +40,11 @@ RT( }else{ PIN(jogging) = 0.0; } - //TIM2->ARR = (int)CLAMP(PIN(tim2_rv), 1, 1500); + if(send_counter++ >= PIN(send_step) - 1){ - w0[write_pos] = PIN(wave0); - w1[write_pos] = PIN(wave1); - w2[write_pos] = PIN(wave2); - w3[write_pos] = PIN(wave3); - w4[write_pos] = PIN(wave4); - w5[write_pos] = PIN(wave5); - w6[write_pos] = PIN(wave6); - w7[write_pos] = PIN(wave7); + for(int i = 0; i < TERM_NUM_WAVES; i++){ + wave_buf[write_pos][i] = PIN_ARRAY(wave,i); + } write_pos++; write_pos %= TERM_BUF_SIZE; @@ -96,7 +54,7 @@ RT( NRT( - void print_version_info(struct version_info* v){ + void print_version_info(const volatile struct version_info* v){ printf( "%s v%i.%i.%i %s\n", v->product_name, @@ -296,129 +254,22 @@ NRT( } } - void fault_sender(){ - if(EDGE(PIN(state))){ - switch((int)PIN(state)){ - case DISABLED: - printf("INFO: Disabled \n"); - break; - - case ENABLED: - printf("INFO: Enabled \n"); - break; - - case PHASING: - printf("ERROR: Phasing \n"); - break; - - case SOFT_FAULT: - printf("ERROR: Soft fault: "); - switch((int)PIN(fault)){ - case NO_ERROR: - printf("no error\n"); - break; - - case CMD_ERROR: - printf("CMD error\n"); - break; - - case FB0_ERROR: - printf("FB0 error\n"); - break; - - case FB1_ERROR: - printf("FB1 error\n"); - break; - - case POS_ERROR: - printf("position error\n"); - break; - - case SAT_ERROR: - printf("saturation error\n"); - break; - - case HV_ERROR: - printf("HV error\n"); - break; - - case HV_TEMP_ERROR: - printf("HV overtemperture\n"); - break; - - case HV_VOLT_ERROR: - printf("HV volt error\n"); - break; - - case MOT_TEMP_ERROR: - printf("Motor overtemperture\n"); - break; - } - break; - - case HARD_FAULT: - printf("ERROR: Hard fault: \n"); - break; - - default: - break; - } - } - } - - - int e = 0; + int tmp = 0; unsigned char buf[8 + 2]; buf[0] = 255; buf[8 + 1] = 0; - float o0 = PIN(offset0); - float o1 = PIN(offset1); - float o2 = PIN(offset2); - float o3 = PIN(offset3); - float o4 = PIN(offset4); - float o5 = PIN(offset5); - float o6 = PIN(offset6); - float o7 = PIN(offset7); - float g0 = PIN(gain0); - float g1 = PIN(gain1); - float g2 = PIN(gain2); - float g3 = PIN(gain3); - float g4 = PIN(gain4); - float g5 = PIN(gain5); - float g6 = PIN(gain6); - float g7 = PIN(gain7); - - unsigned int wp = write_pos; unsigned int bc = 0; while(read_pos != wp){ bc++; - e = (w0[read_pos] + o0) * g0 + 128; - buf[1] = CLAMP(e,1,254); - - e = (w1[read_pos] + o1) * g1 + 128; - buf[2] = CLAMP(e,1,254); - - e = (w2[read_pos] + o2) * g2 + 128; - buf[3] = CLAMP(e,1,254); - - e = (w3[read_pos] + o3) * g3 + 128; - buf[4] = CLAMP(e,1,254); - - e = (w4[read_pos] + o4) * g4 + 128; - buf[5] = CLAMP(e,1,254); - - e = (w5[read_pos] + o5) * g5 + 128; - buf[6] = CLAMP(e,1,254); - - e = (w6[read_pos] + o6) * g6 + 128; - buf[7] = CLAMP(e,1,254); - - e = (w7[read_pos] + o7) * g7 + 128; - buf[8] = CLAMP(e,1,254); + + for(int i = 0; i < TERM_NUM_WAVES; i++){ + tmp = (wave_buf[read_pos][i] + PIN_ARRAY(offset,i)) * PIN_ARRAY(gain,i) + 128; + buf[i+1] = CLAMP(tmp,1,254); + } read_pos++; read_pos %= TERM_BUF_SIZE; @@ -439,7 +290,6 @@ NRT( char source[APP_TX_BUF_SIZE]; char sink[APP_TX_BUF_SIZE]; - fault_sender(); if(EDGE(hal.hal_state)){ hal_term_print_state(); } @@ -447,7 +297,7 @@ NRT( int i = -1; char rx_buf[APP_TX_BUF_SIZE]; if (USB_VCP_get_string(rx_buf)) { - i = sscanf_(rx_buf, "%N = %N", sink, source); + i = sscanf_(rx_buf, "%N = %N", sink, source);//TODO: change to lib sscanf } if(i == 2){ // read hal pin if(hal_is_pin(sink)){ diff --git a/src/defines_res.h b/src/defines_res.h index 1fcf6e71..e6f90f5f 100644 --- a/src/defines_res.h +++ b/src/defines_res.h @@ -87,42 +87,6 @@ #define FAN_PORT GPIOC #define FAN_IO_RCC RCC_AHB1Periph_GPIOC -//states - -// state -10 = reset state, -6 = fb error, -5 = sat error, -4 = over current, -3 = pos_error, -2 = over temperature, -1 = over voltage, 0 = disabled, 1 = enabled, 2 = phasing -// #define STATE_RESET -10 -// #define STATE_HV_ERROR -7 -// #define STATE_FB_ERROR -6 -// #define STATE_SAT_ERROR -5 -// #define STATE_OVR_CURR -4 -// #define STATE_POS_ERROR -3 -// #define STATE_OVR_TEMP -2 -// #define STATE_OVR_VOLT -1 -// #define STATE_DISABLED 0 -// #define STATE_ENABLED 1 -// #define STATE_PHASING 2 - -enum state_t{ - DISABLED = 0, - ENABLED, - PHASING, - SOFT_FAULT, - HARD_FAULT -}; - -enum fault_t{ - NO_ERROR = 0, - CMD_ERROR, - FB0_ERROR, - FB1_ERROR, - POS_ERROR, - SAT_ERROR, - HV_ERROR, - HV_TEMP_ERROR, - HV_VOLT_ERROR, - MOT_TEMP_ERROR, -}; - #ifdef TROLLER #define UART_DRV USART1 @@ -191,59 +155,59 @@ enum fault_t{ #endif -// i2c -#define I2C I2C1 -#define I2C_RCC RCC_AHB1Periph_I2C1 - -//// gpio -#define I2C_SDA_PIN GPIO_Pin_9 -#define I2C_SDA_PORT GPIOB -#define I2C_SDA_IO_RCC RCC_AHB1Periph_GPIOB -#define I2C_SDA_PIN_SOURCE GPIO_PinSource9 -#define I2C_SDA_AF_SOURCE GPIO_AF_I2C1 - -#define I2C_SCK_PIN GPIO_Pin_8 -#define I2C_SCK_PORT GPIOB -#define I2C_SCK_IO_RCC RCC_AHB1Periph_GPIOB -#define I2C_SCK_PIN_SOURCE GPIO_PinSource8 -#define I2C_SCK_AF_SOURCE GPIO_AF_I2C1 - -// spi -#define SPI SPI1 -#define SPI_RCC RCC_AHB1Periph_SPI1 - -//// gpio -#define SPI_MOSI_PIN GPIO_Pin_5 -#define SPI_MOSI_PORT GPIOB -#define SPI_MOSI_IO_RCC RCC_AHB1Periph_GPIOB -#define SPI_MOSI_PIN_SOURCE GPIO_PinSource5 -#define SPI_MOSI_AF_SOURCE GPIO_AF_SPI1 - -#define SPI_MISO_PIN GPIO_Pin_4 -#define SPI_MISO_PORT GPIOB -#define SPI_MISO_IO_RCC RCC_AHB1Periph_GPIOB -#define SPI_MISO_PIN_SOURCE GPIO_PinSource4 -#define SPI_MISO_AF_SOURCE GPIO_AF_SPI1 - -#define SPI_SCK_PIN GPIO_Pin_3 -#define SPI_SCK_PORT GPIOB -#define SPI_SCK_IO_RCC RCC_AHB1Periph_GPIOB -#define SPI_SCK_PIN_SOURCE GPIO_PinSource3 -#define SPI_SCK_AF_SOURCE GPIO_AF_SPI1 - -// can -#define CAN CAN1 -#define CAN_RCC RCC_AHB1Periph_CAN1 - -//// gpio -#define CAN_RX_PIN GPIO_Pin_8 -#define CAN_RX_PORT GPIOB -#define CAN_RX_IO_RCC RCC_AHB1Periph_GPIOB -#define CAN_RX_PIN_SOURCE GPIO_PinSource8 -#define CAN_RX_AF_SOURCE GPIO_AF_CAN1 - -#define CAN_TX_PIN GPIO_Pin_9 -#define CAN_TX_PORT GPIOB -#define CAN_TX_IO_RCC RCC_AHB1Periph_GPIOB -#define CAN_TX_PIN_SOURCE GPIO_PinSource9 -#define CAN_TX_AF_SOURCE GPIO_AF_CAN1 +// // i2c +// #define I2C I2C1 +// #define I2C_RCC RCC_AHB1Periph_I2C1 +// +// //// gpio +// #define I2C_SDA_PIN GPIO_Pin_9 +// #define I2C_SDA_PORT GPIOB +// #define I2C_SDA_IO_RCC RCC_AHB1Periph_GPIOB +// #define I2C_SDA_PIN_SOURCE GPIO_PinSource9 +// #define I2C_SDA_AF_SOURCE GPIO_AF_I2C1 +// +// #define I2C_SCK_PIN GPIO_Pin_8 +// #define I2C_SCK_PORT GPIOB +// #define I2C_SCK_IO_RCC RCC_AHB1Periph_GPIOB +// #define I2C_SCK_PIN_SOURCE GPIO_PinSource8 +// #define I2C_SCK_AF_SOURCE GPIO_AF_I2C1 +// +// // spi +// #define SPI SPI1 +// #define SPI_RCC RCC_AHB1Periph_SPI1 +// +// //// gpio +// #define SPI_MOSI_PIN GPIO_Pin_5 +// #define SPI_MOSI_PORT GPIOB +// #define SPI_MOSI_IO_RCC RCC_AHB1Periph_GPIOB +// #define SPI_MOSI_PIN_SOURCE GPIO_PinSource5 +// #define SPI_MOSI_AF_SOURCE GPIO_AF_SPI1 +// +// #define SPI_MISO_PIN GPIO_Pin_4 +// #define SPI_MISO_PORT GPIOB +// #define SPI_MISO_IO_RCC RCC_AHB1Periph_GPIOB +// #define SPI_MISO_PIN_SOURCE GPIO_PinSource4 +// #define SPI_MISO_AF_SOURCE GPIO_AF_SPI1 +// +// #define SPI_SCK_PIN GPIO_Pin_3 +// #define SPI_SCK_PORT GPIOB +// #define SPI_SCK_IO_RCC RCC_AHB1Periph_GPIOB +// #define SPI_SCK_PIN_SOURCE GPIO_PinSource3 +// #define SPI_SCK_AF_SOURCE GPIO_AF_SPI1 +// +// // can +// #define CAN CAN1 +// #define CAN_RCC RCC_AHB1Periph_CAN1 +// +// //// gpio +// #define CAN_RX_PIN GPIO_Pin_8 +// #define CAN_RX_PORT GPIOB +// #define CAN_RX_IO_RCC RCC_AHB1Periph_GPIOB +// #define CAN_RX_PIN_SOURCE GPIO_PinSource8 +// #define CAN_RX_AF_SOURCE GPIO_AF_CAN1 +// +// #define CAN_TX_PIN GPIO_Pin_9 +// #define CAN_TX_PORT GPIOB +// #define CAN_TX_IO_RCC RCC_AHB1Periph_GPIOB +// #define CAN_TX_PIN_SOURCE GPIO_PinSource9 +// #define CAN_TX_AF_SOURCE GPIO_AF_CAN1 diff --git a/src/hal.c b/src/hal.c index 0faf2b99..aa54d826 100644 --- a/src/hal.c +++ b/src/hal.c @@ -25,7 +25,8 @@ //global hal struct hal_struct_t hal; -char* itoa(int i){ +//TODO: only works for single positive digit +char* hal_itoa(int i){ hal.tmp[0] = (i % 10) + '0'; hal.tmp[1] = '\0'; return(hal.tmp); @@ -210,9 +211,10 @@ void hal_stop_frt(){ hal.frt_state = FRT_STOP; } +//TODO: calculate and reduce name length... void hal_init_pin(hal_name_t name, hal_pin_t* pin, float value){ strncpy(pin->name, hal.comp_types[hal.comp_type], HAL_NAME_LENGTH); - strncat(pin->name, itoa(hal.comp_types_counter[hal.comp_type]), HAL_NAME_LENGTH); + strncat(pin->name, hal_itoa(hal.comp_types_counter[hal.comp_type]), HAL_NAME_LENGTH); strncat(pin->name, ".", HAL_NAME_LENGTH); strncat(pin->name, name, HAL_NAME_LENGTH); pin->value = value; diff --git a/src/hal.h b/src/hal.h index c0b32d6f..f17d43c0 100644 --- a/src/hal.h +++ b/src/hal.h @@ -167,6 +167,8 @@ extern uint32_t hal_get_systick_value(); extern uint32_t hal_get_systick_reload(); extern uint32_t hal_get_systick_freq(); +char* hal_itoa(int i); + #define HAL_COMP(type) \ { \ static hal_comp_t self; \ @@ -190,6 +192,16 @@ extern uint32_t hal_get_systick_freq(); hal_init_pin(#name, &name##_hal_pin, 0.0); \ (name##_hal_pin.value) +//TODO: clamp loads of stuff, like count... check length... +#define HAL_PIN_ARRAY(name,count) \ + static hal_pin_t name##_hal_pin[count]; \ + for(uint32_t i = 0; i < count; i++){ \ + char pinname[HAL_NAME_LENGTH]; \ + strncpy(pinname,#name,HAL_NAME_LENGTH); \ + strncat(pinname,hal_itoa(i),HAL_NAME_LENGTH); \ + hal_init_pin(pinname, &name##_hal_pin[i], 0.0); \ + } + #define GLOBAL_HAL_PIN(name) \ volatile hal_pin_t name##_hal_pin; @@ -198,6 +210,9 @@ extern uint32_t hal_get_systick_freq(); #define PIN(name) \ (name##_hal_pin.source->source->value) +#define PIN_ARRAY(name,index) \ + (name##_hal_pin[index].source->source->value) + #define INIT(func) \ self.nrt_init = ({ void function(){func} function;}); @@ -273,6 +288,7 @@ if(ht_time_count < (time)){ \ } \ jump_label_pointer = jump_label_pointer_old; + //TODO: rename HAL_ENDCOMP #define ENDCOMP \ self.hal_pin_count = hal.hal_pin_count - self.hal_pin_start_index; \ hal_add_comp(&self); \ diff --git a/src/hal_conf.c b/src/hal_conf.c index 2f7d0368..55d9e112 100644 --- a/src/hal_conf.c +++ b/src/hal_conf.c @@ -2,6 +2,7 @@ #include "eeprom.h" #include "crc16.h" #include "link.h" +#include "main.h" // for Wait #include uint16_t hal_conf_init(){ @@ -14,7 +15,7 @@ uint16_t hal_conf_init(){ void hal_conf_save(){ if(hal.rt_state != RT_STOP || hal.frt_state != FRT_STOP){ printf("cannot save while hal is running(run 'stop' command)\n"); - return(-1); + return; } typedef union{ float f; @@ -45,7 +46,7 @@ void hal_conf_save(){ if(elo != FLASH_COMPLETE || ehi != FLASH_COMPLETE){ FLASH_Lock(); printf("error writing to address %i: error: %i,%i\n",address,elo,ehi); - return -1; + return; } address+=2; } @@ -56,7 +57,7 @@ void hal_conf_save(){ if(elo != FLASH_COMPLETE){ printf("error writing crc to address %i: error: %i\n",address,elo); FLASH_Lock(); - return -1; + return; } FLASH_Lock(); printf("flash save OK\n"); diff --git a/src/hal_term.c b/src/hal_term.c index f3d8d8b6..47510cc0 100644 --- a/src/hal_term.c +++ b/src/hal_term.c @@ -1,5 +1,6 @@ #include #include "hal_term.h" +#include "main.h" // for Wait void hal_term_print_pin(hal_pin_t* pin){ if(pin == pin->source){//if pin is not linked diff --git a/src/link.c b/src/link.c index 80cdeb0c..e37912ee 100644 --- a/src/link.c +++ b/src/link.c @@ -123,12 +123,12 @@ void link_pid(){ // fault hal_link_pins("conf0.max_dc_cur", "fault0.max_dc_cur"); - hal_link_pins("conf0.max_ac_cur", "fault0.max_ac_cur"); - hal_link_pins("conf0.max_dc_volt", "fault0.max_dc_volt"); + // hal_link_pins("conf0.max_ac_cur", "fault0.max_ac_cur"); + hal_link_pins("conf0.max_dc_volt", "fault0.max_hv_volt"); hal_link_pins("conf0.max_hv_temp", "fault0.max_hv_temp"); hal_link_pins("conf0.max_motor_temp", "fault0.max_mot_temp"); hal_link_pins("conf0.max_pos_error", "fault0.max_pos_error"); - hal_link_pins("conf0.high_dc_volt", "fault0.high_dc_volt"); + hal_link_pins("conf0.high_dc_volt", "fault0.high_hv_volt"); hal_link_pins("conf0.high_hv_temp", "fault0.high_hv_temp"); hal_link_pins("conf0.high_motor_temp", "fault0.high_mot_temp"); hal_link_pins("conf0.low_dc_volt", "fault0.min_hv_volt"); @@ -169,8 +169,6 @@ void link_pid(){ hal_link_pins("fault0.fault", "io0.fault"); hal_link_pins("fault0.state", "io0.state"); - hal_link_pins("fault0.state", "term0.state"); //sent state to terminal - hal_link_pins("fault0.fault", "term0.fault"); //sent fault to terminal // cauto hal_link_pins("conf0.fb_offset", "cauto0.mag_offset"); @@ -271,8 +269,8 @@ int update_fb(){ hal_set_pin("enc_fb0.frt_prio", -1.0); hal_set_pin("res0.rt_prio", -1.0); hal_set_pin("encm0.rt_prio", -1.0); - hal_set_pin("encs0.rt_prio", -1.0); - hal_set_pin("yaskawa0.rt_prio", -1.0); + // hal_set_pin("encs0.rt_prio", -1.0); + // hal_set_pin("yaskawa0.rt_prio", -1.0); switch((protocol_t)hal_get_pin("conf0.fb_type")){ case ENC: hal_link_pins("enc_fb0.pos", "rev1.in"); @@ -341,8 +339,8 @@ int update_fb(){ int update_cmd(){ hal_set_pin("enc_cmd0.rt_prio", -1.0); hal_set_pin("enc_cmd0.frt_prio", -1.0); - hal_set_pin("step_cmd0.rt_prio", -1.0); - hal_set_pin("step_cmd0.frt_prio", -1.0); + // hal_set_pin("step_cmd0.rt_prio", -1.0); + // hal_set_pin("step_cmd0.frt_prio", -1.0); hal_set_pin("sserial0.rt_prio", -1.0); hal_set_pin("sserial0.frt_prio", -1.0); hal_set_pin("en0.rt_prio", -1.0); diff --git a/src/main.c b/src/main.c index 04edd83b..0f824748 100644 --- a/src/main.c +++ b/src/main.c @@ -33,6 +33,7 @@ #include #include #include "usb_cdc.h" +#include "main.h" GLOBAL_HAL_PIN(rt_time); GLOBAL_HAL_PIN(frt_time); @@ -240,8 +241,6 @@ int main(void) HAL_PIN(fb_error) = 0.0; HAL_PIN(cmd_d) = 0.0; HAL_PIN(fb_d) = 0.0; - HAL_PIN(core_temp0) = 0.0; - HAL_PIN(core_temp1) = 0.0; HAL_PIN(motor_temp) = 0.0; HAL_PIN(rt_calc_time) = 0.0; HAL_PIN(frt_calc_time) = 0.0; @@ -280,9 +279,9 @@ int main(void) HAL_PIN(cmd_unit) = 0.0;//pos,vel,torque HAL_PIN(cmd_rev) = 0.0; HAL_PIN(cmd_res) = 2000.0; - HAL_PIN(en_condition) = 0.0; + HAL_PIN(en_condition) = 0.0;//enable condition HAL_PIN(error_out) = 0.0; - HAL_PIN(pos_static) = 0.0;//track pos in disabled and error condition + HAL_PIN(pos_static) = 0.0;//track pos in disabled and error condition TODO: not implemented HAL_PIN(sin_offset) = 0.0; HAL_PIN(cos_offset) = 0.0; diff --git a/src/main.h b/src/main.h new file mode 100644 index 00000000..b258e29c --- /dev/null +++ b/src/main.h @@ -0,0 +1,24 @@ +//fault state +typedef enum{ + DISABLED = 0, + ENABLED, + PHASING, + SOFT_FAULT, + HARD_FAULT +} state_t; + +//fault errors +typedef enum{ + NO_ERROR = 0, + CMD_ERROR, + FB0_ERROR, + FB1_ERROR, + POS_ERROR, + SAT_ERROR, + HV_ERROR, + HV_TEMP_ERROR, + HV_VOLT_ERROR, + MOT_TEMP_ERROR, +} fault_t; + +void Wait(uint32_t ms); diff --git a/src/syscalls.c b/src/syscalls.c index e39fb235..8b9aef4a 100644 --- a/src/syscalls.c +++ b/src/syscalls.c @@ -67,9 +67,12 @@ ssize_t _read(int fd, void *ptr, size_t len) return len; } - +//TODO: check if connected? int _write(int fd, const char *ptr, int len) { + if(!USB_CDC_is_connected()){ + return 0; + } char *c = (char *) ptr; (void) fd; int sent = 0; diff --git a/stm32f103/src/main.c b/stm32f103/src/main.c index ae6dfccf..9bdfecdd 100644 --- a/stm32f103/src/main.c +++ b/stm32f103/src/main.c @@ -57,14 +57,14 @@ uint32_t w_error = 0; #define TOFLOAT32(a) ((uint32_t)((a) / 65536)) #define TEMP_RES 32 -#define SCALE (TEMP_RES / ARES) +#define TEMP_SCALE (TEMP_RES / ARES) int16_t temp_buf[TEMP_RES]; float tempb(float i){ - unsigned int x = (int)(i * SCALE); + unsigned int x = (int)(i * TEMP_SCALE); float a = TOFLOAT(temp_buf[x]); float b = TOFLOAT(temp_buf[x + 1]); - return(a + (b - a) * (i * SCALE - x)); + return(a + (b - a) * (i * TEMP_SCALE - x)); } volatile uint32_t timeout = 99999; @@ -474,7 +474,7 @@ int main(void) packet_from_hv.head.key = 0; #ifndef TROLLER for(int i = 0; i < TEMP_RES; i++){ - temp_buf[i] = TOFIXED(TEMP(i / SCALE)); + temp_buf[i] = TOFIXED(TEMP(i / TEMP_SCALE)); } #endif while(1){