Archive 07/10/2020.

Utilisation de plusieurs thread

hetour

Bonjour,
Pour le TP7, j’ai bien compris que l’on pouvait utiliser un seul thread et rajouter la lecture des deux capteurs supplémentaires dans celui-ci. Mais je veux essayer avec 3 thread. Si je mets les priorité à la même valeur je ne lis qu’un capteur et si je régle les prioritées différentes j’ai un reboot de la cible.
Voici mon code main.
#include <stdio.h>

#include “thread.h”
#include “xtimer.h”

/* Add lps331ap related include here */
#include “lpsxxx.h”
#include “lpsxxx_params.h”

/* Add lsm303dlhc related include here */
#include “lsm303dlhc.h”
#include “lsm303dlhc_params.h”

/* Add isl29020 related include here */
#include “isl29020.h”
#include “isl29020_params.h”

/* Add l3g4200d related include here */
#include “l3g4200d.h”
#include “l3g4200d_params.h”

/* Declare the lps331ap device variable here */
static lpsxxx_t lpsxxx;

/* Declare the lsm303dlhc device variable here */
static lsm303dlhc_t lsm303dlhc;

/* Declare the isl29020 device variable here */
static isl29020_t isl29020;

/* Declare the l3g4200d device variable here */
static l3g4200d_t l3g4200d;

static char stack[THREAD_STACKSIZE_MAIN];

/* declaration du thread pour le lsm303dlhc */

static void *thread_lsm303dlhc(void *arg)
{
(void)arg;

/* Add the lsm303dlhc sensor polling endless loop here */
    while (1) {
    lsm303dlhc_3d_data_t mag_value;
    lsm303dlhc_3d_data_t acc_value;
    lsm303dlhc_read_acc(&lsm303dlhc, &acc_value);
    printf("Accelerometer x: %i y: %i z: %i\n",
           acc_value.x_axis, acc_value.y_axis, acc_value.z_axis);
    lsm303dlhc_read_mag(&lsm303dlhc, &mag_value);
    printf("Magnetometer x: %i y: %i z: %i\n",
           mag_value.x_axis, mag_value.y_axis, mag_value.z_axis);
    xtimer_usleep(500 * US_PER_MS);
}

return 0;

}

/* declaration du thread pour le isl29020 */
static void *thread_isl29020(void *arg)
{
(void)arg;

/* Add the isl29020 sensor polling endless loop here */
    while (1) {
    isl29020_t lumin_value;
    printf("Intensite lumineuse: %5i LUX\n", isl29020_read(&lumin_value));
    xtimer_usleep(500 * US_PER_MS);
}

return 0;

}

/* declaration du thread pour le l3g4200d */
static void *thread_l3g4200d (void *arg)
{
(void)arg;

/* Add the l3g4200d  sensor polling endless loop here */
    while (1) {
    l3g4200d_t dev;
    l3g4200d_data_t acc_data;
    l3g4200d_read(&dev, &acc_data);
    printf("Gyro data [dps] - X: %6i   Y: %6i   Z: %6i\n",
           acc_data.acc_x, acc_data.acc_y, acc_data.acc_z);
    xtimer_usleep(500 * US_PER_MS);
}

return 0;

}

int main(void)
{
/* Initialize the lps331ap sensor here /
lpsxxx_init(&lpsxxx, &lpsxxx_params[0]);
/
Initialize the lsm303dlhc sensor here /
lsm303dlhc_init(&lsm303dlhc, lsm303dlhc_params);
/
Initialize the isl29020 sensor here /
isl29020_init(&isl29020, &isl29020_params[0]);
/
Initialize the l3g4200d sensor here */
l3g4200d_init(&l3g4200d, &l3g4200d_params[0]);

thread_create(stack, sizeof(stack), THREAD_PRIORITY_MAIN +1,
           0, thread_lsm303dlhc, NULL, "lsm303dlhc");
thread_create(stack, sizeof(stack), THREAD_PRIORITY_MAIN +1,
           0, thread_isl29020, NULL, "isl29020");
thread_create(stack, sizeof(stack), THREAD_PRIORITY_MAIN +1,
           0, thread_l3g4200d, NULL, "l3g4200d");

/* Add the lps331ap sensor polling endless loop here */
    while (1) {
    uint16_t pres = 0;
    int16_t temp = 0;
    lpsxxx_read_temp(&lpsxxx, &temp);
    lpsxxx_read_pres(&lpsxxx, &pres);
    printf("Pressure: %uhPa, Temperature: %u.%u°C\n",
           pres, (temp / 100), (temp % 100));
    xtimer_sleep(2);
}


return 0;

}

aabadie2

Bonjour,

Il y a une erreur dans le code de lecture du capteur de lumière dans votre deuxième thread:

  • il n’est pas nécessaire d’instancier une nouvelle variable pour le driver. Donc vous pouvez enlever la ligne isl29020_t lumin_value;
  • ensuite dans l’appel de la fonction de lecture isl29020_read, le paramètre à utiliser est &isl29020 car c’est cette instance du driver qui est initialisée, et le crash est provoqué par le fait que lumin_value (dont le nom ne correspond pas à ce que ça doit être) ne l’est pas. La valeur mesurée par le capteur correspond à la valeur de retour de cette fonction et non à son paramètre.
hetour

Merci, J’ai bien fait les changements indiqués. Il y a du mieux mais j’ai un reset de la cible.

