Skip to content

Integrate CCS811 sensor + update code #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
116 changes: 47 additions & 69 deletions src/telaire_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,28 @@
#include "ros/ros.h"
#include "std_msgs/String.h"

const int CO2_ADDR = 0x5A; // default I2C slave address of CCS811
const char *CO2_DEV = "/dev/i2c-8"; // default I2C device file (check Jetson wiring)
const int CO2_ADDR = 0x5A; // adresse par défaut du I2C slave de CCS811
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

J'apprécie les commentaires, mais est-ce que ça dérange de les écrire en anglais ? L'idée d'avoir du franglais dans le code, mais plait plus ou moins

const char *CO2_DEV = "/dev/i2c-8"; // fichier dev par défaut pour le I2C (vérifier branchdement du Jetson)

/*I2C ADDRESS*/
const int CCS811_I2C_ADDRESS1 = 0x5A;
const int CCS811_I2C_ADDRESS2 = 0x5B;

const int CCS811_REG_STATUS = 0x00;
/******* ADRESSES I2C *******
* Tiré de https://www.dropbox.com/sh/or1jzflapzbdepd/AAAGrCZgyjPOtNyLYNcyzL90a/Libraries/CCS811?dl=0&preview=CCS811.h&subfolder_nav_tracking=1
*/
const int CCS811_REG_MEAS_MODE = 0x01;
const int CCS811_REG_ALG_RESULT_DATA = 0x02;
const int CCS811_REG_RAW_DATA = 0x03;
const int CCS811_REG_ENV_DATA = 0x05;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cette constant là est utilisée nulle part, je pense qu'on peut l'enlever.

const int CCS811_REG_NTC = 0x06;
const int CCS811_REG_THRESHOLDS = 0x10;
const int CCS811_REG_BASELINE = 0x11;
const int CCS811_REG_HW_ID = 0x20;
const int CCS811_REG_HW_VERSION = 0x21;
const int CCS811_REG_FW_BOOT_VERSION = 0x23;
const int CCS811_REG_FW_APP_VERSION = 0x24;
const int CCS811_REG_INTERNAL_STATE = 0xA0;
const int CCS811_REG_ERROR_ID = 0xE0;
const int CCS811_REG_SW_RESET = 0xFF;

const int CCS811_BOOTLOADER_APP_ERASE = 0xF1;
const int CCS811_BOOTLOADER_APP_DATA = 0xF2;
const int CCS811_BOOTLOADER_APP_VERIFY = 0xF3;
// Constantes pour le soft reset
const uint8_t CCS811_REG_BASELINE = 0x11;
const int CCS811_REG_SW_RESET = 0xFF;
const uint8_t CCS811_REG_RESET_INDEX2 = 0xE5;
const uint8_t CCS811_REG_RESET_INDEX3 = 0x72;
const uint8_t CCS811_REG_RESET_INDEX4 = 0x8A;
const int CCS811_BOOTLOADER_APP_START = 0xF4;

const int CCS811_HW_ID = 0x81;

/*
* Voir https://www.dropbox.com/sh/or1jzflapzbdepd/AAAGrCZgyjPOtNyLYNcyzL90a/Libraries/CCS811?dl=0&preview=CCS811.h&subfolder_nav_tracking=1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P-e qu'on pourrait les archiver dans le Teams, le lien dropbox ce n'est pas trop fiable et ça ne perdure pas trop longtemps.

* pour plus de Modes si nécessaire
*/
namespace DRIVE_MODE_t
{
enum Mode
Expand All @@ -55,20 +47,12 @@ namespace DRIVE_MODE_t
};
}

namespace Cycles
{
enum Cycle
{
Closed, //Idle (Measurements are disabled in this mode)
Cycle_1s, //Constant power mode, IAQ measurement every second
Cycle_10s, //Pulse heating mode IAQ measurement every 10 seconds
Cycle_60s, //Low power pulse heating mode IAQ measurement every 60 seconds
Cycle_250s //Constant power mode, sensor measurement every 250ms 1xx: Reserved modes (For future use)
};
}

