Radio Control/ESC2020. 4. 29. 21:29

config.h

/*

    This file is part of AutoQuad ESC32.


    AutoQuad ESC32 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.


    AutoQuad ESC32 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 AutoQuad ESC32.  If not, see <http://www.gnu.org/licenses/>.


    Copyright © 2011, 2012, 2013  Bill Nesbitt

*/


#ifndef _CONFIG_H

#define _CONFIG_H


#define DEFAULT_CONFIG_VERSION 2.01f

#define DEFAULT_STARTUP_MODE 0.0f

#define DEFAULT_BAUD_RATE 230400

#define DEFAULT_ESC_ID 0


#define DEFAULT_PTERM 0.25f

#define DEFAULT_PNFAC 10.0f

#define DEFAULT_ITERM 0.0006f

#define DEFAULT_INFAC 0.15f


#define DEFAULT_FF1TERM 0.0f

#define DEFAULT_FF2TERM 0.0f


#define DEFAULT_CL1TERM 0.0f

#define DEFAULT_CL2TERM 0.0f

#define DEFAULT_CL3TERM 0.0f

#define DEFAULT_CL4TERM 0.0f

#define DEFAULT_CL5TERM 0.0f


#define DEFAULT_THR1TERM 0.0f

#define DEFAULT_THR2TERM 1.0f


#define DEFAULT_SHUNT_RESISTANCE 0.5f     // milli Ohms

#define DEFAULT_MIN_PERIOD 50.0f     // us

#define DEFAULT_MAX_PERIOD 12000.0f    // us

#define DEFAULT_BLANKING_MICROS 30.0f     // us

#define DEFAULT_ADVANCE 10.0f     // electrical degrees

#define DEFAULT_START_VOLTAGE 1.1f     // voltage used to start motor

#define DEFAULT_START_ALIGN_TIME 600     // ms to align rotor in known position

#define DEFAULT_START_ALIGN_VOLTAGE 0.9f     // max voltage during align (around 0.8 * START_VOLTAGE)

#define DEFAULT_START_STEPS_NUM 0.0f     // steps without commutation

#define DEFAULT_START_STEPS_PERIOD 16000     // us betweet steps

#define DEFAULT_START_STEPS_ACCEL 0.0f     // us each following step will be shorter (acceleration)

#define DEFAULT_GOOD_DETECTS_START 75.0f     // after which will go into RUNNING mode

#define DEFAULT_BAD_DETECTS_DISARM 48.0f     // after which will go into DISARMED mode

#define DEFAULT_MAX_CURRENT 20.0f     // amps

#define DEFAULT_SWITCH_FREQ 20.0f     // output PWM frequency in KHz

#define DEFAULT_MOTOR_POLES 14.0f


#define DEFAULT_PWM_MIN_PERIOD 2200     // minimum valid period

#define DEFAULT_PWM_MAX_PERIOD 25000     // maximum valid period


#define DEFAULT_PWM_MIN_VALUE 750     // minimum to consider pulse a valid signal

#define DEFAULT_PWM_LO_VALUE 1000     // lowest running value

#define DEFAULT_PWM_HI_VALUE 1950     // highest running value

#define DEFAULT_PWM_MAX_VALUE 2250     // maximum to consider pulse a valid signal

#define DEFAULT_PWM_MIN_START 1100     // minimum value required to start


#define DEFAULT_PWM_LOWPASS 0.0f     // lowpass on PWM input values (0 = none, 10 = heavy, no upper limit)

#define DEFAULT_RPM_MEAS_LP 0.5f     // lowpass measured RPM values for closed loop control (0 = none, 0.99 = max, >=1 not allowed)


#define DEFAULT_PWM_RPM_SCALE 6500     // RPM equivalent of maximum PWM IN in CLOSED_LOOP mode


#define DEFAULT_FET_BRAKING 0


#define DEFAULT_SERVO_DUTY 16.0f     // %

#define DEFAULT_SERVO_P 0.05f

#define DEFAULT_SERVO_D 0.0f

#define DEFAULT_SERVO_MAX_RATE 1000.0f     // deg/s

#define DEFAULT_SERVO_SCALE 360.0f     // deg

#define DEFAULT_DIRECTION 1.0f     // 1 == forward, -1 == reverse


#define FLASH_PAGE_SIZE ((uint16_t)0x400)

#define FLASH_WRITE_ADDR (0x08000000 + (uint32_t)FLASH_PAGE_SIZE * 63)    // use the last KB for storage


enum configParameters {

    CONFIG_VERSION = 0,

    STARTUP_MODE,

    BAUD_RATE,

    PTERM,

    ITERM,

    FF1TERM,

