From e9d16a80d43e5a3b9cebca05f669b9089db36149 Mon Sep 17 00:00:00 2001 From: Simon Schmidt Date: Sat, 3 Jul 2021 13:52:52 +0200 Subject: [PATCH] FSM 2.0 einfacher xD lol: ) --- lib/treppe/FSMTreppe2/FSMTreppe2.cpp | 236 +++++++++++++++++++++++++++ lib/treppe/FSMTreppe2/FSMTreppe2.h | 139 ++++++++++++++++ lib/treppe/treppe.cpp | 8 +- lib/treppe/treppe.h | 4 + 4 files changed, 381 insertions(+), 6 deletions(-) create mode 100644 lib/treppe/FSMTreppe2/FSMTreppe2.cpp create mode 100644 lib/treppe/FSMTreppe2/FSMTreppe2.h diff --git a/lib/treppe/FSMTreppe2/FSMTreppe2.cpp b/lib/treppe/FSMTreppe2/FSMTreppe2.cpp new file mode 100644 index 0000000..c4eef07 --- /dev/null +++ b/lib/treppe/FSMTreppe2/FSMTreppe2.cpp @@ -0,0 +1,236 @@ +// +// Academic License - for use in teaching, academic research, and meeting +// course requirements at degree granting institutions only. Not for +// government, commercial, or other organizational use. +// +// File: FSMTreppe.cpp +// +// Code generated for Simulink model 'FSMTreppe'. +// +// Model version : 1.28 +// Simulink Coder version : 9.5 (R2021a) 14-Nov-2020 +// C/C++ source code generated on : Sat Jul 3 13:50:17 2021 +// +// Target selection: ert.tlc +// Embedded hardware selection: ARM Compatible->ARM Cortex-M +// Code generation objectives: Unspecified +// Validation result: Not run +// +#include "FSMTreppe.h" +#include "FSMTreppe_private.h" + +// Named constants for Chart: '/FSMTreppe' +const uint8_T FSMTreppe_IN_abdimmen_hoch = 1U; +const uint8_T FSMTreppe_IN_abdimmen_runter = 2U; +const uint8_T FSMTreppe_IN_aufdimmen_hoch = 3U; +const uint8_T FSMTreppe_IN_aufdimmen_runter = 4U; +const uint8_T FSMTreppe_IN_inaktiv = 5U; +const uint8_T FSMTreppe_IN_ruhezustand = 6U; +const uint8_T FSMTreppe_IN_warten_hoch = 7U; +const uint8_T FSMTreppe_IN_warten_runter = 8U; + +// Model step function +void FSMTreppeModelClass::step() +{ + // Chart: '/FSMTreppe' incorporates: + // Inport: '/anim_beendet' + // Inport: '/ldr_schwelle' + // Inport: '/sensor_oben' + // Inport: '/sensor_unten' + + if (FSMTreppe_DW.temporalCounter_i1 < 511U) { + FSMTreppe_DW.temporalCounter_i1 = static_cast + (FSMTreppe_DW.temporalCounter_i1 + 1U); + } + + if (FSMTreppe_DW.is_active_c3_FSMTreppe == 0U) { + FSMTreppe_DW.is_active_c3_FSMTreppe = 1U; + FSMTreppe_DW.is_c3_FSMTreppe = FSMTreppe_IN_inaktiv; + } else { + switch (FSMTreppe_DW.is_c3_FSMTreppe) { + case FSMTreppe_IN_abdimmen_hoch: + // Outport: '/dimmrichtung' + FSMTreppe_Y.dimmrichtung = 0U; + + // Outport: '/status' + FSMTreppe_Y.status = 3U; + if (FSMTreppe_U.anim_beendet || (FSMTreppe_DW.temporalCounter_i1 >= 500U)) + { + FSMTreppe_DW.is_c3_FSMTreppe = FSMTreppe_IN_ruhezustand; + + // Outport: '/status' + FSMTreppe_Y.status = 0U; + } + break; + + case FSMTreppe_IN_abdimmen_runter: + // Outport: '/dimmrichtung' + FSMTreppe_Y.dimmrichtung = 0U; + + // Outport: '/status' + FSMTreppe_Y.status = 6U; + if (FSMTreppe_U.anim_beendet || (FSMTreppe_DW.temporalCounter_i1 >= 500U)) + { + FSMTreppe_DW.is_c3_FSMTreppe = FSMTreppe_IN_ruhezustand; + + // Outport: '/status' + FSMTreppe_Y.status = 0U; + } + break; + + case FSMTreppe_IN_aufdimmen_hoch: + // Outport: '/laufrichtung' + FSMTreppe_Y.laufrichtung = 1U; + + // Outport: '/dimmrichtung' + FSMTreppe_Y.dimmrichtung = 1U; + + // Outport: '/status' + FSMTreppe_Y.status = 1U; + if (FSMTreppe_U.anim_beendet || (FSMTreppe_DW.temporalCounter_i1 >= 500U)) + { + FSMTreppe_DW.is_c3_FSMTreppe = FSMTreppe_IN_warten_hoch; + FSMTreppe_DW.temporalCounter_i1 = 0U; + + // Outport: '/status' + FSMTreppe_Y.status = 2U; + } + break; + + case FSMTreppe_IN_aufdimmen_runter: + // Outport: '/laufrichtung' + FSMTreppe_Y.laufrichtung = 0U; + + // Outport: '/dimmrichtung' + FSMTreppe_Y.dimmrichtung = 1U; + + // Outport: '/status' + FSMTreppe_Y.status = 4U; + if (FSMTreppe_U.anim_beendet || (FSMTreppe_DW.temporalCounter_i1 >= 500U)) + { + FSMTreppe_DW.is_c3_FSMTreppe = FSMTreppe_IN_warten_runter; + FSMTreppe_DW.temporalCounter_i1 = 0U; + + // Outport: '/status' + FSMTreppe_Y.status = 5U; + } + break; + + case FSMTreppe_IN_inaktiv: + if (FSMTreppe_U.ldr_schwelle == 1.0) { + FSMTreppe_DW.is_c3_FSMTreppe = FSMTreppe_IN_ruhezustand; + + // Outport: '/status' + FSMTreppe_Y.status = 0U; + } + break; + + case FSMTreppe_IN_ruhezustand: + // Outport: '/status' + FSMTreppe_Y.status = 0U; + if (FSMTreppe_U.sensor_unten) { + FSMTreppe_DW.is_c3_FSMTreppe = FSMTreppe_IN_aufdimmen_hoch; + FSMTreppe_DW.temporalCounter_i1 = 0U; + + // Outport: '/laufrichtung' + FSMTreppe_Y.laufrichtung = 1U; + + // Outport: '/dimmrichtung' + FSMTreppe_Y.dimmrichtung = 1U; + + // Outport: '/status' + FSMTreppe_Y.status = 1U; + } else if (FSMTreppe_U.sensor_oben) { + FSMTreppe_DW.is_c3_FSMTreppe = FSMTreppe_IN_aufdimmen_runter; + FSMTreppe_DW.temporalCounter_i1 = 0U; + + // Outport: '/laufrichtung' + FSMTreppe_Y.laufrichtung = 0U; + + // Outport: '/dimmrichtung' + FSMTreppe_Y.dimmrichtung = 1U; + + // Outport: '/status' + FSMTreppe_Y.status = 4U; + } else if (FSMTreppe_U.ldr_schwelle == 0.0) { + FSMTreppe_DW.is_c3_FSMTreppe = FSMTreppe_IN_inaktiv; + } + break; + + case FSMTreppe_IN_warten_hoch: + // Outport: '/status' + FSMTreppe_Y.status = 2U; + if (FSMTreppe_U.sensor_oben || (FSMTreppe_DW.temporalCounter_i1 >= 500U)) + { + FSMTreppe_DW.is_c3_FSMTreppe = FSMTreppe_IN_abdimmen_hoch; + FSMTreppe_DW.temporalCounter_i1 = 0U; + + // Outport: '/dimmrichtung' + FSMTreppe_Y.dimmrichtung = 0U; + + // Outport: '/status' + FSMTreppe_Y.status = 3U; + } + break; + + default: + // Outport: '/status' + // case IN_warten_runter: + FSMTreppe_Y.status = 5U; + if (FSMTreppe_U.sensor_unten || (FSMTreppe_DW.temporalCounter_i1 >= 500U)) + { + FSMTreppe_DW.is_c3_FSMTreppe = FSMTreppe_IN_abdimmen_runter; + FSMTreppe_DW.temporalCounter_i1 = 0U; + + // Outport: '/dimmrichtung' + FSMTreppe_Y.dimmrichtung = 0U; + + // Outport: '/status' + FSMTreppe_Y.status = 6U; + } + break; + } + } + + // End of Chart: '/FSMTreppe' +} + +// Model initialize function +void FSMTreppeModelClass::initialize() +{ + // (no initialization code required) +} + +// Model terminate function +void FSMTreppeModelClass::terminate() +{ + // (no terminate code required) +} + +// Constructor +FSMTreppeModelClass::FSMTreppeModelClass() : + FSMTreppe_DW(), + FSMTreppe_U(), + FSMTreppe_Y(), + FSMTreppe_M() +{ + // Currently there is no constructor body generated. +} + +// Destructor +FSMTreppeModelClass::~FSMTreppeModelClass() +{ + // Currently there is no destructor body generated. +} + +// Real-Time Model get method +FSMTreppeModelClass::RT_MODEL_FSMTreppe_T * FSMTreppeModelClass::getRTM() +{ + return (&FSMTreppe_M); +} + +// +// File trailer for generated code. +// +// [EOF] +// diff --git a/lib/treppe/FSMTreppe2/FSMTreppe2.h b/lib/treppe/FSMTreppe2/FSMTreppe2.h new file mode 100644 index 0000000..33af230 --- /dev/null +++ b/lib/treppe/FSMTreppe2/FSMTreppe2.h @@ -0,0 +1,139 @@ +// +// Academic License - for use in teaching, academic research, and meeting +// course requirements at degree granting institutions only. Not for +// government, commercial, or other organizational use. +// +// File: FSMTreppe.h +// +// Code generated for Simulink model 'FSMTreppe'. +// +// Model version : 1.28 +// Simulink Coder version : 9.5 (R2021a) 14-Nov-2020 +// C/C++ source code generated on : Sat Jul 3 13:50:17 2021 +// +// Target selection: ert.tlc +// Embedded hardware selection: ARM Compatible->ARM Cortex-M +// Code generation objectives: Unspecified +// Validation result: Not run +// +#ifndef RTW_HEADER_FSMTreppe_h_ +#define RTW_HEADER_FSMTreppe_h_ +#include "rtwtypes.h" +#include "FSMTreppe_types.h" + +// Macros for accessing real-time model data structure +#ifndef rtmGetErrorStatus +#define rtmGetErrorStatus(rtm) ((rtm)->errorStatus) +#endif + +#ifndef rtmSetErrorStatus +#define rtmSetErrorStatus(rtm, val) ((rtm)->errorStatus = (val)) +#endif + +// Class declaration for model FSMTreppe +class FSMTreppeModelClass { + // public data and function members + public: + // Block states (default storage) for system '' + struct DW_FSMTreppe_T { + uint16_T temporalCounter_i1; // '/FSMTreppe' + uint8_T is_active_c3_FSMTreppe; // '/FSMTreppe' + uint8_T is_c3_FSMTreppe; // '/FSMTreppe' + }; + + // External inputs (root inport signals with default storage) + struct ExtU_FSMTreppe_T { + boolean_T sensor_unten; // '/sensor_unten' + boolean_T sensor_oben; // '/sensor_oben' + boolean_T anim_beendet; // '/anim_beendet' + real_T ldr_schwelle; // '/ldr_schwelle' + }; + + // External outputs (root outports fed by signals with default storage) + struct ExtY_FSMTreppe_T { + uint8_T laufrichtung; // '/laufrichtung' + uint8_T status; // '/status' + uint8_T dimmrichtung; // '/dimmrichtung' + }; + + // Real-time Model Data Structure + struct RT_MODEL_FSMTreppe_T { + const char_T * volatile errorStatus; + }; + + // model initialize function + void initialize(); + + // model step function + void step(); + + // model terminate function + void terminate(); + + // Constructor + FSMTreppeModelClass(); + + // Destructor + ~FSMTreppeModelClass(); + + // Root-level structure-based inputs set method + + // Root inports set method + void setExternalInputs(const ExtU_FSMTreppe_T* pExtU_FSMTreppe_T) + { + FSMTreppe_U = *pExtU_FSMTreppe_T; + } + + // Root-level structure-based outputs get method + + // Root outports get method + const FSMTreppeModelClass::ExtY_FSMTreppe_T & getExternalOutputs() const + { + return FSMTreppe_Y; + } + + // Real-Time Model get method + FSMTreppeModelClass::RT_MODEL_FSMTreppe_T * getRTM(); + + // private data and function members + private: + // Block states + DW_FSMTreppe_T FSMTreppe_DW; + + // External inputs + ExtU_FSMTreppe_T FSMTreppe_U; + + // External outputs + ExtY_FSMTreppe_T FSMTreppe_Y; + + // Real-Time Model + RT_MODEL_FSMTreppe_T FSMTreppe_M; +}; + +//- +// The generated code includes comments that allow you to trace directly +// back to the appropriate location in the model. The basic format +// is /block_name, where system is the system number (uniquely +// assigned by Simulink) and block_name is the name of the block. +// +// Note that this particular code originates from a subsystem build, +// and has its own system numbers different from the parent model. +// Refer to the system hierarchy for this subsystem below, and use the +// MATLAB hilite_system command to trace the generated code back +// to the parent model. For example, +// +// hilite_system('FSM_Treppenlicht/FSMTreppe') - opens subsystem FSM_Treppenlicht/FSMTreppe +// hilite_system('FSM_Treppenlicht/FSMTreppe/Kp') - opens and selects block Kp +// +// Here is the system hierarchy for this model +// +// '' : 'FSM_Treppenlicht' +// '' : 'FSM_Treppenlicht/FSMTreppe' + +#endif // RTW_HEADER_FSMTreppe_h_ + +// +// File trailer for generated code. +// +// [EOF] +// diff --git a/lib/treppe/treppe.cpp b/lib/treppe/treppe.cpp index ebb53c5..4d7b858 100644 --- a/lib/treppe/treppe.cpp +++ b/lib/treppe/treppe.cpp @@ -181,12 +181,8 @@ void Treppe::task(){ if(finish){ direction = switch_direction; state = switch_state; - } - static uint8_t last_sensor_state[2] = {0,0}; - uint8_t current_sensor_state[2] = {0,0}; - current_sensor_state[0] = digitalRead(SENSOR_OBEN); - current_sensor_state[1] = digitalRead(SENSOR_UNTEN); - + } + fsm_inputs.sensor_oben = read_sensor(SENSOR_OBEN); fsm_inputs.sensor_unten = read_sensor(SENSOR_UNTEN); fsm_inputs.anim_finished = static_cast(finish); diff --git a/lib/treppe/treppe.h b/lib/treppe/treppe.h index b225199..448bb15 100644 --- a/lib/treppe/treppe.h +++ b/lib/treppe/treppe.h @@ -39,6 +39,10 @@ private: FSMTreppeModelClass::ExtU_FSMTreppe_T fsm_inputs; FSMTreppeModelClass::ExtY_FSMTreppe_T fsm_outputs; void print_state_on_change(); + + const uint8_t FSMTreppe_IN_animation_down = 1U; + const uint8_t FSMTreppe_IN_animation_up = 2U; + const uint8_t FSMTreppe_IN_idle = 3U; uint8_t softstart_led(uint8_t led, uint16_t startval, uint16_t stopval);