Benutzer-Werkzeuge

Webseiten-Werkzeuge


hardware:controllers:bk-g4m_lesekopf

Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen angezeigt.

Link zu dieser Vergleichsansicht

Beide Seiten der vorigen RevisionVorhergehende Überarbeitung
Nächste Überarbeitung
Vorhergehende Überarbeitung
hardware:controllers:bk-g4m_lesekopf [2022/11/12 11:41] – [Benötigte Komponenten] tilmanhardware:controllers:bk-g4m_lesekopf [2022/11/12 11:54] (aktuell) – [Binary] tilman
Zeile 39: Zeile 39:
  * Example5_Basics.cpp  * Example5_Basics.cpp
  * HSCDTD008A Library Demo  * HSCDTD008A Library Demo
- Copyright Tilman Glötzner+ * Tilman Glötzner
  * Original Creation Date: 2022-09-03  * Original Creation Date: 2022-09-03
  *  *
Zeile 59: Zeile 59:
 #include <pthread.h> #include <pthread.h>
 #include <semaphore.h> #include <semaphore.h>
 +
 +#define NUMBABSENTRIES 3
  
 // Create an instance of the sensor. // Create an instance of the sensor.
Zeile 67: Zeile 69:
   hscdtd_status_t status;   hscdtd_status_t status;
      
 +  //geomag.begin();
   // If you know the I2C address is different than in the provided   // If you know the I2C address is different than in the provided
   // data sheet. Uncomment the line below, and configure the address.   // data sheet. Uncomment the line below, and configure the address.
Zeile 89: Zeile 91:
 hscdtd_status_t status; hscdtd_status_t status;
 sem_t mutex; sem_t mutex;
-float bAbs = 0+float bAbs[NUMBABSENTRIES];
-float bAbsOld = 0;+int bAbsLastEntryIndex = 0;
 float lowLimit = 50; float lowLimit = 50;
 float highLimit = 200; float highLimit = 200;
