Skip to content

Commit

Permalink
Preparation for boot loader pin initialization
Browse files Browse the repository at this point in the history
Added Toyota motor temperature sensor
Added tripmode "AutoResume" that will restart inverter 0.5 seconds after it tripped - do not use with FOC code!
Ramping up heat dissipation current to avoid controller overshoot
Improved initial current sensor offset calibration
Fixed spurious current offset warnings
  • Loading branch information
jsphuebner committed Oct 17, 2020
1 parent a4f4803 commit cf19d6f
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 32 deletions.
1 change: 1 addition & 0 deletions include/hwdefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#define TERM_USART_DMATX DMA_CHANNEL2 //this means we can not use it on rev1 hardware (TIM3_CH3)
#define TERM_USART_DR USART3_DR
#define TERM_BUFSIZE 128
#define UARTDMABLOCKED //enables special code for Rev1 boards
//Address of parameter block in flash
#define FLASH_PAGE_SIZE 1024
#define PARAM_ADDRESS 0x0801FC00
Expand Down
1 change: 1 addition & 0 deletions include/hwinit.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ void nvic_setup(void);
void rtc_setup(void);
void tim_setup(void);
HWREV detect_hw(void);
void write_bootloader_pininit();

#ifdef __cplusplus
}
Expand Down
13 changes: 7 additions & 6 deletions include/param_prj.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#define VER 4.93.R
#define VER 4.94.R

