Skip to content

Commit

Permalink
Support pin swap (only PWM channels) in sine firmware also
Browse files Browse the repository at this point in the history
  • Loading branch information
jsphuebner committed Apr 2, 2024
1 parent 36b8689 commit 730dfa3
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 42 deletions.
6 changes: 3 additions & 3 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 5.32.R
#define VER 5.33.R

/* Entries should be ordered as follows:
1. Saveable parameters
Expand Down Expand Up @@ -71,10 +71,10 @@
PARAM_ENTRY(CAT_INVERTER,udcgain, "dig/V", 0, 4095, 6.175, 29 ) \
PARAM_ENTRY(CAT_INVERTER,udcofs, "dig", 0, 4095, 0, 77 ) \
PARAM_ENTRY(CAT_INVERTER,udclim, "V", 0, 1000, 540, 48 ) \
PARAM_ENTRY(CAT_INVERTER,snshs, SNS_HS, 0, 7, 0, 45 )
PARAM_ENTRY(CAT_INVERTER,snshs, SNS_HS, 0, 7, 0, 45 ) \
PARAM_ENTRY(CAT_INVERTER,pinswap, SWAPS, 0, 15, 0, 109 ) \

#define INVERTER_PARAMETERS_FOC \
PARAM_ENTRY(CAT_INVERTER,pinswap, SWAPS, 0, 15, 0, 109 ) \
PARAM_ENTRY(CAT_INVERTER,modmax, "dig", 37000, 45000, 37836, 148 )

#define DERATE_PARAMETERS_COMMON \
Expand Down
2 changes: 2 additions & 0 deletions include/pwmgeneration.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define PWMGENERATION_H

#include <stdint.h>
#include <libopencm3/stm32/timer.h>
#include "my_fp.h"
#include "anain.h"

Expand Down Expand Up @@ -74,6 +75,7 @@ class PwmGeneration
static int opmode;
static s32fp ilofs[2];
static int polePairRatio;
static tim_oc_id ocChannels[3];
};

#endif // PWMGENERATION_H
2 changes: 1 addition & 1 deletion include/throttle.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
class Throttle
{
public:
static bool CheckAndLimitRange(int* potval, int potIdx);
static bool CheckAndLimitRange(int* potval, uint8_t potIdx);
static float DigitsToPercent(int potval, int potidx);
static float CalcThrottle(float potval, float pot2val, bool brkpedal, float rotorHz);
static float CalcThrottleBiDir(float potval, bool brkpedal);
Expand Down
20 changes: 0 additions & 20 deletions src/pwmgeneration-foc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ static const s32fp dcCurFac = FP_FROMFLT(0.81649658092772603273 * 1.05); //sqrt(
static const int32_t fwOutMax = 1024;
static const uint32_t shiftForFilter = 8;
static s32fp idMtpa = 0, iqMtpa = 0;
static tim_oc_id ocChannels[3];
static PiController qController;
static PiController dController;
static s32fp fwCurMax = 0;
Expand Down Expand Up @@ -186,25 +185,6 @@ void PwmGeneration::PwmInit()
dController.SetCallingFrequency(pwmfrq);
dController.SetMinMaxY(-maxVd, maxVd);

if ((Param::GetInt(Param::pinswap) & SWAP_PWM13) > 0)
{
ocChannels[0] = TIM_OC3;
ocChannels[1] = TIM_OC2;
ocChannels[2] = TIM_OC1;
}
else if ((Param::GetInt(Param::pinswap) & SWAP_PWM23) > 0)
{
ocChannels[0] = TIM_OC1;
ocChannels[1] = TIM_OC3;
ocChannels[2] = TIM_OC2;
}
else
{
ocChannels[0] = TIM_OC1;
ocChannels[1] = TIM_OC2;
ocChannels[2] = TIM_OC3;
}

if (opmode == MOD_ACHEAT)
AcHeatTimerSetup();
}
Expand Down
7 changes: 4 additions & 3 deletions src/pwmgeneration-sine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ void PwmGeneration::Run()
}

/* Match to PWM resolution */
timer_set_oc_value(PWM_TIMER, TIM_OC1, SineCore::DutyCycles[0] >> shiftForTimer);
timer_set_oc_value(PWM_TIMER, TIM_OC2, SineCore::DutyCycles[1] >> shiftForTimer);
timer_set_oc_value(PWM_TIMER, TIM_OC3, SineCore::DutyCycles[2] >> shiftForTimer);
for (int i = 0; i < 3; i++)
{
timer_set_oc_value(PWM_TIMER, ocChannels[i], SineCore::DutyCycles[i] >> shiftForTimer);
}
}
else if (opmode == MOD_BOOST || opmode == MOD_BUCK)
{
Expand Down
42 changes: 31 additions & 11 deletions src/pwmgeneration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,18 @@
#define DIGIT_TO_DEGREE(a) FP_FROMINT(angle) / (65536 / 360)
#define FRQ_DIVIDER 8192 //PWM ISR callback frequency divider

uint16_t PwmGeneration::pwmfrq = 1;
uint16_t PwmGeneration::angle;
s32fp PwmGeneration::ampnom;
s32fp PwmGeneration::fslip;
s32fp PwmGeneration::frq;
s32fp PwmGeneration::frqFiltered;
uint8_t PwmGeneration::shiftForTimer;
int PwmGeneration::opmode;
s32fp PwmGeneration::ilofs[2];
int PwmGeneration::polePairRatio;
int16_t PwmGeneration::slipIncr;
uint16_t PwmGeneration::pwmfrq = 1;
uint16_t PwmGeneration::angle;
s32fp PwmGeneration::ampnom;
s32fp PwmGeneration::fslip;
s32fp PwmGeneration::frq;
s32fp PwmGeneration::frqFiltered;
uint8_t PwmGeneration::shiftForTimer;
int PwmGeneration::opmode;
s32fp PwmGeneration::ilofs[2];
int PwmGeneration::polePairRatio;
int16_t PwmGeneration::slipIncr;
tim_oc_id PwmGeneration::ocChannels[3];

static int execTicks;
static bool tripped;
Expand Down Expand Up @@ -370,6 +371,25 @@ uint16_t PwmGeneration::TimerSetup(uint16_t deadtime, bool activeLow)
const uint16_t pwmmax = 1U << pwmdigits;
const uint8_t outputMode = activeLow ? GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN : GPIO_CNF_OUTPUT_ALTFN_PUSHPULL;

if ((Param::GetInt(Param::pinswap) & SWAP_PWM13) > 0)
{
ocChannels[0] = TIM_OC3;
ocChannels[1] = TIM_OC2;
ocChannels[2] = TIM_OC1;
}
else if ((Param::GetInt(Param::pinswap) & SWAP_PWM23) > 0)
{
ocChannels[0] = TIM_OC1;
ocChannels[1] = TIM_OC3;
ocChannels[2] = TIM_OC2;
}
else
{
ocChannels[0] = TIM_OC1;
ocChannels[1] = TIM_OC2;
ocChannels[2] = TIM_OC3;
}

//Disable output in active low mode before resetting timer, otherwise shoot through will occur!
if (activeLow)
{
Expand Down
8 changes: 4 additions & 4 deletions src/throttle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ int Throttle::accelflt;
float Throttle::maxregentravelhz;
float Throttle::regenrampstr;

bool Throttle::CheckAndLimitRange(int* potval, int potIdx)
bool Throttle::CheckAndLimitRange(int* potval, uint8_t potIdx)
{
int potMin = potmax[potIdx] > potmin[potIdx] ? potmin[potIdx] : potmax[potIdx];
int potMax = potmax[potIdx] > potmin[potIdx] ? potmax[potIdx] : potmin[potIdx];
Expand Down Expand Up @@ -87,10 +87,10 @@ float Throttle::DigitsToPercent(int potval, int potidx)


/**
* Calculate throttle and regen.
* Calculate throttle and regen.
* - Increase regentravel when lifting accelerator using historical pedal values.
* - Decrease regentravel when speed goes below regenrampstr.
*
*
* Parameters:
* potnom = accelerator pedal pressed percentage
* pot2nom = optional potentiometer for adjusting regen braking
Expand Down Expand Up @@ -134,7 +134,7 @@ float Throttle::CalcThrottle(float potnom, float pot2nom, bool brkpedal, float r
}

// increase currentRegenTravel when lifting accelerator
if (currentRegentravel < speedBasedRegenTravel) // if currentregentravel is smaller than speedBaseRegenTravel
if (currentRegentravel < speedBasedRegenTravel) // if currentregentravel is smaller than speedBaseRegenTravel
{
// current pedal position delta compared against average of last 200ms
float acceleratorDelta = potnom - potnomSums / history;
Expand Down

0 comments on commit 730dfa3

Please sign in to comment.