Leistungsvorgabe jetzt in Schritten und nicht mehr in Prozent

This commit is contained in:
Bernhard
2026-01-17 19:14:44 +01:00
parent c16ac482bd
commit 1b265c71f0
4 changed files with 35 additions and 27 deletions

View File

@@ -41,11 +41,11 @@ int Can_OpenInterface(int iMotorIndex, const char * ifacename)
motctrl[iMotorIndex].nDriveReady = 0;
motctrl[iMotorIndex].iActualMotorPowerW = 0;
motctrl[iMotorIndex].iMotorGear = MOTOR_GEAR_NEUTRAL;
motctrl[iMotorIndex].iMotorPower = 0;
motctrl[iMotorIndex].iMotorPowerPct = 0;
motctrl[iMotorIndex].iMotorPowerRaw = 0;
motctrl[iMotorIndex].iMotorPowerSteps = 0;
motctrl[iMotorIndex].nSwitchState = 0;
mylog(LOG_INFO, "CAN: PWR_MIN_PCT=%d PWR_MAX_PCT=%d PWR_STEP=%d", MOTOR_PWR_MIN_PCT, MOTOR_PWR_MAX_PCT, MOTOR_PWR_STEP);
mylog(LOG_INFO, "CAN: PWR_MAX_RAW=%d PWR_STEP_COUNT=%d", MOTOR_PWR_MAX_RAW, MOTOR_PWR_STEP_COUNT);
strcpy(intf_data[iMotorIndex].iface_name, ifacename);
Can_SetMotorGear(iMotorIndex, MOTOR_GEAR_NEUTRAL);
@@ -123,7 +123,7 @@ void Can_SetMotorGear(int iMotorIndex, int iGear)
MqttClient_Publish_MotorGear(iMotorIndex, iGear);
motctrl[iMotorIndex].iMotorGear = MOTOR_GEAR_FORWARD;
// motor is switched to forward -> set min. power
Can_SetMotorPower(iMotorIndex, MOTOR_PWR_MIN_PCT);
Can_SetMotorPower(iMotorIndex, 1);
}
WriteOutputPin(GPIO_LED_MOTRUN, HIGH);
mylog(LOG_INFO, "CAN: Motor[%d]: Set gear forward.", iMotorIndex);
@@ -135,7 +135,7 @@ void Can_SetMotorGear(int iMotorIndex, int iGear)
MqttClient_Publish_MotorGear(iMotorIndex, iGear);
motctrl[iMotorIndex].iMotorGear = MOTOR_GEAR_REVERSE;
// motor is switched to reverse -> set min. power
Can_SetMotorPower(iMotorIndex, MOTOR_PWR_MIN_PCT);
Can_SetMotorPower(iMotorIndex, 1);
}
WriteOutputPin(GPIO_LED_MOTRUN, HIGH);
mylog(LOG_INFO, "CAN: Motor[%d]: Set gear reverse.", iMotorIndex);
@@ -157,35 +157,42 @@ void Can_SetMotorGear(int iMotorIndex, int iGear)
/// @brief Set power for the given motor
/// @param iMotorIndex
/// @param iPower (Range: 0..100)
/// @param iPower (Range: 0..MOTOR_PWR_STEP_COUNT)
void Can_SetMotorPower(int iMotorIndex, int iPower)
{
if ((motctrl[iMotorIndex].iMotorGear == MOTOR_GEAR_NEUTRAL) || (motctrl[iMotorIndex].nDriveReady == 0))
{
// when motor is neutral or not ready set power to 0
motctrl[iMotorIndex].iMotorPowerPct = 0;
motctrl[iMotorIndex].iMotorPowerSteps = 0;
}
else if (iPower <= MOTOR_PWR_MIN_PCT)
else if (iPower <= 1)
{
// limit to min. power
motctrl[iMotorIndex].iMotorPowerPct = MOTOR_PWR_MIN_PCT;
motctrl[iMotorIndex].iMotorPowerSteps = 1;
}
else if (iPower >= MOTOR_PWR_MAX_PCT)
else if (iPower >= MOTOR_PWR_STEP_COUNT)
{
// limit to max. power
motctrl[iMotorIndex].iMotorPowerPct = MOTOR_PWR_MAX_PCT;
motctrl[iMotorIndex].iMotorPowerSteps = MOTOR_PWR_STEP_COUNT;
}
else
{
motctrl[iMotorIndex].iMotorPowerPct = iPower;
motctrl[iMotorIndex].iMotorPowerSteps = iPower;
}
MqttClient_Publish_MotorPower(iMotorIndex, motctrl[iMotorIndex].iMotorPowerPct);
MqttClient_Publish_MotorPower(iMotorIndex, motctrl[iMotorIndex].iMotorPowerSteps);
// calc value for telegram
motctrl[iMotorIndex].iMotorPower = 250 * motctrl[iMotorIndex].iMotorPowerPct / 100;
if (motctrl[iMotorIndex].iMotorPowerSteps <= 0)
{
motctrl[iMotorIndex].iMotorPowerRaw = 0;
}
else
{
motctrl[iMotorIndex].iMotorPowerRaw = (((MOTOR_PWR_MAX_RAW - MOTOR_PWR_MIN_RAW) * motctrl[iMotorIndex].iMotorPowerSteps) / MOTOR_PWR_STEP_COUNT) + MOTOR_PWR_MIN_RAW;
}
mylog(LOG_INFO, "CAN: Motor[%d]: Set power to %d%% -> %d",
iMotorIndex, motctrl[iMotorIndex].iMotorPowerPct, motctrl[iMotorIndex].iMotorPower);
mylog(LOG_INFO, "CAN: Motor[%d]: Set power to %d -> %d",
iMotorIndex, motctrl[iMotorIndex].iMotorPowerSteps, motctrl[iMotorIndex].iMotorPowerRaw);
}
@@ -226,7 +233,7 @@ void Can_TransmitMotorPower(int iMotorIndex)
frame.can_id |= CAN_EFF_FLAG;
frame.can_dlc = 8;
frame.data[0] = 0xFF;
frame.data[1] = motctrl[iMotorIndex].iMotorPower; // motor power 0 = 0%, 250 = 100%
frame.data[1] = motctrl[iMotorIndex].iMotorPowerRaw; // motor power 0 = 0%, 250 = 100%
frame.data[2] = 0xFF;
frame.data[3] = 0xFF;
frame.data[4] = 0xFF;

