new conf system

This commit is contained in:
crinq
2017-07-10 00:03:00 +02:00
parent 065c99db21
commit 5a67cc3811
8 changed files with 142 additions and 233 deletions

11
conf/template/enc_fb0.txt Normal file
View File

@@ -0,0 +1,11 @@
load enc_fb
enc_fb0.rt_prio = 2
enc_fb0.frt_prio = 1
enc_fb0.res = conf0.fb_res
enc_fb0.sin = adc0.sin3
enc_fb0.cos = adc0.cos3
enc_fb0.quad = adc0.quad
rev1.in = enc_fb0.pos
fb_switch0.mot_polecount = 1
fb_switch0.mot_state = 1
fault0.mot_fb_error = enc_fb0.error

15
conf/template/misc.txt Normal file
View File

@@ -0,0 +1,15 @@
fault0.mot_temp = 0
term0.wave0 = vel0.pos_out
term0.wave1 = fb_switch0.pos_fb
term0.wave2 = vel0.vel
term0.wave3 = vel1.vel
conf0.autophase = 0
conf0.max_ac_cur = 5
conf0.out_rev = 1
conf0.vel_i = 2
conf0.j = 0.0002
conf0.cur_i = 0.0001
conf0.cur_p = 0.2
rev1.rev = 1
rev0.in = sim0.msin
sim0.freq = 0.2

View File

@@ -1,6 +1,6 @@
load conf
load adc
load res_limit
load reslimit
load rev
load rev
load rev
@@ -16,9 +16,8 @@ load iit
load sim
load stp
load io
confload
adc0.rt_prio = 1
res_limit0.rt_prio = 3
reslimit0.rt_prio = 3
rev0.rt_prio = 4
rev1.rt_prio = 4
rev2.rt_prio = 4
@@ -110,59 +109,3 @@ io0.out0 = fault0.mot_brake
io0.fan = fault0.hv_fan
io0.fault = fault0.fault
io0.state = fault0.state
load pmsm_limits
load pmsm_ttc
pmsm_limits0.rt_prio = 7
pmsm_ttc0.rt_prio = 9
pmsm_limits0.r = conf0.r
pmsm_limits0.ld = conf0.l
pmsm_limits0.lq = conf0.l
pmsm_limits0.psi = conf0.psi
pmsm_limits0.j = conf0.j
pmsm_limits0.polecount = conf0.polecount
pmsm_ttc0.psi = conf0.psi
pmsm_ttc0.polecount = conf0.polecount
pmsm_limits0.ac_volt = hv0.pwm_volt
pmsm_limits0.iq = hv0.q_fb
pmsm_limits0.id = hv0.d_fb
pid0.max_torque = pmsm_limits0.max_torque
pid0.min_torque = pmsm_limits0.min_torque
pid0.max_vel = pmsm_limits0.abs_max_vel
pmsm_ttc0.torque = pid0.torque_cor_cmd
hv0.q_cmd = pmsm_ttc0.cur
load enc_fb
enc_fb0.rt_prio = 2
enc_fb0.frt_prio = 1
enc_fb0.res = conf0.fb_res
enc_fb0.sin = adc0.sin3
enc_fb0.cos = adc0.cos3
enc_fb0.quad = adc0.quad
rev1.in = enc_fb0.pos
fb_switch0.mot_polecount = 1
fb_switch0.mot_state = 1
fault0.mot_fb_error = enc_fb0.error
load uvw
uvw0.rt_prio = 2
uvw0.u = io0.fb1a
uvw0.v = io0.fb1b
uvw0.w = io0.fb1z
rev2.in = uvw0.pos
fb_switch0.com_state = 3
fb_switch0.com_polecount = conf0.polecount
fault0.mot_temp = 0
term0.wave0 = vel0.pos_out
term0.wave1 = fb_switch0.pos_fb
term0.wave2 = vel0.vel
term0.wave3 = vel1.vel
conf0.autophase = 0
conf0.max_ac_cur = 5
conf0.out_rev = 1
conf0.vel_i = 2
conf0.j = 0.0002
conf0.cur_i = 0.0001
conf0.cur_p = 0.2
rev1.rev = 1
rev0.in = sim0.msin
sim0.freq = 0.2
init_hw
start

20
conf/template/pmsm.txt Normal file
View File

