Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Ardupilot
GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_Compass/AP_Compass_DroneCAN.cpp
9460 views
1
/*
2
This program is free software: you can redistribute it and/or modify
3
it under the terms of the GNU General Public License as published by
4
the Free Software Foundation, either version 3 of the License, or
5
(at your option) any later version.
6
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
11
12
You should have received a copy of the GNU General Public License
13
along with this program. If not, see <http://www.gnu.org/licenses/>.
14
*/
15
16
#include "AP_Compass_DroneCAN.h"
17
18
#if AP_COMPASS_DRONECAN_ENABLED
19
20
#include <AP_HAL/AP_HAL.h>
21
22
#include <AP_CANManager/AP_CANManager.h>
23
#include <AP_DroneCAN/AP_DroneCAN.h>
24
#include <AP_BoardConfig/AP_BoardConfig.h>
25
#include <AP_Logger/AP_Logger.h>
26
#include <SITL/SITL.h>
27
28
#define LOG_TAG "COMPASS"
29
30
AP_Compass_DroneCAN::DetectedModules AP_Compass_DroneCAN::_detected_modules[];
31
HAL_Semaphore AP_Compass_DroneCAN::_sem_registry;
32
33
AP_Compass_DroneCAN::AP_Compass_DroneCAN(AP_DroneCAN* ap_dronecan, uint32_t devid) :
34
_devid(devid)
35
{
36
}
37
38
bool AP_Compass_DroneCAN::subscribe_msgs(AP_DroneCAN* ap_dronecan)
39
{
40
const auto driver_index = ap_dronecan->get_driver_index();
41
42
return (Canard::allocate_sub_arg_callback(ap_dronecan, &handle_magnetic_field, driver_index) != nullptr)
43
&& (Canard::allocate_sub_arg_callback(ap_dronecan, &handle_magnetic_field_2, driver_index) != nullptr)
44
#if AP_COMPASS_DRONECAN_HIRES_ENABLED
45
&& (Canard::allocate_sub_arg_callback(ap_dronecan, &handle_magnetic_field_hires, driver_index) != nullptr)
46
#endif
47
;
48
}
49
50
AP_Compass_Backend* AP_Compass_DroneCAN::probe(uint8_t index)
51
{
52
AP_Compass_DroneCAN* driver = nullptr;
53
if (!_detected_modules[index].driver && _detected_modules[index].ap_dronecan) {
54
WITH_SEMAPHORE(_sem_registry);
55
// Register new Compass mode to a backend
56
driver = NEW_NOTHROW AP_Compass_DroneCAN(_detected_modules[index].ap_dronecan, _detected_modules[index].devid);
57
if (driver) {
58
if (!driver->init()) {
59
delete driver;
60
return nullptr;
61
}
62
_detected_modules[index].driver = driver;
63
AP::can().log_text(AP_CANManager::LOG_INFO,
64
LOG_TAG,
65
"Found Mag Node %d on Bus %d Sensor ID %d\n",
66
_detected_modules[index].node_id,
67
_detected_modules[index].ap_dronecan->get_driver_index(),
68
_detected_modules[index].sensor_id);
69
#if AP_TEST_DRONECAN_DRIVERS
70
// Scroll through the registered compasses, and set the offsets
71
if (driver->_compass.get_offsets(index).is_zero()) {
72
driver->_compass.set_offsets(index, AP::sitl()->mag_ofs[index]);
73
}
74
75
// we want to simulate a calibrated compass by default, so set
76
// scale to 1
77
AP_Param::set_default_by_name("COMPASS_SCALE", 1);
78
AP_Param::set_default_by_name("COMPASS_SCALE2", 1);
79
AP_Param::set_default_by_name("COMPASS_SCALE3", 1);
80
driver->save_dev_id(index);
81
driver->set_rotation(index, ROTATION_NONE);
82
83
// make first compass external
84
driver->set_external(index, true);
85
#endif
86
}
87
}
88
return driver;
89
}
90
91
bool AP_Compass_DroneCAN::init()
92
{
93
// Adding 1 is necessary to allow backward compatibility, where this field was set as 1 by default
94
if (!register_compass(_devid)) {
95
return false;
96
}
97
set_external(true);
98
99
AP::can().log_text(AP_CANManager::LOG_INFO, LOG_TAG, "AP_Compass_DroneCAN loaded\n\r");
100
return true;
101
}
102
103
AP_Compass_DroneCAN* AP_Compass_DroneCAN::get_dronecan_backend(AP_DroneCAN* ap_dronecan, uint8_t node_id, uint8_t sensor_id)
104
{
105
if (ap_dronecan == nullptr) {
106
return nullptr;
107
}
108
for (uint8_t i=0; i<ARRAY_SIZE(_detected_modules); i++) {
109
if (_detected_modules[i].driver &&
110
_detected_modules[i].ap_dronecan == ap_dronecan &&
111
_detected_modules[i].node_id == node_id &&
112
_detected_modules[i].sensor_id == sensor_id) {
113
return _detected_modules[i].driver;
114
}
115
}
116
117
bool already_detected = false;
118
// Check if there's an empty spot for possible registration
119
for (uint8_t i = 0; i < ARRAY_SIZE(_detected_modules); i++) {
120
if (_detected_modules[i].ap_dronecan == ap_dronecan &&
121
_detected_modules[i].node_id == node_id &&
122
_detected_modules[i].sensor_id == sensor_id) {
123
// Already Detected
124
already_detected = true;
125
break;
126
}
127
}
128
if (!already_detected) {
129
for (uint8_t i = 0; i < ARRAY_SIZE(_detected_modules); i++) {
130
if (nullptr == _detected_modules[i].ap_dronecan) {
131
_detected_modules[i].ap_dronecan = ap_dronecan;
132
_detected_modules[i].node_id = node_id;
133
_detected_modules[i].sensor_id = sensor_id;
134
_detected_modules[i].devid = AP_HAL::Device::make_bus_id(AP_HAL::Device::BUS_TYPE_UAVCAN,
135
ap_dronecan->get_driver_index(),
136
node_id,
137
sensor_id + 1); // we use sensor_id as devtype
138
break;
139
}
140
}
141
}
142
143
struct DetectedModules tempslot;
144
// Sort based on the node_id, larger values first
145
// we do this, so that we have repeatable compass
146
// registration, especially in cases of extraneous
147
// CAN compass is connected.
148
for (uint8_t i = 1; i < ARRAY_SIZE(_detected_modules); i++) {
149
for (uint8_t j = i; j > 0; j--) {
150
if (_detected_modules[j].node_id > _detected_modules[j-1].node_id) {
151
tempslot = _detected_modules[j];
152
_detected_modules[j] = _detected_modules[j-1];
153
_detected_modules[j-1] = tempslot;
154
}
155
}
156
}
157
158
return nullptr;
159
}
160
161
void AP_Compass_DroneCAN::handle_mag_msg(const Vector3f &mag)
162
{
163
Vector3f raw_field = mag * 1000.0;
164
165
accumulate_sample(raw_field, _instance);
166
}
167
168
void AP_Compass_DroneCAN::handle_magnetic_field(AP_DroneCAN *ap_dronecan, const CanardRxTransfer& transfer, const uavcan_equipment_ahrs_MagneticFieldStrength& msg)
169
{
170
WITH_SEMAPHORE(_sem_registry);
171
172
Vector3f mag_vector;
173
AP_Compass_DroneCAN* driver = get_dronecan_backend(ap_dronecan, transfer.source_node_id, 0);
174
if (driver != nullptr) {
175
mag_vector[0] = msg.magnetic_field_ga[0];
176
mag_vector[1] = msg.magnetic_field_ga[1];
177
mag_vector[2] = msg.magnetic_field_ga[2];
178
driver->handle_mag_msg(mag_vector);
179
}
180
}
181
182
void AP_Compass_DroneCAN::handle_magnetic_field_2(AP_DroneCAN *ap_dronecan, const CanardRxTransfer& transfer, const uavcan_equipment_ahrs_MagneticFieldStrength2 &msg)
183
{
184
WITH_SEMAPHORE(_sem_registry);
185
186
Vector3f mag_vector;
187
uint8_t sensor_id = msg.sensor_id;
188
AP_Compass_DroneCAN* driver = get_dronecan_backend(ap_dronecan, transfer.source_node_id, sensor_id);
189
if (driver != nullptr) {
190
mag_vector[0] = msg.magnetic_field_ga[0];
191
mag_vector[1] = msg.magnetic_field_ga[1];
192
mag_vector[2] = msg.magnetic_field_ga[2];
193
driver->handle_mag_msg(mag_vector);
194
}
195
}
196
197
#if AP_COMPASS_DRONECAN_HIRES_ENABLED
198
/*
199
just log hires magnetic field data for magnetic surveying
200
*/
201
void AP_Compass_DroneCAN::handle_magnetic_field_hires(AP_DroneCAN *ap_dronecan, const CanardRxTransfer& transfer,
202
const dronecan_sensors_magnetometer_MagneticFieldStrengthHiRes &msg)
203
{
204
// @LoggerMessage: MAGH
205
// @Description: Magnetometer high resolution data
206
// @Field: TimeUS: Time since system startup
207
// @Field: Node: CAN node
208
// @Field: Sensor: sensor ID on node
209
// @Field: Bus: CAN bus
210
// @Field: Mx: X axis field
211
// @Field: My: y axis field
212
// @Field: Mz: z axis field
213
214
#if HAL_LOGGING_ENABLED
215
// just log it for now
216
AP::logger().WriteStreaming("MAGH", "TimeUS,Node,Sensor,Bus,Mx,My,Mz", "s#-----", "F------", "QBBBfff",
217
transfer.timestamp_usec,
218
transfer.source_node_id,
219
ap_dronecan->get_driver_index(),
220
msg.sensor_id,
221
msg.magnetic_field_ga[0]*1000,
222
msg.magnetic_field_ga[1]*1000,
223
msg.magnetic_field_ga[2]*1000);
224
#endif // HAL_LOGGING_ENABLED
225
}
226
#endif // AP_COMPASS_DRONECAN_HIRES_ENABLED
227
228
void AP_Compass_DroneCAN::read(void)
229
{
230
drain_accumulated_samples();
231
}
232
#endif // AP_COMPASS_DRONECAN_ENABLED
233
234