Skip to content

Commit

Permalink
Added Epcos temp sensor
Browse files Browse the repository at this point in the history
Floating point arithmetic in SetTorquePercent
Fixed filter rounding error in SetTorquePercent
Fixed misinterpretation of pot2 configuration
  • Loading branch information
jsphuebner committed Oct 5, 2021
1 parent 8134e2b commit 7c5fbae
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 58 deletions.
6 changes: 3 additions & 3 deletions include/param_prj.h
Expand Up @@ -17,7 +17,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#define VER 5.11.R
#define VER 5.12.R

/* Entries must be ordered as follows:
1. Saveable parameters (id != 0)
Expand All @@ -37,7 +37,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, 21, 12, 46 )
PARAM_ENTRY(CAT_MOTOR, snsm, SNS_M, 12, 22, 12, 46 )

#define MOTOR_PARAMETERS_SINE \
PARAM_ENTRY(CAT_MOTOR, boost, "dig", 0, 37813, 1700, 1 ) \
Expand Down Expand Up @@ -244,7 +244,7 @@
#define DIRS "-1=Reverse, 0=Neutral, 1=Forward"
#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, 7=BMW-i3"
#define SNS_M "12=KTY83-110, 13=KTY84-130, 14=Leaf, 15=KTY81-110, 16=Toyota, 21=OutlanderFront"
#define SNS_M "12=KTY83-110, 13=KTY84-130, 14=Leaf, 15=KTY81-110, 16=Toyota, 21=OutlanderFront, 22=EpcosB57861-S"
#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
2 changes: 1 addition & 1 deletion include/pwmgeneration.h
Expand Up @@ -32,7 +32,7 @@ class PwmGeneration
static void SetOpmode(int opmode);
static void SetAmpnom(s32fp amp);
static void SetFslip(s32fp fslip);
static void SetTorquePercent(s32fp torque);
static void SetTorquePercent(float torque);
static void SetCurrentOffset(int offset1, int offset2);
static void SetCurrentLimitThreshold(s32fp ocurlim);
static void SetControllerGains(int kp, int ki, int fwkp);
Expand Down
29 changes: 26 additions & 3 deletions include/temp_meas.h
Expand Up @@ -19,8 +19,6 @@
#ifndef TEMP_MEAS_H_INCLUDED
#define TEMP_MEAS_H_INCLUDED

#include "my_fp.h"

class TempMeas
{
public:
Expand All @@ -45,6 +43,7 @@ class TempMeas
TEMP_TESLA_LDU_FLUID = 19,
TEMP_TESLA_10K = 20,
TEMP_OUTLANDERFRONT = 21,
TEMP_EPCOSB56871 = 22,
TEMP_LAST
};

Expand Down Expand Up @@ -580,7 +579,31 @@ class TempMeas
2987 ,\
3002 ,\
3016 ,\
3027
3027

#define EPCOSB57861 \
8 ,\
14 ,\
25 ,\
42 ,\
68 ,\
107 ,\
163 ,\
240 ,\
340 ,\
470 ,\
624 ,\
808 ,\
1009 ,\
1223 ,\
1437 ,\
1642 ,\
1839 ,\
2020 ,\
2183 ,\
2327 ,\
2454


#endif

Expand Down
2 changes: 1 addition & 1 deletion libopeninv
Submodule libopeninv updated 1 files
+1 −1 include/my_math.h
Binary file modified misc/temp_sensors.ods
Binary file not shown.
11 changes: 6 additions & 5 deletions src/pwmgeneration-foc.cpp
Expand Up @@ -142,22 +142,23 @@ void PwmGeneration::Run()
}
}

void PwmGeneration::SetTorquePercent(s32fp torquePercent)
void PwmGeneration::SetTorquePercent(float torquePercent)
{
s32fp brkrampstr = Param::Get(Param::brkrampstr);
float brkrampstr = Param::GetFloat(Param::brkrampstr);
float rotorfreq = (float)frq / FRAC_FAC;
int direction = Param::GetInt(Param::dir);

if (frq < brkrampstr && torquePercent < 0)
if (rotorfreq < brkrampstr && torquePercent < 0)
{
torquePercent = FP_MUL(FP_DIV(frq, brkrampstr), torquePercent);
torquePercent = (rotorfreq / brkrampstr) * torquePercent;
}

if (torquePercent < 0)
{
direction = Encoder::GetRotorDirection();
}

int32_t is = FP_TOINT(FP_MUL(Param::Get(Param::throtcur), direction * torquePercent));
int32_t is = Param::GetFloat(Param::throtcur) * direction * torquePercent;
int32_t id, iq;

FOC::Mtpa(is, id, iq);
Expand Down
44 changes: 23 additions & 21 deletions src/pwmgeneration-sine.cpp
Expand Up @@ -81,61 +81,63 @@ void PwmGeneration::Run()
}
}

void PwmGeneration::SetTorquePercent(s32fp torque)
void PwmGeneration::SetTorquePercent(float torque)
{
s32fp fslipmin = Param::Get(Param::fslipmin);
s32fp ampmin = Param::Get(Param::ampmin);
s32fp slipstart = Param::Get(Param::slipstart);
s32fp ampnomLocal;
s32fp fslipspnt = 0;
const int filterConst = 4;
const float roundingError = ((float)((1 << filterConst) - 1)) / FRAC_FAC;
float fslipmin = Param::GetFloat(Param::fslipmin);
float ampmin = Param::GetFloat(Param::ampmin);
float slipstart = Param::GetFloat(Param::slipstart);
float ampnomLocal;
float fslipspnt = 0;

if (torque >= 0)
{
/* In async mode first X% throttle commands amplitude, X-100% raises slip */
ampnomLocal = ampmin + (100 - FP_TOINT(ampmin)) * FP_DIV(torque, slipstart);
ampnomLocal = ampmin + (100.0f - ampmin) * torque / slipstart;

