SignalK-Orientation 1.0.0
Orientation output in Signal K format for ESP32
Loading...
Searching...
No Matches
orientation_sensor.cpp
Go to the documentation of this file.
1
5#define LOG_LOCAL_LEVEL ESP_LOG_INFO
6
7#ifndef TAG
8//avoid conflicts if TAG (used by logging fcns to indicate source) is defined elsewhere.
9static const char* TAG = "orientation_sensor.cpp";
10#endif
11
12#include "esp_log.h"
13
14#include "orientation_sensor.h"
15
16
27OrientationSensor::OrientationSensor(uint8_t pin_i2c_sda, uint8_t pin_i2c_scl,
28 uint8_t accel_mag_i2c_addr,
29 uint8_t gyro_i2c_addr) {
30 sensor_interface_ = new SensorFusion(); // create our fusion engine instance
31
32 bool success;
33 // init IO subsystem, passing NULLs since we use Signal-K output instead.
34 success =
35 sensor_interface_->InitializeInputOutputSubsystem(NULL, NULL) &&
36 // connect to the sensors. Accelerometer and magnetometer are in same IC.
37 sensor_interface_->InstallSensor(accel_mag_i2c_addr,
38 SensorType::kMagnetometer) &&
39 sensor_interface_->InstallSensor(accel_mag_i2c_addr,
40 SensorType::kAccelerometer) &&
41 // A thermometer (uncalibrated) is available in the
42 // accelerometer/magnetometer IC.
43 sensor_interface_->InstallSensor(accel_mag_i2c_addr,
44 SensorType::kThermometer) &&
45 sensor_interface_->InstallSensor(gyro_i2c_addr, SensorType::kGyroscope);
46 if (!success) {
47 //debugE("Trouble installing sensors.");
48 // ...
49 ESP_LOGE(TAG, "Trouble installing sensors.");
50 } else {
51 sensor_interface_->Begin(pin_i2c_sda, pin_i2c_scl);
52 //debugI("Sensors connected & Fusion ready");
53 ESP_LOGI(TAG, "Sensors connected & Fusion ready");
54
55 // The Fusion Library, in build.h, defines how fast the ICs generate new
56 // orientation data and how fast the fusion algorithm runs, using FUSION_HZ.
57 // Usually this rate should be the same as ReadAndProcessSensors() is
58 // called.
59 // We rely on the main program using this library to call ReadAndProcessSensors()
60 // at the rate given by a call to GetFusionRateHz().
61 // Calls to fetch the Orientation values then should run at not faster than the
62 // rate at which ReadAndProcessSensors() is called. e.g. retrieving attitude values
63 // at 10 Hz is fine when the fusion rate is 40 Hz, but retrieving attitude values
64 // at 50 Hz would result in duplicate values.
65 }
66
67} // end OrientationSensor()
68
73 return FUSION_HZ;
74
75} // end ReadAndProcessSensors()
76
81 sensor_interface_->ReadSensors();
82 sensor_interface_->RunFusion();
83
84} // end ReadAndProcessSensors()
85
86
94static const char SCHEMA[] PROGMEM = R"###({
95 "type": "object",
96 "properties": {
97 "report_interval": {
98 "title": "Report Interval",
99 "type": "number",
100 "description": "Milliseconds between outputs of this parameter"
101 },
102 "save_mag_cal": {
103 "title": "Save Magnetic Cal",
104 "type": "number",
105 "description": "Set to 1 to save current magnetic calibration"
106 }
107 }
108 })###";
109
110
111OrientationValues::OrientationValues(OrientationSensor* orientation_sensor,
112 OrientationValType val_type)
113 {
114 orientation_sensor_ = orientation_sensor;
115 value_type_ = val_type;
116 throttlePrint_ = 0;
117 //load_configuration();
118
119 save_mag_cal_ = 0;
120
121} // end OrientationValues()
123 //check whether magnetic calibration has been requested to be saved or deleted
124 if( 1 == save_mag_cal_ ) {
125 orientation_sensor_->sensor_interface_->InjectCommand("SVMC");
126 }else if( -1 == save_mag_cal_ ) {
127 orientation_sensor_->sensor_interface_->InjectCommand("ERMC");
128 }
129 save_mag_cal_ = 0; // set flag back to zero so we don't repeat save/delete
130 //check which type of parameter is requested, and pass it on
131
132 float output = 0.0;
133
134 switch (value_type_) {
135 case (kCompassHeading):
136 case (kYaw):
137 output = orientation_sensor_->sensor_interface_->GetHeadingRadians();
138 throttlePrint_++;
139 if( (throttlePrint_ % 50) == 0 )
140 { ESP_LOGI(TAG, "Yaw: %f", output);
141 }
142 break;
143 case (kRoll):
144 output = orientation_sensor_->sensor_interface_->GetRollRadians();
145 break;
146 case (kPitch):
147 output = orientation_sensor_->sensor_interface_->GetPitchRadians();
148 break;
149 case (kAccelerationX):
150 output = orientation_sensor_->sensor_interface_->GetAccelXMPerSS();
151 break;
152 case (kAccelerationY):
153 output = orientation_sensor_->sensor_interface_->GetAccelYMPerSS();
154 break;
155 case (kAccelerationZ):
156 output = orientation_sensor_->sensor_interface_->GetAccelZMPerSS();
157 break;
158 case (kRateOfTurn):
159 output = orientation_sensor_->sensor_interface_->GetTurnRateRadPerS();
160 break;
161 case (kRateOfPitch):
162 output = orientation_sensor_->sensor_interface_->GetPitchRateRadPerS();
163 break;
164 case (kRateOfRoll):
165 output = orientation_sensor_->sensor_interface_->GetRollRateRadPerS();
166 break;
167 case (kTemperature):
168 output = orientation_sensor_->sensor_interface_->GetTemperatureK();
169 break;
170 case (kMagCalFitInUse):
171 output = orientation_sensor_->sensor_interface_->GetMagneticFitError();
172 break;
173 case (kMagCalFitTrial):
174 output = orientation_sensor_->sensor_interface_->GetMagneticFitErrorTrial();
175 break;
177 output = orientation_sensor_->sensor_interface_->GetMagneticCalSolver();
178 break;
179 case (kMagInclination):
180 output = orientation_sensor_->sensor_interface_->GetMagneticInclinationRad();
181 break;
182 case (kMagFieldMagnitude):
183 //TODO report in T rather than uT, however need widget to be able to display
184 output = orientation_sensor_->sensor_interface_->GetMagneticBMag();
185 break;
187 //TODO report in T rather than uT, however need widget to be able to display
188 output = orientation_sensor_->sensor_interface_->GetMagneticBMagTrial();
189 break;
190 case (kMagNoiseCovariance):
191 output = orientation_sensor_->sensor_interface_->GetMagneticNoiseCovariance();
192 break;
193 default:
194 break;
195 }
196 return output;
197} // end Update()
OrientationSensor represents a 9-Degrees-of-Freedom sensor (magnetometer, accelerometer,...
int GetFusionRateHz(void)
Return how fast we read the physical sensor and run the fusion algorithm.
SensorFusion * sensor_interface_
sensor's Fusion Library interface
void ReadAndProcessSensors(void)
reads sensor hardware and runs fusion algorithm
OrientationSensor(uint8_t pin_i2c_sda, uint8_t pin_i2c_scl, uint8_t accel_mag_i2c_addr, uint8_t gyro_i2c_addr)
Constructor sets up the I2C communications to the sensor and initializes the sensor fusion library.
@ kMagCalFitInUse
fit of currently-used calibration. <3.5 is good.
@ kRateOfTurn
rate of change of compass heading
@ kMagFieldMagnitudeTrial
geomagnetic magnitude based on current readings
@ kCompassHeading
compass heading, also called yaw
@ kYaw
rotation about the vertical axis
@ kMagCalAlgorithmSolver
cal algorithm order used. [0,4,7,10] 10 is best
@ kAccelerationX
acceleration in the stern-to-bow axis
@ kRateOfRoll
rate of change of roll
@ kMagCalFitTrial
fit of candidate calibration. <3.5 is good.
@ kAccelerationZ
acceleration in the down-to-up axis
@ kRateOfPitch
rate of change of pitch
@ kMagNoiseCovariance
deviation of current reading from calibrated geomag sphere
@ kPitch
rotation about the transverse axis
@ kMagFieldMagnitude
geomagnetic magnitude of current calibration
@ kAccelerationY
acceleration in the starboard-to-port axis
@ kRoll
rotation about the longitudinal axis
@ kTemperature
temperature as reported by sensor IC
@ kMagInclination
geomagnetic inclination based on current readings
OrientationValType value_type_
Particular type of orientation parameter supplied.
float ReportValue(void)
fetches current orientation parameter
OrientationSensor * orientation_sensor_
Pointer to the orientation sensor.
int8_t save_mag_cal_
Flag for saving current magnetic calibration.
Orientation sensor interface to SensESP.