/* Entries must be ordered as follows:
1. Saveable parameters (id != 0)
Expand All @@ -36,7 +36,7 @@
PARAM_ENTRY(CAT_MOTOR, numimp, "ppr", 8, 8192, 60, 15 ) \
PARAM_ENTRY(CAT_MOTOR, dirchrpm, "rpm", 0, 20000, 100, 87 ) \
PARAM_ENTRY(CAT_MOTOR, dirmode, DIRMODES, 0, 4, 1, 95 ) \
PARAM_ENTRY(CAT_MOTOR, snsm, SNS_M, 12, 15, 12, 46 )
PARAM_ENTRY(CAT_MOTOR, snsm, SNS_M, 12, 16, 12, 46 )

#define MOTOR_PARAMETERS_SINE \
PARAM_ENTRY(CAT_MOTOR, boost, "dig", 0, 37813, 1700, 1 ) \
Expand Down Expand Up @@ -129,7 +129,7 @@
PARAM_ENTRY(CAT_AUTOM, cruisemode, BTNSWITCH, 0, 2, 0, 62 ) \
PARAM_ENTRY(CAT_CONTACT, udcsw, "V", 0, 1000, 330, 20 ) \
PARAM_ENTRY(CAT_CONTACT, udcswbuck, "V", 0, 1000, 540, 80 ) \
PARAM_ENTRY(CAT_CONTACT, tripmode, TRIPMODES, 0, 2, 0, 86 ) \
PARAM_ENTRY(CAT_CONTACT, tripmode, TRIPMODES, 0, 3, 0, 86 ) \
PARAM_ENTRY(CAT_PWM, pwmfunc, PWMFUNCS, 0, 3, 0, 58 ) \
PARAM_ENTRY(CAT_PWM, pwmgain, "", -100000,100000, 100, 40 ) \
PARAM_ENTRY(CAT_PWM, pwmofs, "dig", -65535, 65535, 0, 41 ) \
Expand Down Expand Up @@ -234,9 +234,9 @@
#define PWMFRQS "0=17.6kHz, 1=8.8kHz, 2=4.4KHz"
#define PWMPOLS "0=ACTHIGH, 1=ACTLOW"
#define DIRS "-1=Reverse, 0=Neutral, 1=Forward"
#define TRIPMODES "0=AllOff, 1=DcSwOn, 2=PrechargeOn"
#define TRIPMODES "0=AllOff, 1=DcSwOn, 2=PrechargeOn, 3=AutoResume"
#define SNS_HS "0=JCurve, 1=Semikron, 2=MBB600, 3=KTY81, 4=PT1000, 5=NTCK45_2k2, 6=Leaf"
#define SNS_M "12=KTY83-110, 13=KTY84-130, 14=Leaf, 15=KTY81-110"
#define SNS_M "12=KTY83-110, 13=KTY84-130, 14=Leaf, 15=KTY81-110, 16=Toyota"
#define PWMFUNCS "0=tmpm, 1=tmphs, 2=speed, 3=speedfrq"
#define BTNSWITCH "0=Button, 1=Switch, 2=CAN"
#define DIRMODES "0=Button, 1=Switch, 2=ButtonReversed, 3=SwitchReversed, 4=DefaultForward"
Expand Down Expand Up @@ -321,7 +321,8 @@ enum _tripmodes
{
TRIP_ALLOFF = 0,
TRIP_DCSWON,
TRIP_PRECHARGEON
TRIP_PRECHARGEON,
TRIP_AUTORESUME
};

enum _dirmodes
Expand Down
55 changes: 51 additions & 4 deletions include/temp_meas.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ class TempMeas
TEMP_KTY84 = 13,
TEMP_LEAF = 14,
TEMP_KTY81M = 15,
TEMP_TESLA_100K = 16,
TEMP_TESLA_52K = 17,
TEMP_TESLA_LDU_FLUID = 18,
TEMP_TESLA_10K = 19,
TEMP_TOYOTA = 16,
TEMP_TESLA_100K = 17,
TEMP_TESLA_52K = 18,
TEMP_TESLA_LDU_FLUID = 19,
TEMP_TESLA_10K = 20,
TEMP_LAST
};

Expand Down Expand Up @@ -466,6 +467,52 @@ class TempMeas
1007 ,\
980 ,\

#define TOYOTA_M \
4009 ,\
4007 ,\
4004 ,\
4000 ,\
3996 ,\
3990 ,\
3983 ,\
3974 ,\
3963 ,\
3950 ,\
3935 ,\
3917 ,\
3895 ,\
3870 ,\
3841 ,\
3808 ,\
3770 ,\
3726 ,\
3678 ,\
3624 ,\
3564 ,\
3499 ,\
3427 ,\
3350 ,\
3267 ,\
3180 ,\
3087 ,\
2990 ,\
2890 ,\
2786 ,\
2680 ,\
2572 ,\
2464 ,\
2355 ,\
2247 ,\
2140 ,\
2034 ,\
1931 ,\
1831 ,\
1733 ,\
1640 ,\
1549 ,\
1463 ,\
1380 ,\
1302


#endif
Expand Down
2 changes: 1 addition & 1 deletion libopeninv
Binary file modified misc/temp_sensors.ods
Binary file not shown.
43 changes: 43 additions & 0 deletions src/hwinit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@
#include <libopencm3/stm32/timer.h>
#include <libopencm3/stm32/dma.h>
#include <libopencm3/stm32/rtc.h>
#include <libopencm3/stm32/crc.h>
#include <libopencm3/stm32/flash.h>
#include "hwdefs.h"
#include "hwinit.h"
#include "stm32_loader.h"
#include "my_string.h"

/**
* Start clocks of all needed peripherals
Expand Down Expand Up @@ -104,6 +108,45 @@ HWREV detect_hw()
return HW_REV2;
}

void write_bootloader_pininit()
{
struct pincommands *flashCommands = (struct pincommands *)PINDEF_ADDRESS;
struct pincommands commands;

memset32((int*)&commands, 0, PINDEF_NUMWORDS);

commands.pindef[0].port = GPIOC;
commands.pindef[0].inout = PIN_OUT;
commands.pindef[0].level = 0;

if (hwRev == HW_BLUEPILL)
{
commands.pindef[0].pin = GPIO15;
}
else
{
commands.pindef[0].pin = GPIO13;
}

crc_reset();
uint32_t crc = crc_calculate_block(((uint32_t*)&commands), PINDEF_NUMWORDS);
commands.crc = crc;

if (commands.crc != flashCommands->crc)
{
flash_unlock();
flash_erase_page(PINDEF_ADDRESS);

//Write flash including crc, therefor <=
for (uint32_t idx = 0; idx <= PINDEF_NUMWORDS; idx++)
{
uint32_t* pData = ((uint32_t*)&commands) + idx;
flash_program_word(PINDEF_ADDRESS + idx * sizeof(uint32_t), *pData);
}
flash_lock();
}
}

/**
* Setup UART3 115200 8N1
*/
Expand Down
10 changes: 5 additions & 5 deletions src/inc_encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -544,11 +544,6 @@ uint16_t Encoder::DecodeAngle(bool invert)
int sin = adc_read_injected(ADC1, sinChan);
int cos = adc_read_injected(ADC1, cosChan);

