Optimierung Systemhochlauf

This commit is contained in:
Bernhard
2025-12-12 18:50:18 +01:00
parent fbff41de71
commit bd6e3bbfe9
6 changed files with 69 additions and 42 deletions

View File

@@ -6,6 +6,19 @@
struct MOTOR_CONTROL_DATA motctrl[MOTOR_COUNT];
struct CAN_INTERFACE_DATA intf_data[MOTOR_COUNT];
int iBusTimeoutCounter = 0;
void IncBusTimeoutCounter(int iMotorIndex)
{
if (iBusTimeoutCounter < 2000)
{
iBusTimeoutCounter++;
if (iBusTimeoutCounter >= 2000)
{
motctrl[iMotorIndex].nSwitchState = 0;
MqttClient_Publish_MotorSwitchState(iMotorIndex, motctrl[iMotorIndex].nSwitchState);
}
}
}
/// @brief Open socket of CAN interface for the given motor
/// @param iMotorIndex
/// @param ifacename
@@ -152,6 +165,8 @@ void Can_SetMotorPower(int iMotorIndex, int iPower)
/// @param iMotorIndex
void Can_TransmitMotorGear(int iMotorIndex)
{
//if (motctrl[iMotorIndex].nSwitchState > 0)
{
// Transmission rate: 100ms
struct can_frame frame;
@@ -171,6 +186,7 @@ void Can_TransmitMotorGear(int iMotorIndex)
{
}
}
}
@@ -178,6 +194,8 @@ void Can_TransmitMotorGear(int iMotorIndex)
/// @param iMotorIndex
void Can_TransmitMotorPower(int iMotorIndex)
{
//if (motctrl[iMotorIndex].nSwitchState > 0)
{
// Transmission rate: 50ms
struct can_frame frame;
@@ -197,6 +215,7 @@ void Can_TransmitMotorPower(int iMotorIndex)
{
}
}
}
@@ -207,6 +226,8 @@ void Can_ReadData(int iMotorIndex)
ssize_t nbytes = 0;
struct can_frame frame;
IncBusTimeoutCounter(iMotorIndex);
// wir wollen immer alle verfügbaren Frames lesen
while ((nbytes = read(intf_data[iMotorIndex].socket, &frame, sizeof(frame))) > 0)
{
@@ -239,7 +260,8 @@ void Can_ReadData(int iMotorIndex)
/// @param frame
void Can_Read_Manu_PGN(int iMotorIndex, struct can_frame *frame)
{
motctrl[iMotorIndex].nSwitchState = frame->data[2];
iBusTimeoutCounter = 0;
motctrl[iMotorIndex].nSwitchState = frame->data[4];
MqttClient_Publish_MotorSwitchState(iMotorIndex, motctrl[iMotorIndex].nSwitchState);
}
@@ -247,6 +269,6 @@ void Can_Read_Manu_PGN(int iMotorIndex, struct can_frame *frame)
/// @param frame
void Can_Read_Manu_PGN2(int iMotorIndex, struct can_frame *frame)
{
motctrl[iMotorIndex].iActualMotorPowerW = (frame->data[3] << 16) | (frame->data[4] << 8) | frame->data[5];
motctrl[iMotorIndex].iActualMotorPowerW = (frame->data[6] << 16) | (frame->data[5] << 8) | frame->data[4];
MqttClient_Publish_MotorActualPowerW(iMotorIndex, motctrl[iMotorIndex].iActualMotorPowerW);
}

View File

@@ -21,9 +21,9 @@
#define MOTOR_GEAR_NEUTRAL 0x7D
#define MOTOR_GEAR_FORWARD 0x7E
#define MOTOR_PWR_MIN_PCT 5
#define MOTOR_PWR_MIN_PCT 15
#define MOTOR_PWR_MAX_PCT 100
#define MOTOR_PWR_STEP 14
#define MOTOR_PWR_STEP 12
struct MOTOR_CONTROL_DATA
{

4
main.c
View File

@@ -130,7 +130,7 @@ void *thread_func(void *data)
periodic_task_init(1, &pinfo);
// "Zündung" ein
WriteOutputPin(GPIO_OUT_PWRON, 1);
WriteOutputPin(GPIO_OUT_PWRON, HIGH);
// cyclic call of do_cyclic_1ms()
while (iThreadControl == 0)
@@ -146,7 +146,7 @@ void *thread_func(void *data)
}
// "Zündung" aus
WriteOutputPin(GPIO_OUT_PWRON, 0);
WriteOutputPin(GPIO_OUT_PWRON, LOW);
// Disconnect from mqtt broker
MqttClient_Close();

View File

@@ -297,7 +297,7 @@ void MqttClient_Publish_MotorSwitchState(int iMotorIndex, unsigned char nSwitchS
{
nMqttMotor1SwitchState = nSwitchState;
char message[100];
snprintf(message, sizeof(message), "%d", nSwitchState);
snprintf(message, sizeof(message), "%2X", nSwitchState);
mosquitto_publish(mosq, NULL, mqtt_topic_motor1_switchstate, strlen(message), message, 0, false);
}
}
@@ -307,7 +307,7 @@ void MqttClient_Publish_MotorSwitchState(int iMotorIndex, unsigned char nSwitchS
{
nMqttMotor2SwitchState = nSwitchState;
char message[100];
snprintf(message, sizeof(message), "%d", nSwitchState);
snprintf(message, sizeof(message), "%2X", nSwitchState);
mosquitto_publish(mosq, NULL, mqtt_topic_motor2_switchstate, strlen(message), message, 0, false);
}
}

View File

@@ -2,8 +2,9 @@
## Datei gehört in /etc/systemd/system
## aktivieren dann mit:
## - sudo systemctl daemon-reload
## - sudo systemctl enable CanRtDriver.service
## - sudo systemctl start CanRtDriver.service
## - sudo systemctl enable CanRtDriver.service NICHT AUSFÜHREN!!!
## - sudo systemctl start CanRtDriver.service NICHT AUSFÜHREN!!!
## => Service wird über ./scripts/can_link_up.sh gestartet
##
## Device Unit ermitteln mit: systemctl --type=device | grep can0
## Ggf. unter 'Requires', 'After' und 'BindsTo' anpassen!

View File

@@ -2,6 +2,10 @@
ip link set can0 up type can bitrate 250000
ip link set can1 up type can bitrate 250000
sleep 5
ifconfig can0 txqueuelen 65536
ifconfig can1 txqueuelen 65536
sleep 10
systemctl start CanRtDriver