You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					214 lines
				
				5.8 KiB
			
		
		
			
		
	
	
					214 lines
				
				5.8 KiB
			| 
											5 years ago
										 | #include <sys/resource.h>
 | ||
|  | 
 | ||
| 
											5 years ago
										 | #include <chrono>
 | ||
|  | #include <thread>
 | ||
|  | #include <vector>
 | ||
| 
											3 years ago
										 | #include <map>
 | ||
| 
											3 years ago
										 | #include <poll.h>
 | ||
|  | #include <linux/gpio.h>
 | ||
| 
											5 years ago
										 | 
 | ||
| 
											5 years ago
										 | #include "cereal/messaging/messaging.h"
 | ||
| 
											4 years ago
										 | #include "common/i2c.h"
 | ||
|  | #include "common/swaglog.h"
 | ||
|  | #include "common/timing.h"
 | ||
|  | #include "common/util.h"
 | ||
| 
											3 years ago
										 | #include "system/sensord/sensors/bmx055_accel.h"
 | ||
|  | #include "system/sensord/sensors/bmx055_gyro.h"
 | ||
|  | #include "system/sensord/sensors/bmx055_magn.h"
 | ||
|  | #include "system/sensord/sensors/bmx055_temp.h"
 | ||
|  | #include "system/sensord/sensors/constants.h"
 | ||
|  | #include "system/sensord/sensors/light_sensor.h"
 | ||
|  | #include "system/sensord/sensors/lsm6ds3_accel.h"
 | ||
|  | #include "system/sensord/sensors/lsm6ds3_gyro.h"
 | ||
|  | #include "system/sensord/sensors/lsm6ds3_temp.h"
 | ||
|  | #include "system/sensord/sensors/mmc5603nj_magn.h"
 | ||
|  | #include "system/sensord/sensors/sensor.h"
 | ||
| 
											5 years ago
										 | 
 | ||
|  | #define I2C_BUS_IMU 1
 | ||
|  | 
 | ||
| 
											5 years ago
										 | ExitHandler do_exit;
 | ||
| 
											3 years ago
										 | uint64_t init_ts = 0;
 | ||
|  | 
 | ||
| 
											3 years ago
										 | void interrupt_loop(std::vector<Sensor *>& sensors,
 | ||
|  |                     std::map<Sensor*, std::string>& sensor_service)
 | ||