/*
* Tiré de https://www.dropbox.com/sh/or1jzflapzbdepd/AAAGrCZgyjPOtNyLYNcyzL90a/Libraries/CCS811?dl=0&preview=CCS811.cpp&subfolder_nav_tracking=1
*/
int main(int argc, char *argv[])
{

//Initialisation de ROS pour faire un talker
ros::init(argc, argv, "co2_node");
ros::NodeHandle nh;
Expand All @@ -83,85 +67,79 @@ int main(int argc, char *argv[])

if ((file = open(filename.c_str(), O_RDWR)) < 0)
{
ROS_INFO("Failed to open the bus.");
/* ERROR HANDLING; you can check errno to see what went wrong */
ROS_INFO("Impossible d\'ouvrir le bus...");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On pourrait utiliser ROS_ERROR ici ça la place d'un ROS_INFO. Et même chose pour les places où qu'on fait un exit(1).

/* ERROR HANDLING; vérifier errno pour voir le problème */
exit(1);
}

if (ioctl(file, I2C_SLAVE, CO2_ADDR) < 0)
{
ROS_INFO("Failed to acquire bus access and/or talk to slave.\n");
ROS_INFO("Impossible d\'acceder au bus et/ou de parler au slave.\n");

exit(1);
}

// STARTING SETUP
ROS_INFO("Starting setup\n");
// DÉBUT INITIALISATION
ROS_INFO(" *** Debut initialisation *** \n");
// Soft reset
uint8_t buffer_soft_reset[5] = {CCS811_REG_SW_RESET, 0x11, 0xE5, 0x72, 0x8A};
uint8_t buffer_soft_reset[5] = {CCS811_REG_SW_RESET, CCS811_REG_BASELINE, CCS811_REG_RESET_INDEX2, CCS811_REG_RESET_INDEX3, CCS811_REG_RESET_INDEX4};
if (write(file, buffer_soft_reset, 5) != 5)
{
ROS_INFO("Failed to write SOFT RESET to I2C bus \n");
ROS_INFO("Impossible d\'envoyer le SOFT RESET au bus I2C.\n");
}
else
{
ROS_INFO("Sent soft reset\n");
ROS_INFO("SOFT RESET envoye.\n");
std::this_thread::sleep_for(std::chrono::milliseconds(1));
ROS_INFO("Soft reset completed\n");
ROS_INFO("SOFT RESET complete.\n");
}

// Bootloader start
// Bootloader
uint8_t buffer_bootloader[1] = {CCS811_BOOTLOADER_APP_START};
if (write(file, buffer_bootloader, 1) != 1)
{
ROS_INFO("Failed to write BOOTLOADER SETUP to I2C bus \n");
ROS_INFO("Impossible d'ecrire l\'initialisation du BOOTLOADER au bus I2C.\n");
}
else
{
ROS_INFO("Sent bootloader setup\n");
ROS_INFO("Initialisation du BOOTLOADER envoye.\n");
}

// Set Measurement Mode
// Mode de mesure - Measurement Mode
uint8_t measurement[1] = {0};
measurement[0] = (0 << 2) | (0 << 3) | (DRIVE_MODE_t::Mode4 << 4);
ROS_INFO("Measurement mode set");
measurement[0] = 0 | (DRIVE_MODE_t::Mode4 << 4);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ouin, c'est mieux, mais pas tout à fait exacte, en fait tu peux juste enlever le 0. Ça va rien changer à la logique et ça va enlever des opérations inutiles.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Je te conseille fortement de comprendre le code avant de le considérer comme acceptable pour une PR. C'est vraiment pas une bonne idée de pas comprendre ce qui se passe, surtout dans un contexte de système embarqué.

ROS_INFO("MEASUREMENT MODE mis en place.");
uint8_t buffer_measurement_mode[2] = {CCS811_REG_MEAS_MODE, measurement[0]};
if (write(file, buffer_measurement_mode, 2) != 2)
{
ROS_INFO("Failed to write MEASUREMENT MODE to I2C bus \n");
}
else
{
ROS_INFO("Sent Measurement mode\n");
}

// Set Temperature and Humidity
double buffer_set_temp_hum[5] = {CCS811_REG_ENV_DATA, 50.5, 0, 24.5, 0};
if (write(file, buffer_set_temp_hum, 5) != 5)
{
ROS_INFO("Failed to write TEMP AND HUM to I2C bus \n");
ROS_INFO("Impossible d\'ecrire le MEASUREMENT MODE au bus I2C.\n");
}
else
{
ROS_INFO("Set Temp and Hum\n");
ROS_INFO("MEASUREMENT MODE envoye.\n");
}

// FIN SETUP
ROS_INFO("End setup\n");
// FIN INITIALISATION
ROS_INFO(" *** Fin initialistion *** \n");
uint8_t buffer_fetch_value[2] = {CCS811_REG_ALG_RESULT_DATA, 1};
char buffer_read[8];
ROS_INFO("Sending request for results\n");
ROS_INFO("Envoi de la requete pour recolter les donnees...\n");


while (true)
{
if (write(file, buffer_fetch_value, 2) != 2)
{
ROS_INFO("Failed to write REQUEST FOR DATA to I2C bus \n");
ROS_INFO("Impossible d\'envoyer la requete au bus I2C pour récolter les donnees.\n");
}
else
{
usleep(100000);
read(file, buffer_read, 8);
if (log_measurement_enabled == true)
{
ROS_INFO("buffer_read value: %d, %d, %d, %d, %d, %d, %d, %d \n", buffer_read[0], buffer_read[1], buffer_read[2], buffer_read[3], buffer_read[4], buffer_read[5], buffer_read[6], buffer_read[7]);
}

ppmReading = (((uint16_t)buffer_read[0] << 8) | (uint16_t)buffer_read[1]);

std_msgs::String msg;
Expand All @@ -176,4 +154,4 @@ int main(int argc, char *argv[])
}

return EXIT_SUCCESS;
}
}