    FF2TERM,

    CL1TERM,

    CL2TERM,

    CL3TERM,

    CL4TERM,

    CL5TERM,

    SHUNT_RESISTANCE,

    MIN_PERIOD,

    MAX_PERIOD,

    BLANKING_MICROS,

    ADVANCE,

    START_VOLTAGE,

    GOOD_DETECTS_START,

    BAD_DETECTS_DISARM,

    MAX_CURRENT,

    SWITCH_FREQ,

    MOTOR_POLES,

    PWM_MIN_PERIOD,

    PWM_MAX_PERIOD,

    PWM_MIN_VALUE,

    PWM_LO_VALUE,

    PWM_HI_VALUE,

    PWM_MAX_VALUE,

    PWM_MIN_START,

    PWM_RPM_SCALE,

    FET_BRAKING,

    PNFAC,

    INFAC,

    THR1TERM,

    THR2TERM,

    START_ALIGN_TIME,

    START_ALIGN_VOLTAGE,

    START_STEPS_NUM,

    START_STEPS_PERIOD,

    START_STEPS_ACCEL,

    PWM_LOWPASS,

    RPM_MEAS_LP,

    SERVO_DUTY,

    SERVO_P,

    SERVO_D,

    SERVO_MAX_RATE,

    SERVO_SCALE,

    ESC_ID,

    DIRECTION,

    CONFIG_NUM_PARAMS

};


extern float p[CONFIG_NUM_PARAMS];

extern const char *configParameterStrings[];

extern const char *configFormatStrings[];


extern void configInit(void);

extern int configSetParam(char *param, float value);

extern int configSetParamByID(int i, float value);

extern int configGetId(char *param);

extern float configGetParam(char *param);

extern void configLoadDefault(void);

extern void configReadFlash(void);

extern int configWriteFlash(void);


#endif


config.c

/*

    This file is part of AutoQuad ESC32.


    AutoQuad ESC32 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.


    AutoQuad ESC32 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 AutoQuad ESC32.  If not, see <http://www.gnu.org/licenses/>.


    Copyright © 2011-2014  Bill Nesbitt

*/


#include "config.h"

#include "fet.h"

#include "pwm.h"

#include "adc.h"

#include "run.h"

#include "serial.h"

#include "can.h"

#include "stm32f10x_flash.h"

#include "stm32f10x_rcc.h"

#include <string.h>

#include <math.h>


float p[CONFIG_NUM_PARAMS];


const char *configParameterStrings[] = {

    "CONFIG_VERSION",

    "STARTUP_MODE",

    "BAUD_RATE",

    "PTERM",

    "ITERM",

    "FF1TERM",

    "FF2TERM",

    "CL1TERM",

    "CL2TERM",

    "CL3TERM",

    "CL4TERM",

    "CL5TERM",

    "SHUNT_RESISTANCE",

    "MIN_PERIOD",

    "MAX_PERIOD",

    "BLANKING_MICROS",

    "ADVANCE",

    "START_VOLTAGE",

    "GOOD_DETECTS_START",

    "BAD_DETECTS_DISARM",

    "MAX_CURRENT",

    "SWITCH_FREQ",

    "MOTOR_POLES",

    "PWM_MIN_PERIOD",

    "PWM_MAX_PERIOD",

    "PWM_MIN_VALUE",

    "PWM_LO_VALUE",

    "PWM_HI_VALUE",

    "PWM_MAX_VALUE",

    "PWM_MIN_START",

    "PWM_RPM_SCALE",

    "FET_BRAKING",

    "PNFAC",

    "INFAC",

    "THR1TERM",

    "THR2TERM",

    "START_ALIGN_TIME",

    "START_ALIGN_VOLTAGE",

    "START_STEPS_NUM",

    "START_STEPS_PERIOD",

    "START_STEPS_ACCEL",

    "PWM_LOWPASS",

    "RPM_MEAS_LP",

    "SERVO_DUTY",

    "SERVO_P",

    "SERVO_D",

    "SERVO_MAX_RATE",

    "SERVO_SCALE",

    "ESC_ID",

    "DIRECTION"

};