@@ -0,0 +1,20 @@
load pmsm_limits
load pmsm_ttc
pmsm_limits0.rt_prio = 7
pmsm_ttc0.rt_prio = 9
pmsm_limits0.r = conf0.r
pmsm_limits0.ld = conf0.l
pmsm_limits0.lq = conf0.l
pmsm_limits0.psi = conf0.psi
pmsm_limits0.j = conf0.j
pmsm_limits0.polecount = conf0.polecount
pmsm_ttc0.psi = conf0.psi
pmsm_ttc0.polecount = conf0.polecount
pmsm_limits0.ac_volt = hv0.pwm_volt
pmsm_limits0.iq = hv0.q_fb
pmsm_limits0.id = hv0.d_fb
pid0.max_torque = pmsm_limits0.max_torque
pid0.min_torque = pmsm_limits0.min_torque
pid0.max_vel = pmsm_limits0.abs_max_vel
pmsm_ttc0.torque = pid0.torque_cor_cmd
hv0.q_cmd = pmsm_ttc0.cur

8
conf/template/uvw.txt Normal file
View File

@@ -0,0 +1,8 @@
load uvw
uvw0.rt_prio = 2
uvw0.u = io0.fb1a
uvw0.v = io0.fb1b
uvw0.w = io0.fb1z
rev2.in = uvw0.pos
fb_switch0.com_state = 3
fb_switch0.com_polecount = conf0.polecount

View File