int temp = MIN(sin, cos);
resolverMin = MIN(temp, resolverMin);
temp = MAX(sin, cos);
resolverMax = MAX(temp, resolverMax);

//Wait for signal to reach usable amplitude
if ((resolverMax - resolverMin) > MIN_RES_AMP)
{
Expand All @@ -558,6 +553,11 @@ uint16_t Encoder::DecodeAngle(bool invert)
}
else
{
int temp = MIN(sin, cos);
resolverMin = MIN(temp, resolverMin);
temp = MAX(sin, cos);
resolverMax = MAX(temp, resolverMax);

if (0 == startupDelay)
{
ErrorMessage::Post(ERR_LORESAMP);
Expand Down
31 changes: 23 additions & 8 deletions src/pwmgeneration-foc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ void PwmGeneration::Run()

void PwmGeneration::SetTorquePercent(s32fp torquePercent)
{
static int32_t heatCurRamped = 0;
s32fp brkrampstr = Param::Get(Param::brkrampstr);
int direction = Param::GetInt(Param::dir);
int heatCur = Param::GetInt(Param::heatcur);
Expand All @@ -153,7 +154,8 @@ void PwmGeneration::SetTorquePercent(s32fp torquePercent)
if (speed == 0 && torquePercent <= 0)
{
iq = 0;
id = (heatCur * 2) / 3;
heatCurRamped = RAMPUP(heatCurRamped, heatCur, 10);
id = heatCurRamped;
}
/*else if (torquePercent > 0)
{
Expand All @@ -162,11 +164,13 @@ void PwmGeneration::SetTorquePercent(s32fp torquePercent)
else
{
FOC::Mtpa(is, id, iq);
heatCurRamped = 0;
}
}
else
{
FOC::Mtpa(is, id, iq);
heatCurRamped = 0;
}

qController.SetRef(FP_FROMINT(iq));
Expand Down Expand Up @@ -225,22 +229,33 @@ void PwmGeneration::PwmInit()

s32fp PwmGeneration::ProcessCurrents(s32fp& id, s32fp& iq)
{
static int il1Avg = 0, il2Avg = 0;
const int offsetSamples = 16;

if (initwait > 0)
{
initwait--;
SetCurrentOffset(AnaIn::il1.Get(), AnaIn::il2.Get());

if (initwait <= offsetSamples)
{
il1Avg += AnaIn::il1.Get();
il2Avg += AnaIn::il2.Get();
}
else
{
il1Avg = il2Avg = 0;
}

if (initwait == 1)
{
SetCurrentOffset(il1Avg / offsetSamples, il2Avg / offsetSamples);
}
}
else
{
s32fp il1 = GetCurrent(AnaIn::il1, ilofs[0], Param::Get(Param::il1gain));
s32fp il2 = GetCurrent(AnaIn::il2, ilofs[1], Param::Get(Param::il2gain));

//1s after motor stand still and no current request, recalibrate current sensor offset
if (idleCounter == pwmfrq)
{
SetCurrentOffset(AnaIn::il1.Get(), AnaIn::il2.Get());
}

if ((Param::GetInt(Param::pinswap) & SWAP_CURRENTS) > 0)
FOC::ParkClarke(il2, il1, angle);
else
Expand Down
19 changes: 11 additions & 8 deletions src/stm32_sine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,7 @@ static void SetContactorsOffState()
case TRIP_ALLOFF:
DigIo::dcsw_out.Clear();
break;
case TRIP_AUTORESUME:
case TRIP_DCSWON:
//do nothing
break;
Expand Down Expand Up @@ -600,14 +601,10 @@ static void Ms10Task(void)
CalcAndOutputTemp();
Param::SetInt(Param::speed, Encoder::GetSpeed());

if (MOD_RUN == opmode)
if (MOD_RUN == opmode && initWait == -1)
{
PwmGeneration::SetTorquePercent(torquePercent);
}
else if (MOD_OFF == opmode && Encoder::GetSpeed() == 0)
{
PwmGeneration::SetCurrentOffset(AnaIn::il1.Get(), AnaIn::il2.Get());
}
else if (MOD_BOOST == opmode || MOD_BUCK == opmode)
{
s32fp chargeCur = Param::Get(Param::chargecur);
Expand Down Expand Up @@ -651,7 +648,8 @@ static void Ms10Task(void)
if ((chargemode == MOD_BUCK && udc >= Param::Get(Param::udcswbuck)) || chargemode == MOD_BOOST)
newMode = chargemode;
}
else if (Param::GetBool(Param::din_start))
else if (Param::GetBool(Param::din_start) ||
(Param::GetInt(Param::tripmode) == TRIP_AUTORESUME && PwmGeneration::Tripped()))
{
newMode = MOD_RUN;
}
Expand Down Expand Up @@ -680,11 +678,13 @@ static void Ms10Task(void)
initWait = 50;

SetContactorsOffState();
PwmGeneration::SetTorquePercent(0);
PwmGeneration::SetOpmode(MOD_OFF);
Throttle::cruiseSpeed = -1;
}
else if (0 == initWait)
{
Throttle::RampThrottle(0); //Restart ramp
Encoder::Reset();
//this applies new deadtime and pwmfrq and enables the outputs for the given mode
PwmGeneration::SetOpmode(opmode);
Expand All @@ -693,9 +693,11 @@ static void Ms10Task(void)
DigIo::vtg_out.Clear();
initWait = -1;
}
else if (hwRev == HW_TESLAM3 && initWait == 10)
else if (initWait == 10)
{
DigIo::vtg_out.Set();
PwmGeneration::SetCurrentOffset(AnaIn::il1.Get(), AnaIn::il2.Get());
if (hwRev == HW_TESLAM3)
DigIo::vtg_out.Set();
initWait--;
}
else if (initWait > 0)
Expand Down Expand Up @@ -862,6 +864,7 @@ extern "C" int main(void)
clock_setup();
rtc_setup();
ConfigureVariantIO();
write_bootloader_pininit();

//Additional test pins on JTAG header
//AFIO_MAPR |= AFIO_MAPR_SPI1_REMAP | AFIO_MAPR_SWJ_CFG_JTAG_OFF_SW_OFF;
Expand Down
4 changes: 4 additions & 0 deletions src/temp_meas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ static const uint16_t TeslaFluid[] = { TESLA_LDU_FLUID };
/* Temp sensor embedded in Tesla rear heatsink */
static const uint16_t Tesla10k[] = { TESLA_10K };

/* Temp sensor embedded in many Toyota motors */
static const uint16_t Toyota[] = { TOYOTA_M };

/* contributed by Fabian Brauss */
/* Temp sensor KTY81-121 */
static const uint16_t Kty81hs[] = { KTY81_HS };
Expand All @@ -94,6 +97,7 @@ static const TEMP_SENSOR sensors[] =
{ -40, 300, 10, TABLEN(Kty84), PTC, Kty84 },
{ -20, 150, 10, TABLEN(leaf), NTC, leaf },
{ -50, 150, 10, TABLEN(kty81m), PTC, kty81m },
{ -20, 200, 5, TABLEN(Toyota), PTC, Toyota },
{ -20, 190, 5, TABLEN(Tesla100k), PTC, Tesla100k },
{ 0, 100, 10, TABLEN(Tesla52k), PTC, Tesla52k },
{ 5, 100, 5, TABLEN(TeslaFluid),PTC, TeslaFluid },
Expand Down

0 comments on commit cf19d6f

Please sign in to comment.