const char *configFormatStrings[] = {

    "%f",     // CONFIG_VERSION

    "%.0f",     // STARTUP_MODE

    "%.0f baud",    // BAUD_RATE

    "%.3f",     // PTERM

    "%.5f",     // ITERM

    "%+e",     // FF1TERM

    "%+e",     // FF2TERM

    "%+e",     // CL1TERM

    "%+e",     // CL2TERM

    "%+e",     // CL3TERM

    "%+e",     // CL4TERM

    "%+e",     // CL5TERM

    "%.3f mohms",   // SHUNT_RESISTANCE

    "%.0f us",     // MIN_PERIOD

    "%.0f us",     // MAX_PERIOD

    "%.0f us",     // BLANKING_MICROS

    "%.2f Degs",    // ADVANCE

    "%.2f Volts",   // START_VOLTAGE

    "%.0f",     // GOOD_DETECTS_START

    "%.0f",     // BAD_DETECTS_DISARM

    "%.1f Amps",    // MAX_CURRENT

    "%.1f KHz",     // SWITCH_FREQ

    "%.0f",     // MOTOR_POLES

    "%.0f us",     // PWM_MIN_PERIOD

    "%.0f us",     // PWM_MAX_PERIOD

    "%.0f us",     // PWM_MIN_VALUE

    "%.0f us",     // PWM_LO_VALUE

    "%.0f us",     // PWM_HI_VALUE

    "%.0f us",     // PWM_MAX_VALUE

    "%.0f us",     // PWM_MIN_START

    "%.0f RPM",     // PWM_RPM_SCALE

    "%.0f",     // FET_BRAKING

    "%.2f",     // PNFAC

    "%.2f",     // INFAC

    "%+e",     // THR1TERM

    "%+e",     // THR2TERM

    "%.0f ms",     // START_ALIGN_TIME

    "%.2f Volts",   // START_ALIGN_VOLTAGE

    "%.0f",     // START_STEPS_NUM

    "%.0f us",     // START_STEPS_PERIOD

    "%.0f us",     // START_STEPS_ACCEL

    "%2.2f",     // PWM_LOWPASS

    "%.3f",     // RPM_MEAS_LP

    "%.1f %%",     // SERVO_DUTY

    "%.3f",     // SERVO_P

    "%.3f",     // SERVO_D

    "%.1f deg/s",   // SERVO_MAX_RATE

    "%.1f deg",     // SERVO_SCALE

    "%.0f",     // ESC_ID

    "%.0f"     // DIRECTION

};


void configInit(void) {

    float ver;


    configLoadDefault();


    ver = *(float *)FLASH_WRITE_ADDR;


    if (isnan(ver))

configWriteFlash();

    else if (ver >= p[CONFIG_VERSION])

configReadFlash();

    else if (p[CONFIG_VERSION] > ver)

configWriteFlash();

}


// recalculate constants with bounds checking

void configRecalcConst(void) {

    adcSetConstants();

    fetSetConstants();

    runSetConstants();

    pwmSetConstants();

    serialSetConstants();

    canSetConstants();

}


int configSetParamByID(int i, float value) {

    int ret = 0;


    if (i >= 0 && i < CONFIG_NUM_PARAMS) {

p[i] = value;

configRecalcConst();


ret = 1;

    }


    return ret;

}


int configSetParam(char *param, float value) {

    int ret = 0;

    int i;


    for (i = 0; i < CONFIG_NUM_PARAMS; i++) {

if (!strncasecmp(configParameterStrings[i], param, strlen(configParameterStrings[i]))) {

    configSetParamByID(i, value);

    ret = 1;

    break;

}

    }


    return ret;

}


int configGetId(char *param) {

    int i;


    for (i = 0; i < CONFIG_NUM_PARAMS; i++)

if (!strncasecmp(configParameterStrings[i], param, 16))

    return i;


    return -1;

}


float configGetParam(char *param) {

    int i;


    i = configGetId(param);


    if (i >= 0)

return p[i];

    else

return __float32_nan;

}


