diff --git a/can/can_client.c b/can/can_client.c index 8f3e962..d14e7fb 100755 --- a/can/can_client.c +++ b/can/can_client.c @@ -1,14 +1,15 @@ #include "main.h" +#include #include #include #include int iCanSimu = 0; -struct MOTOR_CONTROL_DATA motctrl[MOTOR_COUNT]; -struct CAN_INTERFACE_DATA intf_data[MOTOR_COUNT]; +struct MOTOR_CONTROL_DATA motctrl[MAX_MOTOR_COUNT]; +struct CAN_INTERFACE_DATA intf_data[MAX_MOTOR_COUNT]; int iBusTimeoutCounter = 0; @@ -47,7 +48,8 @@ int Can_OpenInterface(int iMotorIndex, const char * ifacename) motctrl[iMotorIndex].nDriveConnected = 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); Can_SetMotorGear(iMotorIndex, MOTOR_GEAR_NEUTRAL); @@ -177,7 +179,7 @@ void Can_SetMotorGear(int iMotorIndex, int iGear) /// @brief Set power for the given motor /// @param iMotorIndex -/// @param iPower (Range: 0..MOTOR_PWR_STEP_COUNT) +/// @param iPower (Range: 0..settings.iMotorPwrStepCount) void Can_SetMotorPower(int iMotorIndex, int iPower) { 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 motctrl[iMotorIndex].iMotorPowerSteps = 1; } - else if (iPower >= MOTOR_PWR_STEP_COUNT) + else if (iPower >= settings.iMotorPwrStepCount) { // limit to max. power - motctrl[iMotorIndex].iMotorPowerSteps = MOTOR_PWR_STEP_COUNT; + motctrl[iMotorIndex].iMotorPowerSteps = settings.iMotorPwrStepCount; } else { @@ -208,7 +210,16 @@ void Can_SetMotorPower(int iMotorIndex, int iPower) } 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", diff --git a/can/can_client.h b/can/can_client.h index 65cefab..627c0c1 100755 --- a/can/can_client.h +++ b/can/can_client.h @@ -15,17 +15,13 @@ #include #include -#define MOTOR_COUNT 2 +#define MAX_MOTOR_COUNT 2 // motor gear: 0x7C=reverse, 0x7D=neutral, 0x7E=forward #define MOTOR_GEAR_REVERSE 0x7C #define MOTOR_GEAR_NEUTRAL 0x7D #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 { char nDriveConnected; @@ -36,7 +32,7 @@ struct MOTOR_CONTROL_DATA unsigned char nSwitchState; int iActualMotorPowerW; }; -extern struct MOTOR_CONTROL_DATA motctrl[MOTOR_COUNT]; +extern struct MOTOR_CONTROL_DATA motctrl[MAX_MOTOR_COUNT]; struct CAN_INTERFACE_DATA { diff --git a/main.c b/main.c index a4d6800..fa6c88a 100755 --- a/main.c +++ b/main.c @@ -3,6 +3,7 @@ #include #include #include +#include // Period info of the realtime task struct period_info pinfo; @@ -16,27 +17,30 @@ int iLogToConsole = 1; /// @param void mylog(int prio, const char *format, ...) { -va_list args; + if (prio >= settings.iDebugLevel) + { + va_list args; - // 1. Initialisiere die Argumentenliste mit dem letzten festen Parameter - va_start(args, format); + // 1. Initialize the argument list with the last fixed argument + va_start(args, format); - // 2. Übergabe an vsyslog (statt syslog) - // vsyslog nimmt eine va_list entgegen - vsyslog(prio, format, args); + // 2. Transfer to vsyslog (instead of syslog) + // vsyslog accepts a va_list + vsyslog(prio, format, args); - // 3. Optional: Zusätzlich auf die Konsole ausgeben - // Wir müssen die Liste neu initialisieren, da va_list "verbraucht" wird - if (iLogToConsole) - { - va_end(args); - va_start(args, format); - vfprintf(stderr, format, args); - fprintf(stderr, "\n"); - } + // 3. Optional: Output additionally to the console + // We have to reinitialize the list because va_list is "consumed." + if (iLogToConsole) + { + va_end(args); + va_start(args, format); + vfprintf(stderr, format, args); + fprintf(stderr, "\n"); + } - // 4. Aufräumen - va_end(args); + // 4. Cleanup + va_end(args); + } } @@ -209,6 +213,9 @@ int main(int argc, char* argv[]) pthread_t thread; int ret; + // First we have to get the default values of our settings + Settings_InitDefaultValues(); + openlog("CanRtDriver", LOG_PID | LOG_CONS, LOG_DAEMON); mylog(LOG_INFO, "Service started. PID: %d", getpid()); diff --git a/makefile b/makefile index 8b73eb4..f8228e3 100755 --- a/makefile +++ b/makefile @@ -7,13 +7,14 @@ CFLAGS = -Wextra -Wall -std=gnu99 -I. -Wno-unused-parameter -Wno-unused-variable MQTT_SOURCES = mqtt/mqtt_client.c CAN_SOURCES = can/can_client.c IO_SOURCES = io/io.c +SETTINGS_SOURCES = settings/settings.c PROG = bin/CanRtDriver BINDIR = bin 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 $@ $(BINDIR): diff --git a/mqtt/mqtt_client.h b/mqtt/mqtt_client.h index 3a35a01..fb1ac94 100755 --- a/mqtt/mqtt_client.h +++ b/mqtt/mqtt_client.h @@ -1,7 +1,7 @@ #if !defined(__MQTT_CLIENT_H__) #define __MQTT_CLIENT_H__ -#define USE_MOSQUITTO_LIB +//#define USE_MOSQUITTO_LIB int MqttClient_Connect(); void MqttClient_Close(); diff --git a/settings/settings.c b/settings/settings.c new file mode 100644 index 0000000..e428524 --- /dev/null +++ b/settings/settings.c @@ -0,0 +1,23 @@ + +#include +#include + +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; +} diff --git a/settings/settings.h b/settings/settings.h new file mode 100644 index 0000000..664b7f2 --- /dev/null +++ b/settings/settings.h @@ -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