|  | {
 | ||
|  |   PubMaster pm_int({"gyroscope", "accelerometer"});
 | ||
|  | 
 | ||
|  |   int fd = sensors[0]->gpio_fd;
 | ||
| 
											3 years ago
										 |   struct pollfd fd_list[1] = {0};
 | ||
|  |   fd_list[0].fd = fd;
 | ||
|  |   fd_list[0].events = POLLIN | POLLPRI;
 | ||
|  | 
 | ||
|  |   while (!do_exit) {
 | ||
|  |     int err = poll(fd_list, 1, 100);
 | ||
|  |     if (err == -1) {
 | ||
|  |       if (errno == EINTR) {
 | ||
|  |         continue;
 | ||
|  |       }
 | ||
|  |       return;
 | ||
|  |     } else if (err == 0) {
 | ||
|  |       LOGE("poll timed out");
 | ||
|  |       continue;
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     if ((fd_list[0].revents & (POLLIN | POLLPRI)) == 0) {
 | ||
|  |       LOGE("no poll events set");
 | ||
|  |       continue;
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     // Read all events
 | ||
|  |     struct gpioevent_data evdata[16];
 | ||
|  |     err = read(fd, evdata, sizeof(evdata));
 | ||
|  |     if (err < 0 || err % sizeof(*evdata) != 0) {
 | ||
|  |       LOGE("error reading event data %d", err);
 | ||
|  |       continue;
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     int num_events = err / sizeof(*evdata);
 | ||
| 
											3 years ago
										 |     uint64_t offset = nanos_since_epoch() - nanos_since_boot();
 | ||
| 
											3 years ago
										 |     uint64_t ts = evdata[num_events - 1].timestamp - offset;
 | ||
|  | 
 | ||
|  |     for (Sensor *sensor : sensors) {
 | ||
| 
											3 years ago
										 |       MessageBuilder msg;
 | ||
|  |       if (!sensor->get_event(msg, ts)) {
 | ||
| 
											3 years ago
										 |         continue;
 | ||
|  |       }
 | ||
|  | 
 | ||
| 
											3 years ago
										 |       if (!sensor->is_data_valid(init_ts, ts)) {
 | ||
|  |         continue;
 | ||
|  |       }
 | ||
| 
											3 years ago
										 | 
 | ||
| 
											3 years ago
										 |       pm_int.send(sensor_service[sensor].c_str(), msg);
 | ||
| 
											3 years ago
										 |     }
 | ||
| 
											3 years ago
										 |   }
 | ||
|  | 
 | ||
|  |   // poweroff sensors, disable interrupts
 | ||
|  |   for (Sensor *sensor : sensors) {
 | ||
|  |     sensor->shutdown();
 | ||
|  |   }
 | ||
|  | }
 | ||
| 
											5 years ago
										 | 
 | ||
| 
											3 years ago
										 | int sensor_loop(I2CBus *i2c_bus_imu) {
 | ||
| 
											5 years ago
										 |   BMX055_Accel bmx055_accel(i2c_bus_imu);
 | ||
|  |   BMX055_Gyro bmx055_gyro(i2c_bus_imu);
 | ||
|  |   BMX055_Magn bmx055_magn(i2c_bus_imu);
 | ||
|  |   BMX055_Temp bmx055_temp(i2c_bus_imu);
 | ||
|  | 
 | ||
| 
											3 years ago
										 |   LSM6DS3_Accel lsm6ds3_accel(i2c_bus_imu, GPIO_LSM_INT);
 | ||
|  |   LSM6DS3_Gyro lsm6ds3_gyro(i2c_bus_imu, GPIO_LSM_INT, true); // GPIO shared with accel
 | ||
| 
											5 years ago
										 |   LSM6DS3_Temp lsm6ds3_temp(i2c_bus_imu);
 | ||
|  | 
 | ||
| 
											4 years ago
										 |   MMC5603NJ_Magn mmc5603nj_magn(i2c_bus_imu);
 | ||
|  | 
 | ||
| 
											5 years ago
										 |   LightSensor light("/sys/class/i2c-adapter/i2c-2/2-0038/iio:device1/in_intensity_both_raw");
 | ||
| 
											5 years ago
										 | 
 | ||
| 
											3 years ago
										 |   std::map<Sensor*, std::string> sensor_service = {
 | ||
|  |     {&bmx055_accel, "accelerometer2"},
 | ||
|  |     {&bmx055_gyro, "gyroscope2"},
 | ||
|  |     {&bmx055_magn, "magnetometer"},
 | ||
|  |     {&bmx055_temp, "temperatureSensor"},
 | ||
|  | 
 | ||
|  |     {&lsm6ds3_accel, "accelerometer"},
 | ||
|  |     {&lsm6ds3_gyro, "gyroscope"},
 | ||
|  |     {&lsm6ds3_temp, "temperatureSensor"},
 | ||
|  | 
 | ||
|  |     {&mmc5603nj_magn, "magnetometer"},
 | ||
|  |     {&light, "lightSensor"}
 | ||
|  |   };
 | ||
|  | 
 | ||
| 
											5 years ago
										 |   // Sensor init
 | ||
| 
											4 years ago
										 |   std::vector<std::pair<Sensor *, bool>> sensors_init; // Sensor, required
 | ||
| 
											4 years ago
										 |   sensors_init.push_back({&bmx055_accel, false});
 | ||
|  |   sensors_init.push_back({&bmx055_gyro, false});
 | ||
|  |   sensors_init.push_back({&bmx055_magn, false});
 | ||
|  |   sensors_init.push_back({&bmx055_temp, false});
 | ||
| 
											5 years ago
										 | 
 | ||
| 
											4 years ago
										 |   sensors_init.push_back({&lsm6ds3_accel, true});
 | ||
|  |   sensors_init.push_back({&lsm6ds3_gyro, true});
 | ||
|  |   sensors_init.push_back({&lsm6ds3_temp, true});
 | ||
| 
											5 years ago
										 | 
 | ||
| 
											4 years ago
										 |   sensors_init.push_back({&mmc5603nj_magn, false});
 | ||
| 
											5 years ago
										 | 
 | ||
| 
											4 years ago
										 |   sensors_init.push_back({&light, true});
 | ||
| 
											5 years ago
										 | 
 | ||
| 
											4 years ago
										 |   bool has_magnetometer = false;
 | ||
| 
											4 years ago
										 | 
 | ||
|  |   // Initialize sensors
 | ||
|  |   std::vector<Sensor *> sensors;
 | ||
| 
											3 years ago
										 |   for (auto &[sensor, required] : sensors_init) {
 | ||
|  |     int err = sensor->init();
 | ||
| 
											4 years ago
										 |     if (err < 0) {
 | ||
| 
											3 years ago
										 |       if (required) {
 | ||
| 
											4 years ago
										 |         LOGE("Error initializing sensors");
 | ||
|  |         return -1;
 | ||
|  |       }
 | ||
|  |     } else {
 | ||
| 
											3 years ago
										 | 
 | ||
|  |       if (sensor == &bmx055_magn || sensor == &mmc5603nj_magn) {
 | ||
| 
											4 years ago
										 |         has_magnetometer = true;
 | ||
|  |       }
 | ||
| 
											3 years ago
										 | 
 | ||
| 
											3 years ago
										 |       if (!sensor->has_interrupt_enabled()) {
 | ||
|  |         sensors.push_back(sensor);
 | ||
| 
											3 years ago
										 |       }
 | ||
| 
											5 years ago
										 |     }
 | ||
|  |   }
 | ||
|  | 
 | ||
| 
											4 years ago
										 |   if (!has_magnetometer) {
 | ||
|  |     LOGE("No magnetometer present");
 | ||
|  |     return -1;
 | ||
|  |   }
 | ||
|  | 
 | ||
| 
											3 years ago
										 |   // increase interrupt quality by pinning interrupt and process to core 1
 | ||
|  |   setpriority(PRIO_PROCESS, 0, -18);
 | ||
|  |   util::set_core_affinity({1});
 | ||
|  |   std::system("sudo su -c 'echo 1 > /proc/irq/336/smp_affinity_list'");
 | ||
|  | 
 | ||
| 
											3 years ago
										 |   PubMaster pm_non_int({"gyroscope2", "accelerometer2", "temperatureSensor",
 | ||
|  |                         "lightSensor", "magnetometer"});
 | ||
| 
											3 years ago
										 |   init_ts = nanos_since_boot();
 | ||
| 
											5 years ago
										 | 
 | ||
| 
											3 years ago
										 |   // thread for reading events via interrupts
 | ||
|  |   std::vector<Sensor *> lsm_interrupt_sensors = {&lsm6ds3_accel, &lsm6ds3_gyro};
 | ||
| 
											3 years ago
										 |   std::thread lsm_interrupt_thread(&interrupt_loop, std::ref(lsm_interrupt_sensors),
 | ||
|  |                                    std::ref(sensor_service));
 | ||
| 
											3 years ago
										 | 
 | ||
|  |   // polling loop for non interrupt handled sensors
 | ||
| 
											4 years ago
										 |   while (!do_exit) {
 | ||
| 
											5 years ago
										 |     std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
 | ||
| 
											5 years ago
										 | 
 | ||
| 
											3 years ago
										 |     for (Sensor *sensor : sensors) {
 | ||
|  |       MessageBuilder msg;
 | ||
|  |       if (!sensor->get_event(msg)) {
 | ||
|  |         continue;
 | ||
|  |       }
 | ||
| 
											5 years ago
										 | 
 | ||
| 
											3 years ago
										 |       if (!sensor->is_data_valid(init_ts, nanos_since_boot())) {
 | ||
|  |         continue;
 | ||
|  |       }
 | ||
| 
											3 years ago
										 | 
 | ||
| 
											3 years ago
										 |       pm_non_int.send(sensor_service[sensor].c_str(), msg);
 | ||
| 
											3 years ago
										 |     }
 | ||
| 
											5 years ago
										 | 
 | ||
| 
											5 years ago
										 |     std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
 | ||
|  |     std::this_thread::sleep_for(std::chrono::milliseconds(10) - (end - begin));
 | ||
| 
											5 years ago
										 |   }
 | ||
| 
											3 years ago
										 | 
 | ||
| 
											3 years ago
										 |   for (Sensor *sensor : sensors) {
 | ||
| 
											3 years ago
										 |     sensor->shutdown();
 | ||
|  |   }
 | ||
|  | 
 | ||
| 
											3 years ago
										 |   lsm_interrupt_thread.join();
 | ||
| 
											5 years ago
										 |   return 0;
 | ||
|  | }
 | ||
|  | 
 | ||
|  | int main(int argc, char *argv[]) {
 | ||
| 
											3 years ago
										 |   try {
 | ||
|  |     auto i2c_bus_imu = std::make_unique<I2CBus>(I2C_BUS_IMU);
 | ||
|  |     return sensor_loop(i2c_bus_imu.get());
 | ||
|  |   } catch (std::exception &e) {
 | ||
|  |     LOGE("I2CBus init failed");
 | ||
|  |     return -1;
 | ||
|  |   }
 | ||
| 
											5 years ago
										 | }
 |