void configLoadDefault(void) {

    p[CONFIG_VERSION] = DEFAULT_CONFIG_VERSION;

    p[STARTUP_MODE] = DEFAULT_STARTUP_MODE;

    p[BAUD_RATE] = DEFAULT_BAUD_RATE;

    p[PTERM] = DEFAULT_PTERM;

    p[ITERM] = DEFAULT_ITERM;

    p[FF1TERM] = DEFAULT_FF1TERM;

    p[FF2TERM] = DEFAULT_FF2TERM;

    p[CL1TERM] = DEFAULT_CL1TERM;

    p[CL2TERM] = DEFAULT_CL2TERM;

    p[CL3TERM] = DEFAULT_CL3TERM;

    p[CL4TERM] = DEFAULT_CL4TERM;

    p[CL5TERM] = DEFAULT_CL5TERM;

    p[SHUNT_RESISTANCE] = DEFAULT_SHUNT_RESISTANCE;

    p[MIN_PERIOD] = DEFAULT_MIN_PERIOD;

    p[MAX_PERIOD] = DEFAULT_MAX_PERIOD;

    p[BLANKING_MICROS] = DEFAULT_BLANKING_MICROS;

    p[ADVANCE] = DEFAULT_ADVANCE;

    p[START_VOLTAGE] = DEFAULT_START_VOLTAGE;

    p[GOOD_DETECTS_START] = DEFAULT_GOOD_DETECTS_START;

    p[BAD_DETECTS_DISARM] = DEFAULT_BAD_DETECTS_DISARM;

    p[MAX_CURRENT] = DEFAULT_MAX_CURRENT;

    p[SWITCH_FREQ] = DEFAULT_SWITCH_FREQ;

    p[MOTOR_POLES] = DEFAULT_MOTOR_POLES;

    p[PWM_MIN_PERIOD] = DEFAULT_PWM_MIN_PERIOD;

    p[PWM_MAX_PERIOD] = DEFAULT_PWM_MAX_PERIOD;

    p[PWM_MIN_VALUE] = DEFAULT_PWM_MIN_VALUE;

    p[PWM_LO_VALUE] = DEFAULT_PWM_LO_VALUE;

    p[PWM_HI_VALUE] = DEFAULT_PWM_HI_VALUE;

    p[PWM_MAX_VALUE] = DEFAULT_PWM_MAX_VALUE;

    p[PWM_MIN_START] = DEFAULT_PWM_MIN_START;

    p[PWM_RPM_SCALE] = DEFAULT_PWM_RPM_SCALE;

    p[FET_BRAKING] = DEFAULT_FET_BRAKING;

    p[PNFAC] = DEFAULT_PNFAC;

    p[INFAC] = DEFAULT_INFAC;

    p[THR1TERM] = DEFAULT_THR1TERM;

    p[THR2TERM] = DEFAULT_THR2TERM;

    p[START_ALIGN_TIME] = DEFAULT_START_ALIGN_TIME;

    p[START_ALIGN_VOLTAGE] = DEFAULT_START_ALIGN_VOLTAGE;

    p[START_STEPS_NUM] = DEFAULT_START_STEPS_NUM;

    p[START_STEPS_PERIOD] = DEFAULT_START_STEPS_PERIOD;

    p[START_STEPS_ACCEL] = DEFAULT_START_STEPS_ACCEL;

    p[PWM_LOWPASS] = DEFAULT_PWM_LOWPASS;

    p[RPM_MEAS_LP] = DEFAULT_RPM_MEAS_LP;

    p[SERVO_DUTY] = DEFAULT_SERVO_DUTY;

    p[SERVO_P] = DEFAULT_SERVO_P;

    p[SERVO_D] = DEFAULT_SERVO_D;

    p[SERVO_MAX_RATE] = DEFAULT_SERVO_MAX_RATE;

    p[SERVO_SCALE] = DEFAULT_SERVO_SCALE;

    p[ESC_ID] = DEFAULT_ESC_ID;

    p[DIRECTION] = DEFAULT_DIRECTION;


    configRecalcConst();

}


int configWriteFlash(void) {

    uint16_t prevReloadVal;

    FLASH_Status FLASHStatus;

    uint32_t address;

    int ret = 0;


    prevReloadVal = runIWDGInit(999);


    // Startup HSI clock

    RCC_HSICmd(ENABLE);

    while (RCC_GetFlagStatus(RCC_FLAG_HSIRDY) != SET)

runFeedIWDG();


    FLASH_Unlock();


    FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);


    if ((FLASHStatus = FLASH_ErasePage(FLASH_WRITE_ADDR)) == FLASH_COMPLETE) {

address = 0;

while (FLASHStatus == FLASH_COMPLETE && address < sizeof(p)) {

    if ((FLASHStatus = FLASH_ProgramWord(FLASH_WRITE_ADDR + address, *(uint32_t *)((char *)p + address))) != FLASH_COMPLETE)

break;


    address += 4;

}


ret = 1;

    }


    FLASH_Lock();


    runFeedIWDG();


    // Shutdown HSI clock

    RCC_HSICmd(DISABLE);

    while (RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == SET)

;


    runIWDGInit(prevReloadVal);


    return ret;

}


void configReadFlash(void) {

    memcpy(p, (char *)FLASH_WRITE_ADDR, sizeof(p));


    configRecalcConst();

}



'Radio Control > ESC' 카테고리의 다른 글

ESC32 V2 메뉴얼 - Calibrations  (0) 2020.05.19
ESC32 V2 메뉴얼 - Configuration & Modes  (0) 2020.04.24
ESC32 V2 메뉴얼 - Firmware Flashing  (0) 2020.04.23
ESC32 V2 메뉴얼 - Connections & Wiring  (0) 2020.04.03
BitWizard ESC의 소개  (0) 2018.12.03
Posted by Nature & Life