hardware:controllers:bk-g4m_lesekopf
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Beide Seiten der vorigen RevisionVorhergehende ÜberarbeitungNächste Überarbeitung | Vorhergehende Überarbeitung | ||
| hardware:controllers:bk-g4m_lesekopf [2022/11/12 11:41] – [Benötigte Komponenten] tilman | hardware: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 | ||
| - | | + | * Tilman Glötzner |
| * Original Creation Date: 2022-09-03 | * Original Creation Date: 2022-09-03 | ||
| * | * | ||
| Zeile 59: | Zeile 59: | ||
| #include < | #include < | ||
| #include < | #include < | ||
| + | |||
| + | #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; | ||
| | | ||
| + | // | ||
| // 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 | + | int bAbsLastEntryIndex |
| 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) | + | |
| - | { | + | |
| - | | + | |
| - | for (i = 0; i < 2;i++) | + | memset(bAbs, 0, sizeof(float)); |
| - | { | + | // Explicitly start a reading. |
| - | status = geomag.startMeasurement(); | + | while (stopFlag |
| - | | + | { |
| - | if (status | + | |
| - | { | + | |
| - | bAbs = sqrt(pow(geomag.mag.mag_x, | + | |
| - | pow(geomag.mag.mag_y, | + | |
| - | pow(geomag.mag.mag_z, | + | |
| - | + | ||
| - | if (( bAbs < highLimit) && (abs(bAbsOld - bAbs)>0.001)) | + | |
| - | | + | |
| - | printf(" | + | |
| - | geomag.mag.mag_x, | + | |
| - | printf("; | + | |
| - | } | + | |
| - | // trigger on falling flank | + | |
| - | if (((bAbs < lowLimit) && (bAbsOld > highLimit)) ) | + | |
| - | { | + | |
| - | sem_wait(& | + | |
| - | counter++; | + | |
| - | sem_post(& | + | |
| - | 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, | ||
| + | pow(geomag.mag.mag_y, | ||
| + | pow(geomag.mag.mag_z, | ||
| - | printf(" | + | // weed out erroneous readings bAbs = 0 |
| - | printf("; | + | if (bAbs[bAbsLastEntryIndex] == 0) |
| + | { | ||
| + | // | ||
| - | | + | if (verbose >=2) |
| - | bAbsOld | + | { |
| - | | + | char c; |
| - | | + | printf(" |
| - | | + | printf(" |
| - | + | geomag.mag.mag_x, | |
| - | | + | for (k = 0 ; k < NUMBABSENTRIES; |
| - | | + | { |
| - | | + | |
| + | if (k == bAbsLastEntryIndex) | ||
| + | c = ' | ||
| + | else | ||
| + | c = ' '; | ||
| + | printf(" | ||
| + | } | ||
| + | printf("; | ||
| + | } | ||
| + | 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; | ||
| + | { | ||
| + | mod = ((bAbsLastEntryIndex-k) % NUMBABSENTRIES); | ||
| + | index = mod >= 0 ? mod : NUMBABSENTRIES + mod ; | ||
| + | if (k< | ||
| + | { | ||
| + | if (bAbs[index] < lowLimit) | ||
| + | trigger++; | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | if (bAbs[index] > highLimit) | ||
| + | trigger++; | ||
| + | } | ||
| + | if (verbose >=3 ) | ||
| + | { | ||
| + | printf(" | ||
| + | } | ||
| + | } | ||
| + | |||
| + | |||
| + | if (trigger == NUMBABSENTRIES) | ||
| + | { | ||
| + | sem_wait(& | ||
| + | counter++; | ||
| + | sem_post(& | ||
| + | |||
| + | totalCounter++; | ||
| + | |||
| + | if (verbose >= 2) | ||
| + | { | ||
| + | printf(" | ||
| + | printf("; | ||
| + | |||
| + | char c; | ||
| + | for (k = 0 ; k < NUMBABSENTRIES; | ||
| + | { | ||
| + | if (k == bAbsLastEntryIndex) | ||
| + | c = ' | ||
| + | else | ||
| + | c = ' '; | ||
| + | printf(" | ||
| + | } | ||
| + | printf(" | ||
| + | } | ||
| + | } | ||
| + | bAbsLastEntryIndex = (bAbsLastEntryIndex + 1) % NUMBABSENTRIES; | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | if (verbose >= 1) | ||
| + | { | ||
| + | printf(" | ||
| + | } | ||
| + | stopFlag = 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) | ||
| { | { | ||
| + | |||
| | | ||
| Zeile 206: | Zeile 271: | ||
| } | } | ||
| + | |||
| + | |||
| + | |||
| + | |||
hardware/controllers/bk-g4m_lesekopf.1668249680.txt.gz · Zuletzt geändert: von tilman