Jump to content

[C/C++] Alternierende Threshold Callbacks für Distance IR


Recommended Posts

Folgendes Problem:

Es soll ein Callback-Event ausgelöst werden, wenn beim Distance IR Bricklet die gemessene Entfernung einen Schwellwert unterschreitet. Allerdings soll der Callback dann nicht periodisch aufgerufen werden, sondern erst wieder, wenn der Schwellwert wieder überschritten wird (quasi Gut- und Schlecht-Alarm).

 

Vermutlich gibt das bisher die API nicht her. Ich stelle mir vor, dass dieses Verhalten durch eine "-1" bei der debounce_period dargestellt werden könnte:

distance_ir_set_debounce_period(&dist, -1);
distance_ir_register_callback(&dist, DISTANCE_IR_CALLBACK_DISTANCE_REACHED, cb_distance_ir);
distance_ir_set_distance_callback_threshold(&dist, '<', 1000, 0;

In der Bricklet Library müsste dann in simple_tick() der Part von "Handle threshold signals" so angepasst werden, dass

if (BC->signal_period[i] == -1) {
   switch(BC->threshold_option[i]) {
      case 'o': BC->threshold_option[i] = 'i';
                break;
      case 'i': BC->threshold_option[i] = 'o';
                break;
      case '<': BC->threshold_option[i] = '>';
                break;
      case '>': BC->threshold_option[i] = '<';
                break;
   }
}

Auf diese Weise würde man dann jeweils beim Überschreiten der Thresholds in beide Richtungen einen Event bekommen.Supertoll wäre es noch, wenn die Art des Events (Alarm true/false) auch beim Callback-Aufruf aus einem Parameter hervorgehen würde, aber das ging zu Not auch ohne.

 

@TF: Liesse sich das einbauen?

Link zu diesem Kommentar
Share on other sites

  • 2 weeks later...

Hier mein Vorschlag für eine Anpassung der Firmware (Bricklet):

diff --git a/software/src/config.h b/software/src/config.h
index 531eaa3..71a3b9f 100644
--- a/software/src/config.h
+++ b/software/src/config.h
@@ -10,7 +10,7 @@
#define BRICKLET_HARDWARE_NAME "Distance IR Bricklet 1.0"
#define BRICKLET_FIRMWARE_VERSION_MAJOR 1
#define BRICKLET_FIRMWARE_VERSION_MINOR 1
-#define BRICKLET_FIRMWARE_VERSION_REVISION 1
+#define BRICKLET_FIRMWARE_VERSION_REVISION 2

#define LOGGING_LEVEL LOGGING_DEBUG
#define DEBUG_BRICKLET 0
@@ -28,8 +28,8 @@ typedef struct {
        uint32_t signal_period[NUM_SIMPLE_VALUES];
        uint32_t signal_period_counter[NUM_SIMPLE_VALUES];

-       uint32_t threshold_debounce;
-       uint32_t threshold_period_current[NUM_SIMPLE_VALUES];
+       int32_t  threshold_debounce;
+       int32_t  threshold_period_current[NUM_SIMPLE_VALUES];
        int32_t  threshold_min[NUM_SIMPLE_VALUES];
        int32_t  threshold_max[NUM_SIMPLE_VALUES];
        char     threshold_option[NUM_SIMPLE_VALUES];

und für die Brickletlib:

diff --git a/bricklet_simple.c b/bricklet_simple.c
index 3574134..80e1890 100644
--- a/bricklet_simple.c
+++ b/bricklet_simple.c
@@ -276,7 +276,19 @@ void simple_tick(uint8_t tick_type) {
                                        BA->send_blocking_with_timeout(&sgvr,
                                                                                                   sizeof(SimpleGetValueReturn),
                                                                                                   *BA->com_current);
-                                       BC->threshold_period_current[i] = 0;
+
+                                       // Toggle threshold trigger if debounce has been set to -1
+                                       if(BC->threshold_debounce == -1) {
+                                               switch(BC->threshold_option[i]) {
+                                                       case 'o': BC->threshold_option[i] = 'i';
+                                                                 break;
+                                                       case 'i': BC->threshold_option[i] = 'o';
+                                                                 break;
+                                               }
+                                               BC->threshold_period_current[i] = -500;
+                                       } else {
+                                               BC->threshold_period_current[i] = 0;
+                                       }

                                        logbli("threshold value: %d\n\r", BC->value[i]);
                                }

Link zu diesem Kommentar
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Gast
Reply to this topic...

×   Du hast formatierten Text eingefügt.   Formatierung jetzt entfernen

  Only 75 emoji are allowed.

×   Dein Link wurde automatisch eingebettet.   Einbetten rückgängig machen und als Link darstellen

×   Dein vorheriger Inhalt wurde wiederhergestellt.   Clear editor

×   Du kannst Bilder nicht direkt einfügen. Lade Bilder hoch oder lade sie von einer URL.

×
×
  • Neu erstellen...