@@ -697,7 +697,29 @@ void hal_print_pin(volatile hal_pin_inst_t * p){
}
}
char * findline(char * ptr){
for(int i = 0; i < 64; i++){
if(ptr[i] == 0){
return(0);
}
if(ptr[i] == '\n'){
return(ptr + i + 1);
}
}
return(0);
}
uint32_t hal_parse_(char * cmd);
uint32_t hal_parse(char * cmd){
do{
hal_parse_(cmd);
cmd = findline(cmd);
}while(cmd);
return(0);
}
uint32_t hal_parse_(char * cmd){
if(call_cmd(cmd)){
return(1);
}

View File

@@ -66,179 +66,6 @@ HAL_PIN(cur_ff);
HAL_PIN(cur_ind);
HAL_PIN(max_sat);
//initialise empty or corrupt flash section
uint16_t conf_init(){
FLASH_Unlock();
uint16_t ret = EE_Init();
FLASH_Lock();
printf("OK\n");
return ret;
}
COMMAND("confinit", conf_init);
//save all pins to flash
void conf_save(){
//TODO: check hal running?
// if(hal.rt_state != RT_STOP || hal.frt_state != FRT_STOP){
// printf("cannot save while hal is running(run 'stop' command)\n");
// return;
// }
hal_comp_inst_t *inst;
inst = (hal_comp_inst_t*)comp_inst_by_name("conf",0);
if(!inst){
printf("conf0 not loaded\n");
return;
}
typedef union{
float f;
uint16_t byte[2];
}param_t;
param_t param;
uint16_t elo;
uint16_t ehi;
uint16_t address = 0;
crc16_t crc = crc16_init();
FLASH_Unlock();
for(int i = 2; i < inst->comp->pin_count; i++){
if(address >= NB_OF_VAR){
printf("NB_OF_VAR too small\n");
FLASH_Lock();
return;
}
param.f = inst->pin_insts[i].source->value;
crc = crc16_update(crc, (void*)&param.byte[0], 2);
crc = crc16_update(crc, (void*)&param.byte[1], 2);
//printf("param: %s=%f address:%i\n",inst->pins[i],param.f,address);
elo = EE_WriteVariable(address,param.byte[0]);
ehi = EE_WriteVariable(address+1,param.byte[1]);
if(elo != FLASH_COMPLETE || ehi != FLASH_COMPLETE){
FLASH_Lock();
printf("error writing to address %i: error: %i,%i\n",address,elo,ehi);
return;
}
address+=2;
}
//write crc
crc = crc16_finalize(crc);
elo = EE_WriteVariable(address, crc);
if(elo != FLASH_COMPLETE){
printf("error writing crc to address %i: error: %i\n",address,elo);
FLASH_Lock();
return;
}
FLASH_Lock();
printf("saved %lu parameters to flash\n",inst->comp->pin_count-2);
}
COMMAND("confsave", conf_save);
//load pins from flash
int conf_load(){
//TODO: check hal running?
// if(hal.rt_state != RT_STOP || hal.frt_state != FRT_STOP){
// return(-4);
// }
hal_comp_inst_t *inst;
inst = (hal_comp_inst_t*)comp_inst_by_name("conf",0);
if(!inst){
printf("conf0 not loaded\n");
return 0;
}
typedef union{
float f;
uint16_t byte[2];
}param_t;
param_t param;
uint16_t address = 0;
uint16_t lo;
uint16_t hi;
uint16_t elo;
uint16_t ehi;
crc16_t crc = crc16_init();
for(int i = 2; i < inst->comp->pin_count; i++){
if(address >= NB_OF_VAR){
return -2;
}
elo = EE_ReadVariable(address,&lo);
ehi = EE_ReadVariable(address+1,&hi);
crc = crc16_update(crc, (void*)&lo, 2);
crc = crc16_update(crc, (void*)&hi, 2);
if(elo == 0 && ehi == 0){
param.byte[0] = lo;
param.byte[1] = hi;
inst->pin_insts[i].source = &(inst->pin_insts[i]);
inst->pin_insts[i].value = param.f;
}else{
printf("error reading address %i: %i,%i\n",address,elo,ehi);
return -3;
}
address+=2;
Wait(1);//TODO: remove wait?
}
elo = EE_ReadVariable(address,&lo);
if(elo != 0){
printf("error reading crc from address %i: %i\n",address,elo);
return -3;
}
crc = crc16_finalize(crc);
if(crc != lo){//CRC error
return -5;
}
printf("loaded %lu parameters from flash\n",inst->comp->pin_count-2);
//TODO: update stuff
//update cmd/fb links
//update_cmd();
//update_fb();
//update_mot();
return 0;
}
COMMAND("confload", conf_load);
//
// void hal_conf_diff(){
// typedef union{
// float f;
// uint16_t byte[2];
// }param_t;
// param_t param;
// uint16_t address = 0;
// uint16_t lo;
// uint16_t hi;
// uint16_t elo;
// uint16_t ehi;
// crc16_t crc = crc16_init();
// for(int i = 0; i < hal.hal_pin_count; i++){
// if(address >= NB_OF_VAR){
// printf("NB_OF_VAR too small\n");
// return;
// }
// char name[6];
// strncpy(name,hal.hal_pins[i]->name,5);
// name[5] = '\0';
// if(!strcmp(name, "conf0")){
// elo = EE_ReadVariable(address,&lo);
// ehi = EE_ReadVariable(address+1,&hi);
// crc = crc16_update(crc, (void*)&lo, 2);
// crc = crc16_update(crc, (void*)&hi, 2);
// if(elo == 0 && ehi == 0){
// param.byte[0] = lo;
// param.byte[1] = hi;
// if(hal.hal_pins[i]->value != param.f){
// hal_term_print_pin(hal.hal_pins[i]);
// }
// }else{
// printf("error reading address %i: %i,%i\n",address,elo,ehi);
// return;
// }
// address+=2;
// Wait(1);//TODO: remove wait?
// }
// }
// }
hal_comp_t conf_comp_struct = {
.name = "conf",
.nrt = 0,

View File

@@ -81,6 +81,65 @@ void nv_reset(char * ptr){
}
COMMAND("reset", nv_reset);
char config[15*1024];
const char* config_ro = (char*)0x08008000;
void flashloadconf(char * ptr){
strncpy(config,config_ro,sizeof(config));
}
COMMAND("flashloadconf", flashloadconf);
void flashsaveconf(char * ptr){
printf("erasing flash page...\n");
FLASH_Unlock();
if(FLASH_EraseSector(FLASH_Sector_2,VoltageRange_3) != FLASH_COMPLETE){
printf("error!\n");
FLASH_Lock();
return;
}
printf("saving conf\n");
int i = 0;
int ret = 0;
do{
ret = FLASH_ProgramByte((uint32_t)(config_ro + i), config[i]) != FLASH_COMPLETE;
if(ret){
printf("error writing %i\n",ret);
break;
}
}while(config[i++] != 0);
printf("OK %i bytes written\n",i);
FLASH_Lock();
}
COMMAND("flashsaveconf", flashsaveconf);
void loadconf(char * ptr){
hal_parse(config);
}
COMMAND("loadconf", loadconf);
void showconf(char * ptr){
printf("** ram **\n");
printf("%s",config);
printf("** flash **\n");
printf("%s",config_ro);
}
COMMAND("showconf", showconf);
void appendconf(char * ptr){
printf("adding %s\n",ptr);
strncat(config,ptr,sizeof(config) - 1);
strncat(config,"\n",sizeof(config) - 1);
}
COMMAND("appendconf", appendconf);
void deleteconf(char * ptr){
config[0] = '\0';
}
COMMAND("deleteconf", deleteconf);
int main(void)
{
// Relocate interrupt vectors
@@ -92,12 +151,16 @@ int main(void)
hal_init(0.0002, 0.00005);
// hal load comps
load_comp(comp_by_name("term"));
hal_parse("flashloadconf");
hal_parse("loadconf");
// load_comp(comp_by_name("sim"));
// load_comp(comp_by_name("io"));
// load_comp(comp_by_name("encm"));
// load_comp(comp_by_name("hv"));
// load_comp(comp_by_name("hal_test"));
hal_parse("term0.rt_prio = 20");
//hal_parse("term0.rt_prio = 20");
// hal_parse("load conf\nload adc\n load reslimit\n# foo bar\nload rev\nload pid");
// hal_parse("encm0.rt_prio = 1");
// hal_parse("sim0.rt_prio = 2");
// hal_parse("io0.rt_prio = 10");