Version: 2020.01)
Accelerometer x: 108 y: 128 z: -660
Magnetometer x: 92 y: -142 z: 84
Intensite lumineuse: 0 LUX
Gyro data [dps] - X: 1 Y: 0 Z: 0
Pressure: 984hPa, Temperature: 37.58°C
Accelerometer x: 108 y: 128 z: -660
Magnetometer x: 90 y: -142 z: 85
Intensite lumineuse: 0 LUX
Gyro data [dps] - X: 1 Y: 0 Z: 0
Pressure: 984hPa, Temperature: 37.62°C
*** RIOT kernel panic:
HARD FAULT HANDLER

Voici mon code main

#include <stdio.h>

#include “thread.h”
#include “xtimer.h”

/* Add lps331ap related include here */
#include “lpsxxx.h”
#include “lpsxxx_params.h”

/* Add lsm303dlhc related include here */
#include “lsm303dlhc.h”
#include “lsm303dlhc_params.h”

/* Add isl29020 related include here */
#include “isl29020.h”
#include “isl29020_params.h”

/* Add l3g4200d related include here */
#include “l3g4200d.h”
#include “l3g4200d_params.h”

/* Declare the lps331ap device variable here */
static lpsxxx_t lpsxxx;

/* Declare the lsm303dlhc device variable here */
static lsm303dlhc_t lsm303dlhc;

/* Declare the isl29020 device variable here */
static isl29020_t isl29020;

/* Declare the l3g4200d device variable here */
static l3g4200d_t l3g4200d;

static char stack[THREAD_STACKSIZE_MAIN];

/* declaration du thread pour le lsm303dlhc */

static void *thread_lsm303dlhc(void *arg)
{
(void)arg;

/* Add the lsm303dlhc sensor polling endless loop here */
    while (1) {
    lsm303dlhc_3d_data_t mag_value;
    lsm303dlhc_3d_data_t acc_value;
    lsm303dlhc_read_acc(&lsm303dlhc, &acc_value);
    printf("Accelerometer x: %i y: %i z: %i\n",
           acc_value.x_axis, acc_value.y_axis, acc_value.z_axis);
    lsm303dlhc_read_mag(&lsm303dlhc, &mag_value);
    printf("Magnetometer x: %i y: %i z: %i\n",
           mag_value.x_axis, mag_value.y_axis, mag_value.z_axis);
    xtimer_usleep(500 * US_PER_MS);
}

return 0;

}

/* declaration du thread pour le isl29020 */
static void *thread_isl29020(void *arg)
{
(void)arg;

/* Add the isl29020 sensor polling endless loop here */
    while (1) {
    isl29020_init(&isl29020, &isl29020_params[0]);
    //isl29020_t lumin_value;
    printf("Intensite lumineuse: %5i LUX\n", isl29020_read(&isl29020));
    xtimer_usleep(600 * US_PER_MS);
}

return 0;

}

/* declaration du thread pour le l3g4200d */
static void *thread_l3g4200d (void *arg)
{
(void)arg;

/* Add the l3g4200d  sensor polling endless loop here */
    while (1) {
    //l3g4200d_t dev;
    l3g4200d_data_t acc_data;
    l3g4200d_read(&l3g4200d, &acc_data);
    printf("Gyro data [dps] - X: %6i   Y: %6i   Z: %6i\n",
           acc_data.acc_x, acc_data.acc_y, acc_data.acc_z);
    xtimer_usleep(700 * US_PER_MS);
}

return 0;

}

int main(void)
{
/* Initialize the lps331ap sensor here /
lpsxxx_init(&lpsxxx, &lpsxxx_params[0]);
/
Initialize the lsm303dlhc sensor here /
lsm303dlhc_init(&lsm303dlhc, lsm303dlhc_params);
/
Initialize the isl29020 sensor here /
isl29020_init(&isl29020, &isl29020_params[0]);
/
Initialize the l3g4200d sensor here */
l3g4200d_init(&l3g4200d, &l3g4200d_params[0]);

thread_create(stack, sizeof(stack), THREAD_PRIORITY_MAIN -1,
           0, thread_lsm303dlhc, NULL, "lsm303dlhc");
thread_create(stack, sizeof(stack), THREAD_PRIORITY_MAIN -2,
           0, thread_isl29020, NULL, "isl29020");
thread_create(stack, sizeof(stack), THREAD_PRIORITY_MAIN -3,
           0, thread_l3g4200d, NULL, "l3g4200d");

/* Add the lps331ap sensor polling endless loop here */
    while (1) {
    uint16_t pres = 0;
    int16_t temp = 0;
    lpsxxx_read_temp(&lpsxxx, &temp);
    lpsxxx_read_pres(&lpsxxx, &pres);
    printf("Pressure: %uhPa, Temperature: %u.%u°C\n",
           pres, (temp / 100), (temp % 100));
    xtimer_sleep(2);
}


return 0;

}

aabadie2

En effet, il y avait un autre problème que je n’ai pas vu la première fois: vos 3 thread utilise la même stack et il faut allouer une stack par thread:

static char stack_1[THREAD_STACKSIZE_MAIN];
static char stack_2[THREAD_STACKSIZE_MAIN];
static char stack_3[THREAD_STACKSIZE_MAIN];

[...]

thread_create(stack_1, sizeof(stack_1), THREAD_PRIORITY_MAIN -1,
           0, thread_lsm303dlhc, NULL, "lsm303dlhc");
thread_create(stack_2, sizeof(stack_2), THREAD_PRIORITY_MAIN -2,
           0, thread_isl29020, NULL, "isl29020");
thread_create(stack_3, sizeof(stack_3), THREAD_PRIORITY_MAIN -3,
           0, thread_l3g4200d, NULL, "l3g4200d");
hetour

Merci, je vais essayer ça me semble plus logique maintenant mais c’était difficile à trouver dans le cours ou dans les tuto.