if (torque >= slipstart)
{
s32fp fstat = Param::Get(Param::fstat);
s32fp fweak = Param::Get(Param::fweakcalc);
s32fp fslipmax = Param::Get(Param::fslipmax);
float fstat = Param::GetFloat(Param::fstat);
float fweak = Param::GetFloat(Param::fweakcalc);
float fslipmax = Param::GetFloat(Param::fslipmax);

if (fstat > fweak)
{
s32fp fconst = Param::Get(Param::fconst);
s32fp fslipconstmax = Param::Get(Param::fslipconstmax);
float fconst = Param::GetFloat(Param::fconst);
float fslipconstmax = Param::GetFloat(Param::fslipconstmax);
//Basically, for every Hz above fweak we get a fraction of
//the difference between fslipconstmax and fslipmax
//of additional slip
fslipmax += FP_MUL(FP_DIV(fstat - fweak, fconst - fweak), fslipconstmax - fslipmax);
fslipmax += (fstat - fweak) / (fconst - fweak) * (fslipconstmax - fslipmax);
fslipmax = MIN(fslipmax, fslipconstmax); //never exceed fslipconstmax!
}

s32fp fslipdiff = fslipmax - fslipmin;
fslipspnt = fslipmin + (FP_MUL(fslipdiff, (torque - slipstart)) / (100 - FP_TOINT(slipstart)));
float fslipdiff = fslipmax - fslipmin;
fslipspnt = roundingError + fslipmin + (fslipdiff * (torque - slipstart)) / (100.0f - slipstart);
}
else
{
fslipspnt = fslipmin;
fslipspnt = fslipmin + roundingError;
}
}
else
{
u32fp brkrampstr = (u32fp)Param::Get(Param::brkrampstr);
float brkrampstr = Param::GetFloat(Param::brkrampstr);

ampnomLocal = -torque;

fslipspnt = -fslipmin;
if (Encoder::GetRotorFrequency() < brkrampstr)
{
ampnomLocal = FP_TOINT(FP_DIV(Encoder::GetRotorFrequency(), brkrampstr) * ampnomLocal);
ampnomLocal = Encoder::GetRotorFrequency() / brkrampstr * ampnomLocal;
}
}

ampnomLocal = MIN(ampnomLocal, FP_FROMINT(100));
ampnomLocal = MIN(ampnomLocal, 100.0f);
//anticipate sudden changes by filtering
ampnom = IIRFILTER(ampnom, ampnomLocal, 4);
fslip = IIRFILTER(fslip, fslipspnt, 4);
ampnom = IIRFILTER(ampnom, FP_FROMFLT(ampnomLocal), filterConst);
fslip = IIRFILTER(fslip, FP_FROMFLT(fslipspnt), filterConst);
Param::Set(Param::ampnom, ampnom);
Param::Set(Param::fslipspnt, fslip);

Expand Down
2 changes: 1 addition & 1 deletion src/stm32_sine.cpp
Expand Up @@ -126,7 +126,7 @@ static void Ms10Task(void)