Zeile 96: Zeile 98:
 unsigned long totalCounter = 0; unsigned long totalCounter = 0;
 int stopFlag = 0; int stopFlag = 0;
 +int daemonized = 0; 
 +int verbose = 2;
  
 void* loop(void *ptr) void* loop(void *ptr)
 { {
-    // Explicitly start a reading. + int k;
-  while (stopFlag == 0) +
-  { +
-   int i = 0;+
  
-   for (i = 0; i < 2;i++) + memset(bAbs, 0, sizeof(float)); 
-   { + // Explicitly start a reading
-   status = geomag.startMeasurement(); + while (stopFlag == 0) 
-   // If the status is OK then we can print the result+ {
-   if (status == HSCDTD_STAT_OK) +
-   { +
-   bAbs = sqrt(pow(geomag.mag.mag_x, 2) + +
-   pow(geomag.mag.mag_y, 2) + +
-   pow(geomag.mag.mag_z, 2)); +
-  +
-   if (( bAbs < highLimit) && (abs(bAbsOld - bAbs)>0.001)+
-   { +
-   printf("X: %f uT, Y: %f uT, Z: %f uT; |B|: %f uT; |Bold|: %f uT", +
-   geomag.mag.mag_x, geomag.mag.mag_y, geomag.mag.mag_z, bAbs, bAbsOld); +
-   printf(";Counter: %lu; TotalCounter: %lu\n", counter,totalCounter);  +
-   } +
-   // trigger on falling flank +
-   if (((bAbs < lowLimit) && (bAbsOld > highLimit)) ) +
-   { +
- sem_wait(&mutex); +
- counter++; +
- sem_post(&mutex);+
  
- totalCounter++;+ status = geomag.startMeasurement(); 
 + // If the status is OK then we can print the result. 
 + if (status == HSCDTD_STAT_OK) 
 +
 + bAbs[bAbsLastEntryIndex] = sqrt(pow(geomag.mag.mag_x, 2) + 
 + pow(geomag.mag.mag_y, 2) + 
 + pow(geomag.mag.mag_z, 2));
  
- printf("INC: Counter: %lu", counter); + // weed out erroneous readings bAbs = 0 
- printf(";TotalCounter: %lu\n", totalCounter);+ if (bAbs[bAbsLastEntryIndex] == 0) 
 +
 + //erronous measurement
  
-   + if (verbose >=2) 
-   bAbsOld = bAbs; +
-   + char c; 
-   else + printf("bABS==0;"); 
-   + printf("X: %f uT, Y: %f uT, Z: %f uT; |B|: %f uT;\n", 
-  + geomag.mag.mag_x, geomag.mag.mag_y, geomag.mag.mag_z,bAbs[bAbsLastEntryIndex]); 
-   printf("Error occurred, unable to read sensor data. Exiting ...\n"); + for (k = 0 ; k < NUMBABSENTRIES; k++) 
-   stopFlag = 1; +
-   }+ 
 + if (k == bAbsLastEntryIndex) 
 + c = '*'; 
 + else 
 + c = ' '; 
 + printf("%c|B[%d]|: %f uT;",c,k,bAbs[k]); 
 +
 + printf(";Counter: %lu; TotalCounter: %lu\n", counter,totalCounter); 
 +
 + continue; 
 + 
 +
 + // trigger on falling flank. 
 + // To prevent occassional missreadings (i.e. bAbs 0)  several measurements are used 
 + // all new entries but the oldest need to be under the lower limit 
 + // while the oldest entriey needs to be above the high limit 
 + int trigger = 0; 
 + int index = 0; 
 + int mod = 0; 
 + for (k=0; k < NUMBABSENTRIES; k++) 
 +
 + mod = ((bAbsLastEntryIndex-k) % NUMBABSENTRIES); 
 + index = mod >= 0 ? mod : NUMBABSENTRIES + mod ; 
 + if (k<NUMBABSENTRIES-1) 
 +
 + if (bAbs[index] < lowLimit) 
 + trigger++
 +
 + else 
 + { 
 + if (bAbs[index] > highLimit) 
 + trigger++; 
 +
 + if (verbose >=3 ) 
 +
 + printf("trigger: %d; bAbs[%d]: %f\n", trigger, index, bAbs[index] ); 
 + } 
 +
 + 
 + 
 + if (trigger ==  NUMBABSENTRIES) 
 +
 + sem_wait(&mutex); 
 + counter++; 
 + sem_post(&mutex); 
 + 
 + totalCounter++; 
 + 
 + if (verbose >= 2) 
 + { 
 + printf("INC: Counter: %lu", counter); 
 + printf(";TotalCounter: %lu;", totalCounter); 
 + 
 + char c; 
 + for (k = 0 ; k < NUMBABSENTRIES; k++) 
 +
 + if (k == bAbsLastEntryIndex) 
 + c = '*'; 
 + else 
 + c = ' '; 
 + printf("%c|B[%d]|: %f uT;",c,k,bAbs[k]); 
 +
 + printf("\n"); 
 +
 +
 + bAbsLastEntryIndex = (bAbsLastEntryIndex + 1) % NUMBABSENTRIES; 
 +
 + else 
 +
 + if (verbose >= 1) 
 +
 + printf("Error occurred, unable to read sensor data. Exiting ...\n"); 
 +
 + stopFlag = 1; 
 + //exit(1); 
 + } 
 + 
 + // Wait 500ms  before reading the next sample. 
 + usleep(500000);
  
-   // Wait 500ms  before reading the next sample. 
-   usleep(500000); 
-   } 
   }   }
   return NULL;   return NULL;
 } }
- 
  
  
 int main(int argc, char** argv) int main(int argc, char** argv)
 { {
 +
    setup();    setup();
  
Zeile 206: Zeile 271:
  
 } }
 +
 +
 +
 +
  
  
hardware/controllers/bk-g4m_lesekopf.1668249680.txt.gz · Zuletzt geändert: 2022/11/12 11:41 von tilman