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;
}
MqttClient_Publish_MotorPower(iMotorIndex, motctrl[iMotorIndex].iMotorPowerPct);
motctrl[iMotorIndex].iMotorPowerSteps = iPower;
}
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;