Erste Vorbereitungen für Settings-Datei

This commit is contained in:
Bernhard Schräer
2026-01-20 07:33:02 +01:00
parent f91aee06c7
commit b5f7b5a45d
7 changed files with 87 additions and 32 deletions

View File

@@ -1,14 +1,15 @@
#include "main.h" #include "main.h"
#include <settings/settings.h>
#include <can/can_client.h> #include <can/can_client.h>
#include <io/io.h> #include <io/io.h>
#include <mqtt/mqtt_client.h> #include <mqtt/mqtt_client.h>
int iCanSimu = 0; int iCanSimu = 0;
struct MOTOR_CONTROL_DATA motctrl[MOTOR_COUNT]; struct MOTOR_CONTROL_DATA motctrl[MAX_MOTOR_COUNT];
struct CAN_INTERFACE_DATA intf_data[MOTOR_COUNT]; struct CAN_INTERFACE_DATA intf_data[MAX_MOTOR_COUNT];
int iBusTimeoutCounter = 0; int iBusTimeoutCounter = 0;
@@ -47,7 +48,8 @@ int Can_OpenInterface(int iMotorIndex, const char * ifacename)
motctrl[iMotorIndex].nDriveConnected = 0; motctrl[iMotorIndex].nDriveConnected = 0;
motctrl[iMotorIndex].nDriveReady = 0; motctrl[iMotorIndex].nDriveReady = 0;
mylog(LOG_INFO, "CAN: PWR_MAX_RAW=%d PWR_STEP_COUNT=%d", MOTOR_PWR_MAX_RAW, MOTOR_PWR_STEP_COUNT); mylog(LOG_INFO, "CAN: PWR_MIN_RAW=%d PWR_MAX_RAW=%d PWR_STEP_COUNT=%d",
settings.iMotorPwrMinRaw, settings.iMotorPwrMaxRaw, settings.iMotorPwrStepCount);
strcpy(intf_data[iMotorIndex].iface_name, ifacename); strcpy(intf_data[iMotorIndex].iface_name, ifacename);
Can_SetMotorGear(iMotorIndex, MOTOR_GEAR_NEUTRAL); Can_SetMotorGear(iMotorIndex, MOTOR_GEAR_NEUTRAL);
@@ -177,7 +179,7 @@ void Can_SetMotorGear(int iMotorIndex, int iGear)
/// @brief Set power for the given motor /// @brief Set power for the given motor
/// @param iMotorIndex /// @param iMotorIndex
/// @param iPower (Range: 0..MOTOR_PWR_STEP_COUNT) /// @param iPower (Range: 0..settings.iMotorPwrStepCount)
void Can_SetMotorPower(int iMotorIndex, int iPower) void Can_SetMotorPower(int iMotorIndex, int iPower)
{ {
if ((motctrl[iMotorIndex].iMotorGear == MOTOR_GEAR_NEUTRAL) || (motctrl[iMotorIndex].nDriveReady == 0)) if ((motctrl[iMotorIndex].iMotorGear == MOTOR_GEAR_NEUTRAL) || (motctrl[iMotorIndex].nDriveReady == 0))
@@ -190,10 +192,10 @@ void Can_SetMotorPower(int iMotorIndex, int iPower)
// limit to min. power // limit to min. power
motctrl[iMotorIndex].iMotorPowerSteps = 1; motctrl[iMotorIndex].iMotorPowerSteps = 1;
} }
else if (iPower >= MOTOR_PWR_STEP_COUNT) else if (iPower >= settings.iMotorPwrStepCount)
{ {
// limit to max. power // limit to max. power
motctrl[iMotorIndex].iMotorPowerSteps = MOTOR_PWR_STEP_COUNT; motctrl[iMotorIndex].iMotorPowerSteps = settings.iMotorPwrStepCount;
} }
else else
{ {
@@ -208,7 +210,16 @@ void Can_SetMotorPower(int iMotorIndex, int iPower)
} }
else else
{ {
motctrl[iMotorIndex].iMotorPowerRaw = (((MOTOR_PWR_MAX_RAW - MOTOR_PWR_MIN_RAW) * (motctrl[iMotorIndex].iMotorPowerSteps - 1)) / (MOTOR_PWR_STEP_COUNT - 1)) + MOTOR_PWR_MIN_RAW; // Scale motor power from steps to raw value
motctrl[iMotorIndex].iMotorPowerRaw = (((settings.iMotorPwrMaxRaw - settings.iMotorPwrMinRaw) * (motctrl[iMotorIndex].iMotorPowerSteps - 1)) / (settings.iMotorPwrStepCount - 1)) + settings.iMotorPwrMinRaw;
if (motctrl[iMotorIndex].iMotorPowerRaw < settings.iMotorPwrMinRaw)
{
motctrl[iMotorIndex].iMotorPowerRaw = settings.iMotorPwrMinRaw;
}
else if (motctrl[iMotorIndex].iMotorPowerRaw > settings.iMotorPwrMaxRaw)
{
motctrl[iMotorIndex].iMotorPowerRaw = settings.iMotorPwrMaxRaw;
}
} }
mylog(LOG_INFO, "CAN: Motor[%d]: Set power to %d -> %d", mylog(LOG_INFO, "CAN: Motor[%d]: Set power to %d -> %d",

View File

@@ -15,17 +15,13 @@
#include <linux/can.h> #include <linux/can.h>
#include <linux/can/raw.h> #include <linux/can/raw.h>
#define MOTOR_COUNT 2 #define MAX_MOTOR_COUNT 2
// motor gear: 0x7C=reverse, 0x7D=neutral, 0x7E=forward // motor gear: 0x7C=reverse, 0x7D=neutral, 0x7E=forward
#define MOTOR_GEAR_REVERSE 0x7C #define MOTOR_GEAR_REVERSE 0x7C
#define MOTOR_GEAR_NEUTRAL 0x7D #define MOTOR_GEAR_NEUTRAL 0x7D
#define MOTOR_GEAR_FORWARD 0x7E #define MOTOR_GEAR_FORWARD 0x7E
#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 struct MOTOR_CONTROL_DATA
{ {
char nDriveConnected; char nDriveConnected;
@@ -36,7 +32,7 @@ struct MOTOR_CONTROL_DATA
unsigned char nSwitchState; unsigned char nSwitchState;
int iActualMotorPowerW; int iActualMotorPowerW;
}; };
extern struct MOTOR_CONTROL_DATA motctrl[MOTOR_COUNT]; extern struct MOTOR_CONTROL_DATA motctrl[MAX_MOTOR_COUNT];
struct CAN_INTERFACE_DATA struct CAN_INTERFACE_DATA
{ {

19
main.c
View File

@@ -3,6 +3,7 @@
#include <mqtt/mqtt_client.h> #include <mqtt/mqtt_client.h>
#include <can/can_client.h> #include <can/can_client.h>
#include <io/io.h> #include <io/io.h>
#include <settings/settings.h>
// Period info of the realtime task // Period info of the realtime task
struct period_info pinfo; struct period_info pinfo;
@@ -15,18 +16,20 @@ int iLogToConsole = 1;
/// @param format /// @param format
/// @param /// @param
void mylog(int prio, const char *format, ...) void mylog(int prio, const char *format, ...)
{
if (prio >= settings.iDebugLevel)
{ {
va_list args; va_list args;
// 1. Initialisiere die Argumentenliste mit dem letzten festen Parameter // 1. Initialize the argument list with the last fixed argument
va_start(args, format); va_start(args, format);
// 2. Übergabe an vsyslog (statt syslog) // 2. Transfer to vsyslog (instead of syslog)
// vsyslog nimmt eine va_list entgegen // vsyslog accepts a va_list
vsyslog(prio, format, args); vsyslog(prio, format, args);
// 3. Optional: Zusätzlich auf die Konsole ausgeben // 3. Optional: Output additionally to the console
// Wir müssen die Liste neu initialisieren, da va_list "verbraucht" wird // We have to reinitialize the list because va_list is "consumed."
if (iLogToConsole) if (iLogToConsole)
{ {
va_end(args); va_end(args);
@@ -35,9 +38,10 @@ va_list args;
fprintf(stderr, "\n"); fprintf(stderr, "\n");
} }
// 4. Aufräumen // 4. Cleanup
va_end(args); va_end(args);
} }
}
/// @brief Initialize period_info with period_ms for cyclic task /// @brief Initialize period_info with period_ms for cyclic task
@@ -209,6 +213,9 @@ int main(int argc, char* argv[])
pthread_t thread; pthread_t thread;
int ret; int ret;
// First we have to get the default values of our settings
Settings_InitDefaultValues();
openlog("CanRtDriver", LOG_PID | LOG_CONS, LOG_DAEMON); openlog("CanRtDriver", LOG_PID | LOG_CONS, LOG_DAEMON);
mylog(LOG_INFO, "Service started. PID: %d", getpid()); mylog(LOG_INFO, "Service started. PID: %d", getpid());

View File

@@ -7,13 +7,14 @@ CFLAGS = -Wextra -Wall -std=gnu99 -I. -Wno-unused-parameter -Wno-unused-variable
MQTT_SOURCES = mqtt/mqtt_client.c MQTT_SOURCES = mqtt/mqtt_client.c
CAN_SOURCES = can/can_client.c CAN_SOURCES = can/can_client.c
IO_SOURCES = io/io.c IO_SOURCES = io/io.c
SETTINGS_SOURCES = settings/settings.c
PROG = bin/CanRtDriver PROG = bin/CanRtDriver
BINDIR = bin BINDIR = bin
all: $(BINDIR) $(PROG) all: $(BINDIR) $(PROG)
bin/CanRtDriver: main.c $(CAN_SOURCES) $(MQTT_SOURCES) $(IO_SOURCES) bin/CanRtDriver: main.c $(CAN_SOURCES) $(MQTT_SOURCES) $(IO_SOURCES) $(SETTINGS_SOURCES)
$(CC) $(CFLAGS) $^ -lpthread -lmosquitto -lwiringPi -o $@ $(CC) $(CFLAGS) $^ -lpthread -lmosquitto -lwiringPi -o $@
$(BINDIR): $(BINDIR):

View File

@@ -1,7 +1,7 @@
#if !defined(__MQTT_CLIENT_H__) #if !defined(__MQTT_CLIENT_H__)
#define __MQTT_CLIENT_H__ #define __MQTT_CLIENT_H__
#define USE_MOSQUITTO_LIB //#define USE_MOSQUITTO_LIB
int MqttClient_Connect(); int MqttClient_Connect();
void MqttClient_Close(); void MqttClient_Close();

23
settings/settings.c Normal file
View File

@@ -0,0 +1,23 @@
#include <main.h>
#include <settings/settings.h>
struct APP_SETTINGS settings;
void Settings_InitDefaultValues()
{
// #define LOG_EMERG 0 /* system is unusable */
// #define LOG_ALERT 1 /* action must be taken immediately */
// #define LOG_CRIT 2 /* critical conditions */
// #define LOG_ERR 3 /* error conditions */
// #define LOG_WARNING 4 /* warning conditions */
// #define LOG_NOTICE 5 /* normal but significant condition */
// #define LOG_INFO 6 /* informational */
// #define LOG_DEBUG 7 /* debug-level messages */
settings.iDebugLevel = LOG_INFO;
settings.iMotorCount = 1;
settings.iMotorPwrMinRaw = 38;
settings.iMotorPwrMaxRaw = 250;
settings.iMotorPwrStepCount = 7;
}

17
settings/settings.h Normal file
View File

@@ -0,0 +1,17 @@
#if !defined(__SETTINGS_H__)
#define __SETTINGS_H__
struct APP_SETTINGS
{
int iDebugLevel; // Level of debug messages
int iMotorCount; // Number of used motors (1 or 2)
int iMotorPwrMinRaw; // Minimum power value for motor (raw value)
int iMotorPwrMaxRaw; // Maximum power value for motor (raw value)
int iMotorPwrStepCount; // Number of power steps
};
extern struct APP_SETTINGS settings;
void Settings_InitDefaultValues();
#endif