View File

@@ -22,17 +22,17 @@
#define MOTOR_GEAR_NEUTRAL 0x7D
#define MOTOR_GEAR_FORWARD 0x7E
#define MOTOR_PWR_MIN_PCT 15
#define MOTOR_PWR_MAX_PCT 100
#define MOTOR_PWR_STEP ((MOTOR_PWR_MAX_PCT - MOTOR_PWR_MIN_PCT) / 7)
#define MOTOR_PWR_STEP_COUNT 7 // how many steps to set power
#define MOTOR_PWR_MIN_RAW 38
#define MOTOR_PWR_MAX_RAW 250 // max. raw power value for motor
struct MOTOR_CONTROL_DATA
{
char nDriveConnected;
char nDriveReady;
int iMotorGear;
int iMotorPower;
int iMotorPowerPct;
int iMotorPowerRaw;
int iMotorPowerSteps;
unsigned char nSwitchState;
int iActualMotorPowerW;
};

View File

@@ -197,22 +197,22 @@ void IO_DoCyclic()
{
mylog(LOG_INFO, "IO: KEY-Plus: Start motor.");
Can_SetMotorGear(0, 1);
Can_SetMotorPower(0, MOTOR_PWR_MIN_PCT);
Can_SetMotorPower(0, 1);
}
else
{
mylog(LOG_INFO, "IO: KEY-Plus: Increase power.");
Can_SetMotorPower(0, motctrl[0].iMotorPowerPct + MOTOR_PWR_STEP);
Can_SetMotorPower(0, motctrl[0].iMotorPowerSteps + 1);
}
}
if (gpioKeyPwrDown.iKeyRisingEdge)
{
// minus key is pressed -> decrease power
if (motctrl[0].iMotorPowerPct > MOTOR_PWR_MIN_PCT)
if (motctrl[0].iMotorPowerSteps > 1)
{
mylog(LOG_INFO, "IO: KEY-Minus: Decrease power.");
Can_SetMotorPower(0, motctrl[0].iMotorPowerPct - MOTOR_PWR_STEP);
Can_SetMotorPower(0, motctrl[0].iMotorPowerSteps - 1);
}
else
{

View File

@@ -1,4 +1,5 @@
#!/bin/bash
git fetch
git pull