diff --git a/can/can_client.c b/can/can_client.c index a33b7be..14289dc 100755 --- a/can/can_client.c +++ b/can/can_client.c @@ -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,24 +165,27 @@ void Can_SetMotorPower(int iMotorIndex, int iPower) /// @param iMotorIndex void Can_TransmitMotorGear(int iMotorIndex) { - // Transmission rate: 100ms - struct can_frame frame; - - frame.can_id = 0x18F005D0; - frame.can_id |= CAN_EFF_FLAG; - frame.can_dlc = 8; - frame.data[0] = motctrl[iMotorIndex].iMotorGear; - frame.data[1] = 0xFF; - frame.data[2] = 0xFF; - frame.data[3] = 0xFF; - frame.data[4] = 0xFF; - frame.data[5] = 0xFF; - frame.data[6] = 0xFF; - frame.data[7] = 0xFF; - - if (write(intf_data[iMotorIndex].socket, &frame, sizeof(frame)) != sizeof(frame)) + //if (motctrl[iMotorIndex].nSwitchState > 0) { + // Transmission rate: 100ms + struct can_frame frame; + frame.can_id = 0x18F005D0; + frame.can_id |= CAN_EFF_FLAG; + frame.can_dlc = 8; + frame.data[0] = motctrl[iMotorIndex].iMotorGear; + frame.data[1] = 0xFF; + frame.data[2] = 0xFF; + frame.data[3] = 0xFF; + frame.data[4] = 0xFF; + frame.data[5] = 0xFF; + frame.data[6] = 0xFF; + frame.data[7] = 0xFF; + + if (write(intf_data[iMotorIndex].socket, &frame, sizeof(frame)) != sizeof(frame)) + { + + } } } @@ -178,24 +194,27 @@ void Can_TransmitMotorGear(int iMotorIndex) /// @param iMotorIndex void Can_TransmitMotorPower(int iMotorIndex) { - // Transmission rate: 50ms - struct can_frame frame; - - frame.can_id = 0x0CF003D0; - 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[2] = 0xFF; - frame.data[3] = 0xFF; - frame.data[4] = 0xFF; - frame.data[5] = 0xFF; - frame.data[6] = 0xFF; - frame.data[7] = 0xFF; - - if (write(intf_data[iMotorIndex].socket, &frame, sizeof(frame)) != sizeof(frame)) + //if (motctrl[iMotorIndex].nSwitchState > 0) { + // Transmission rate: 50ms + struct can_frame frame; + frame.can_id = 0x0CF003D0; + 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[2] = 0xFF; + frame.data[3] = 0xFF; + frame.data[4] = 0xFF; + frame.data[5] = 0xFF; + frame.data[6] = 0xFF; + frame.data[7] = 0xFF; + + if (write(intf_data[iMotorIndex].socket, &frame, sizeof(frame)) != sizeof(frame)) + { + + } } } @@ -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); } diff --git a/can/can_client.h b/can/can_client.h index 5f237b3..c324312 100755 --- a/can/can_client.h +++ b/can/can_client.h @@ -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 { diff --git a/main.c b/main.c index c8fa9ee..032be45 100755 --- a/main.c +++ b/main.c @@ -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(); diff --git a/mqtt/mqtt_client.c b/mqtt/mqtt_client.c index 30d1655..eae3893 100755 --- a/mqtt/mqtt_client.c +++ b/mqtt/mqtt_client.c @@ -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); } } diff --git a/scripts/CanRtDriver.service b/scripts/CanRtDriver.service index db7928b..e02f19e 100755 --- a/scripts/CanRtDriver.service +++ b/scripts/CanRtDriver.service @@ -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! diff --git a/scripts/can_link_up.sh b/scripts/can_link_up.sh index 1c20b14..42390be 100755 --- a/scripts/can_link_up.sh +++ b/scripts/can_link_up.sh @@ -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