void VoltageReader::run() { cs::SensorData sensorMessage; sensorMessage.set_sensor_id((int) SensorIds::Voltage_sensor); sensorMessage.set_sensor_desc(VOLTAGE_SENSOR_NAME); auto values = sensorMessage.mutable_sensor_values(); auto voltageData = values->Add(); auto currentData = values->Add(); voltageData->set_associated_name(VOLTAGE_PARAM_VOLTAGE); voltageData->set_data_type(cs::REAL); currentData->set_associated_name(VOLTAGE_PARAM_CURRENT); currentData->set_data_type(cs::REAL); float prevVoltageData = 0, prevCurrentData = 0; auto delayTime = std::chrono::milliseconds(sensorInfo.measurementRate); auto t = std::chrono::high_resolution_clock::now(); while (!stopVariable) { pc::ADCReader::ADCValue adcValue; adcReader.read(adcValue); float voltage = adcValue.adcVoltages[voltageChannel] * sensorInfo.voltageScale; voltage = (voltage + prevVoltageData) / 2; prevVoltageData = voltage; float current = (adcValue.adcVoltages[currentChannel] - sensorInfo.currentOffset) / sensorInfo.currentScale; current = (current + prevCurrentData) / 2; prevCurrentData = current; voltageData->set_real_value(voltage); currentData->set_real_value(current); int messageSize = sensorMessage.ByteSize(); std::vector<int8_t> outArray(messageSize); sensorMessage.SerializeToArray(&outArray[0], messageSize); sendData(outArray); std::this_thread::sleep_until(t + delayTime); t += delayTime; } }
void IMUReader::run() { initSensors(); cs::SensorData sensorMessage; sensorMessage.set_sensor_id((int) SensorIds::IMU_sensor); sensorMessage.set_sensor_desc(IMU_SENSOR_NAME); auto values = sensorMessage.mutable_sensor_values(); auto gyroData = values->Add(); gyroData->set_associated_name("gyro_data"); gyroData->set_data_type(cs::COORD_3D); auto gyroCoords = gyroData->mutable_coord_value(); int64_t loopCount = 0; GyroState gyroState = { 0 }; bool calibration = true; int64_t calibrationDelay = 500; AHRSProcessor ahrsProcessor(100); auto instance = ConfigManager::getInstance(); if (!instance) { COMM_EXCEPTION(NullPointerException, "Can't get instance of " "configuration manager"); } float pitch, roll, yaw; QUATERNION offsetQ; showCalibration(true); auto delayTime = std::chrono::milliseconds(10); auto t = std::chrono::high_resolution_clock::now(); #if WRITE_SENSORS_DATA std::ofstream stream("raw_data.txt"); #endif while (!stopVariable) { IMUSensorsData sensorsData = { 0 }; readSensors(sensorsData, gyroState, calibration); std::this_thread::sleep_until(t + delayTime); t += delayTime; #if WRITE_SENSORS_DATA stream << sensorsData.rawAccelX << " " << sensorsData.rawAccelY << " " << sensorsData.rawAccelZ << " " << sensorsData.rawGyroX << " " << sensorsData.rawGyroY << " " << sensorsData.rawGyroZ << std::endl; #endif if (!calibration) { ahrsProcessor.updateState(sensorsData, roll, pitch, yaw); putCurrentAngles(roll, pitch, yaw); } else { IMUSensorsData tmpData = { 0 }; tmpData.rawAccelX = sensorsData.rawAccelX; tmpData.rawAccelY = sensorsData.rawAccelY; tmpData.rawAccelZ = sensorsData.rawAccelZ; ahrsProcessor.updateState(tmpData, offsetQ); } gyroCoords->set_x(roll); gyroCoords->set_y(pitch); gyroCoords->set_z(yaw); int messageSize = sensorMessage.ByteSize(); std::vector<int8_t> outArray(messageSize); sensorMessage.SerializeToArray(&outArray[0], messageSize); if (!calibration && loopCount % 10 == 0) { //std::cout << sensorsData.rawCompassX << " " << sensorsData.rawCompassY << " " << // sensorsData.rawCompassZ << std::endl; sendData(outArray); } loopCount++; if (loopCount == calibrationDelay) { calibration = false; gyroState.offsetX /= calibrationDelay; gyroState.offsetY /= calibrationDelay; gyroState.offsetZ /= calibrationDelay; ahrsProcessor.setGyroOffsets(gyroState.offsetX, gyroState.offsetY, gyroState.offsetZ); ahrsProcessor.setOffsetQuaternion(offsetQ); showCalibration(false); } } #if WRITE_SENSORS_DATA stream.close(); #endif }