Skip to content

Commit

Permalink
Display rotor direction and selected direction separately
Browse files Browse the repository at this point in the history
Allow switching from neutral into rotor direction
Removed dodgy speed frequency output, lets wait if anyone complains
Made CRC check of control message optional
  • Loading branch information
jsphuebner committed Sep 14, 2023
1 parent 84138fa commit e1ea274
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 38 deletions.
5 changes: 3 additions & 2 deletions include/param_prj.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
3. Display values
*/
//Next param id (increase when adding new parameter!): 158
//Next value Id: 2052
//Next value Id: 2053
/* category name unit min max default id */

#define MOTOR_PARAMETERS_COMMON \
Expand Down Expand Up @@ -179,7 +179,8 @@
VALUE_ENTRY(pot2, "dig", 2016 ) \
VALUE_ENTRY(regenpreset, "%", 2051 ) \
VALUE_ENTRY(potnom, "%", 2017 ) \
VALUE_ENTRY(dir, DIRS, 2018 ) \
VALUE_ENTRY(seldir, DIRS, 2018 ) \
VALUE_ENTRY(rotordir, DIRS, 2053 ) \
VALUE_ENTRY(tmphs, "°C", 2019 ) \
VALUE_ENTRY(tmpm, "°C", 2020 ) \
VALUE_ENTRY(uaux, "V", 2021 ) \
Expand Down
2 changes: 1 addition & 1 deletion libopeninv
Submodule libopeninv updated 1 files
+3 −3 include/cansdo.h
2 changes: 1 addition & 1 deletion src/pwmgeneration-foc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void PwmGeneration::Run()
{
static s32fp idcFiltered = 0;
static int amplitudeErrFiltered;
int dir = Param::GetInt(Param::dir);
int dir = Param::GetInt(Param::seldir);
s32fp id, iq;

Encoder::UpdateRotorAngle(0);
Expand Down
4 changes: 2 additions & 2 deletions src/pwmgeneration-sine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void PwmGeneration::Run()
{
if (opmode == MOD_MANUAL || opmode == MOD_RUN || opmode == MOD_SINE)
{
int dir = Param::GetInt(Param::dir);
int dir = Param::GetInt(Param::seldir);

Encoder::UpdateRotorAngle(dir);
s32fp ampNomLimited = LimitCurrent();
Expand Down Expand Up @@ -134,7 +134,7 @@ void PwmGeneration::SetTorquePercent(float torque)
}
}
}
else if (Encoder::GetRotorDirection() != Param::GetInt(Param::dir))
else if (Encoder::GetRotorDirection() != Param::GetInt(Param::seldir))
{
// Do not apply negative torque if we are already traveling backwards.
fslipspnt = 0;
Expand Down
21 changes: 1 addition & 20 deletions src/stm32_sine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ static void Ms10Task(void)
VehicleControl::GetDigInputs();
float torquePercent = VehicleControl::ProcessThrottle();
Param::SetInt(Param::speed, Encoder::GetSpeed());
Param::SetInt(Param::rotordir, Encoder::GetRotorDirection());

if (MOD_RUN == opmode && initWait == -1)
{
Expand Down Expand Up @@ -248,25 +249,6 @@ static void Ms10Task(void)
canMap->SendAll();
}

static void Ms1Task(void)
{
static int speedCnt = 0;

if (Param::GetInt(Param::pwmfunc) == PWM_FUNC_SPEEDFRQ)
{
int speed = Param::GetInt(Param::speed);
if (speedCnt == 0 && speed != 0)
{
DigIo::speed_out.Toggle();
speedCnt = Param::GetInt(Param::pwmgain) / (2 * speed);
}
else if (speedCnt > 0)
{
speedCnt--;
}
}
}

/** This function is called when the user changes a parameter */
void Param::Change(Param::PARAM_NUM paramNum)
{
Expand Down Expand Up @@ -413,7 +395,6 @@ extern "C" int main(void)

s.AddTask(Ms100Task, 100);
s.AddTask(Ms10Task, 10);
s.AddTask(Ms1Task, 1);

DigIo::prec_out.Set();

Expand Down
30 changes: 18 additions & 12 deletions src/vehiclecontrol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,16 @@ bool VehicleControl::CanReceive(uint32_t canId, uint32_t data[2])
uint8_t ctr2 = (data[1] >> 14) & 0x3;
uint8_t regenpreset = (data[1] >> 16) & 0xFF;
uint8_t crc = (data[1] >> 24) & 0xFF;
uint8_t calcCrc = crc;

//Zero out CRC byte
data[1] &= 0x00FFFFFF;
crc_reset();
uint32_t calcCrc = crc_calculate_block(data, 2) & 0xFF;
//Optional CRC check
if (Param::GetBool(Param::controlcheck))
{
//Zero out CRC byte
data[1] &= 0x00FFFFFF;
crc_reset();
calcCrc = crc_calculate_block(data, 2) & 0xFF;
}

//We check for CRC and sequence counter errors. As long as we stay below maxErrors
//we can heal bad frames with good frames. Once we've surpassed maxErrors we do
Expand Down Expand Up @@ -206,7 +211,8 @@ void VehicleControl::CruiseControl()

void VehicleControl::SelectDirection()
{
int selectedDir = Param::GetInt(Param::dir);
int selectedDir = Param::GetInt(Param::seldir);
int rotorDir = Param::GetInt(Param::rotordir);
int userDirSelection = 0;
int dirSign = (Param::GetInt(Param::dirmode) & DIR_REVERSED) ? -1 : 1;
bool potPressed = Param::GetInt(Param::potnom) > 0;
Expand Down Expand Up @@ -247,10 +253,10 @@ void VehicleControl::SelectDirection()
}

/* Only change direction when below certain motor speed and throttle is not pressed */
if ((int)Encoder::GetSpeed() < Param::GetInt(Param::dirchrpm) && !potPressed)
if (((int)Encoder::GetSpeed() < Param::GetInt(Param::dirchrpm) || userDirSelection == rotorDir) && !potPressed)
selectedDir = userDirSelection;

Param::SetInt(Param::dir, selectedDir);
Param::SetInt(Param::seldir, selectedDir);
}

float VehicleControl::ProcessThrottle()
Expand Down Expand Up @@ -313,7 +319,7 @@ float VehicleControl::ProcessThrottle()
if (finalSpnt < 0)
finalSpnt *= Encoder::GetRotorDirection();
else //inconsistency here: in slip control negative always means regen
finalSpnt *= Param::GetInt(Param::dir);
finalSpnt *= Param::GetInt(Param::seldir);

//At 110% fmax start derating field weakening current just in case it has a torque producing component
Throttle::fmax = Param::GetFloat(Param::fmax) * 1.1f;
Expand All @@ -324,7 +330,7 @@ float VehicleControl::ProcessThrottle()
}

//Make sure we never command torque in neutral
if (Param::GetInt(Param::dir) == 0)
if (Param::GetInt(Param::seldir) == 0)
finalSpnt = 0;

return finalSpnt;
Expand Down Expand Up @@ -683,16 +689,16 @@ float VehicleControl::GetUserThrottleCommand()
if (bidirThrot == 0 || (requestedDirection != rotorDirection && speed > 30))
{
bidirThrot = Throttle::brkmax;
Param::SetInt(Param::dir, rotorDirection);
Param::SetInt(Param::seldir, rotorDirection);
}
else if (bidirThrot < 0)
{
bidirThrot = -bidirThrot;
Param::SetInt(Param::dir, -1);
Param::SetInt(Param::seldir, -1);
}
else //bidirThrot > 0
{
Param::SetInt(Param::dir, 1);
Param::SetInt(Param::seldir, 1);
}
return bidirThrot;
}
Expand Down

0 comments on commit e1ea274

Please sign in to comment.