if (MOD_RUN == opmode && initWait == -1)
{
PwmGeneration::SetTorquePercent(FP_FROMFLT(torquePercent));
PwmGeneration::SetTorquePercent(torquePercent);
}
else if ((MOD_BOOST == opmode || MOD_BUCK == opmode) && initWait == -1)
{
Expand Down
51 changes: 29 additions & 22 deletions src/temp_meas.cpp
Expand Up @@ -23,13 +23,14 @@
#include <stdint.h>

#define TABLEN(a) sizeof(a) / sizeof(a[0])
#define FIRST_MOTOR_SENSOR TEMP_KTY83

enum coeff { PTC, NTC };

typedef struct TempSensor
{
int tempMin;
int tempMax;
int16_t tempMin;
int16_t tempMax;
uint8_t step;
uint8_t tabSize;
enum coeff coeff;
Expand Down Expand Up @@ -90,32 +91,36 @@ static const uint16_t NtcK45[] = { NTCK45 };
/* Temp sensor for outlander front motor (47k) in series with 1.2k with a 2.2k for R2 */
static const uint16_t OutlanderFront[] = { OUTLANDERFRONT };

/* EPCOS B57861-S 103-F40 temp sensor */
static const uint16_t epcosb57861[] = { EPCOSB57861 };

static const TEMP_SENSOR sensors[] =
{
{ -25, 105, 5, TABLEN(JCurve), NTC, JCurve },
{ 0, 100, 5, TABLEN(Semikron), PTC, Semikron },
{ -5, 100, 5, TABLEN(mbb600), PTC, mbb600 },
{ -50, 150, 10, TABLEN(Kty81hs), NTC, Kty81hs },
{ -50, 150, 10, TABLEN(Pt1000), PTC, Pt1000 },
{ -50, 150, 5, TABLEN(NtcK45), NTC, NtcK45 },
{ -10, 100, 10, TABLEN(leafhs), NTC, leafhs },
{ -25, 105, 5, TABLEN(fs800), PTC, fs800 },
{ -50, 170, 10, TABLEN(Kty83), PTC, Kty83 },
{ -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 },
{ -20, 190, 5, TABLEN(Tesla10k), PTC, Tesla10k },
{ -40, 300, 10, TABLEN(OutlanderFront), NTC, OutlanderFront },
{ -25, 105, 5, TABLEN(JCurve), NTC, JCurve },
{ 0, 100, 5, TABLEN(Semikron), PTC, Semikron },
{ -5, 100, 5, TABLEN(mbb600), PTC, mbb600 },
{ -50, 150, 10, TABLEN(Kty81hs), NTC, Kty81hs },
{ -50, 150, 10, TABLEN(Pt1000), PTC, Pt1000 },
{ -50, 150, 5, TABLEN(NtcK45), NTC, NtcK45 },
{ -10, 100, 10, TABLEN(leafhs), NTC, leafhs },
{ -25, 105, 5, TABLEN(fs800), PTC, fs800 },
{ -50, 170, 10, TABLEN(Kty83), PTC, Kty83 },
{ -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 },
{ -20, 190, 5, TABLEN(Tesla10k), PTC, Tesla10k },
{ -40, 300, 10, TABLEN(OutlanderFront), NTC, OutlanderFront },
{ -50, 150, 10, TABLEN(epcosb57861), NTC, epcosb57861},
};

float TempMeas::Lookup(int digit, Sensors sensorId)
{
if (sensorId >= TEMP_LAST) return 0;
int index = sensorId >= TEMP_KTY83 ? sensorId - TEMP_KTY83 + NUM_HS_SENSORS : sensorId;
int index = sensorId >= FIRST_MOTOR_SENSOR ? sensorId - FIRST_MOTOR_SENSOR + NUM_HS_SENSORS : sensorId;

const TEMP_SENSOR * sensor = &sensors[index];
uint16_t last;
Expand All @@ -129,7 +134,9 @@ float TempMeas::Lookup(int digit, Sensors sensorId)
if (0 == i) return sensor->tempMin;
float a = sensor->coeff == NTC?cur - digit:digit - cur;
float b = sensor->coeff == NTC?cur - last:last - cur;
return sensor->step * i + sensor->tempMin - sensor->step * a / b;
float c = sensor->step * a / b;
float d = (int)(sensor->step * i) + sensor->tempMin;
return d - c;
}
last = cur;
}
Expand Down
3 changes: 2 additions & 1 deletion src/throttle.cpp
Expand Up @@ -74,6 +74,7 @@ bool Throttle::CheckAndLimitRange(int* potval, int potIdx)
float Throttle::DigitsToPercent(int potval, int potidx)
{
if (potidx > 1) return 0;
if (potmax[potidx] == potmin[potidx]) return 100.0f;

return (100 * (potval - potmin[potidx])) / (potmax[potidx] - potmin[potidx]);
}
Expand All @@ -83,7 +84,7 @@ float Throttle::CalcThrottle(float potnom, float pot2nom, bool brkpedal)
float scaledBrkMax = brkpedal ? brknompedal : brkmax;

//Never reach 0, because that can spin up the motor
scaledBrkMax = -1 + (scaledBrkMax * pot2nom) / 100;
scaledBrkMax = -0.1 + (scaledBrkMax * pot2nom) / 100.0f;

if (brkpedal)
{
Expand Down

0 comments on commit 7c5fbae

Please sign in to comment.