forked from ArduPilot/ardupilot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AP_Generator_RichenPower.cpp
401 lines (345 loc) · 11.8 KB
/
AP_Generator_RichenPower.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "AP_Generator_RichenPower.h"
#if GENERATOR_ENABLED
#include <AP_HAL/AP_HAL.h>
#include <AP_Logger/AP_Logger.h>
#include <AP_SerialManager/AP_SerialManager.h>
#include <GCS_MAVLink/GCS.h>
#include <AP_HAL/utility/sparse-endian.h>
extern const AP_HAL::HAL& hal;
// TODO: failsafe if we don't get readings?
AP_Generator_RichenPower::AP_Generator_RichenPower()
{
if (_singleton) {
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
AP_HAL::panic("Too many richenpower generators");
#endif
return;
}
_singleton = this;
}
void AP_Generator_RichenPower::init()
{
const AP_SerialManager &serial_manager = AP::serialmanager();
uart = serial_manager.find_serial(AP_SerialManager::SerialProtocol_Generator, 0);
if (uart != nullptr) {
const uint32_t baud = serial_manager.find_baudrate(AP_SerialManager::SerialProtocol_Generator, 0);
uart->begin(baud, 256, 256);
}
}
void AP_Generator_RichenPower::move_header_in_buffer(uint8_t initial_offset)
{
uint8_t header_offset;
for (header_offset=initial_offset; header_offset<body_length; header_offset++) {
if (u.parse_buffer[header_offset] == HEADER_MAGIC1) {
break;
}
}
if (header_offset != 0) {
// header was found, but not at index 0; move it back to start of array
memmove(u.parse_buffer, &u.parse_buffer[header_offset], body_length - header_offset);
body_length -= header_offset;
}
}
// read - read serial port, return true if a new reading has been found
bool AP_Generator_RichenPower::get_reading()
{
if (uart == nullptr) {
return false;
}
// Example of a packet from a H2 controller:
//AA 55 00 0A 00 00 00 00 00 04 1E B0 00 10 00 00 23 7A 23
//7A 11 1D 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00
//00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
//00 00 00 00 00 00 00 00 00 1E BE 55 AA
//16 bit one data Hight + Low
// fill our buffer some more:
uint32_t nbytes = uart->read(&u.parse_buffer[body_length],
ARRAY_SIZE(u.parse_buffer)-body_length);
if (nbytes == 0) {
return false;
}
body_length += nbytes;
move_header_in_buffer(0);
// header byte 1 is correct.
if (body_length < ARRAY_SIZE(u.parse_buffer)) {
// need a full buffer to have a valid message...
return false;
}
if (u.packet.headermagic2 != HEADER_MAGIC2) {
move_header_in_buffer(1);
return false;
}
// check for the footer signature:
if (u.packet.footermagic1 != FOOTER_MAGIC1) {
move_header_in_buffer(1);
return false;
}
if (u.packet.footermagic2 != FOOTER_MAGIC2) {
move_header_in_buffer(1);
return false;
}
// calculate checksum....
uint16_t checksum = 0;
const uint8_t *checksum_buffer = &u.parse_buffer[2];
for (uint8_t i=0; i<5; i++) {
checksum += be16toh_ptr(&checksum_buffer[2*i]);
}
if (checksum != be16toh(u.packet.checksum)) {
move_header_in_buffer(1);
return false;
}
// check the version:
const uint16_t version = be16toh(u.packet.version);
const uint8_t major = version / 100;
const uint8_t minor = (version % 100) / 10;
const uint8_t point = version % 10;
if (!protocol_information_anounced) {
gcs().send_text(MAV_SEVERITY_INFO, "RichenPower: protocol %u.%u.%u", major, minor, point);
protocol_information_anounced = true;
}
last_reading.runtime = be16toh(u.packet.runtime_hours) * 3600 +
u.packet.runtime_minutes * 60 +
u.packet.runtime_seconds;
last_reading.seconds_until_maintenance = be32toh(u.packet.seconds_until_maintenance);
last_reading.errors = be16toh(u.packet.errors);
last_reading.rpm = be16toh(u.packet.rpm);
last_reading.output_voltage = be16toh(u.packet.output_voltage) / 100.0f;
last_reading.output_current = be16toh(u.packet.output_current) / 100.0f;
last_reading.mode = (Mode)u.packet.mode;
last_reading_ms = AP_HAL::millis();
body_length = 0;
return true;
}
/*
update the state of the sensor
*/
void AP_Generator_RichenPower::update(void)
{
if (uart == nullptr) {
return;
}
update_servo_channel();
update_runstate();
(void)get_reading();
Log_Write();
}
void AP_Generator_RichenPower::update_runstate()
{
if (_servo_channel == nullptr) {
// doesn't really matter what we want to do; we're not going
// to be able to affect it...
return;
}
// don't run the generator while the safety is on:
// if (hal.util->safety_switch_state() == AP_HAL::Util::SAFETY_DISARMED) {
// _servo_channel->set_output_pwm(SERVO_PWM_STOP);
// return;
// }
static const uint16_t SERVO_PWM_STOP = 1200;
static const uint16_t SERVO_PWM_IDLE = 1500;
static const uint16_t SERVO_PWM_RUN = 1900;
switch (runstate) {
case RunState::STOP:
_servo_channel->set_output_pwm(SERVO_PWM_STOP);
break;
case RunState::IDLE:
_servo_channel->set_output_pwm(SERVO_PWM_IDLE);
break;
case RunState::RUN:
_servo_channel->set_output_pwm(SERVO_PWM_RUN);
break;
}
}
void AP_Generator_RichenPower::update_servo_channel(void)
{
const uint32_t now = AP_HAL::millis();
if (now - _last_servo_channel_check < 1000) {
return;
}
_last_servo_channel_check = now;
SRV_Channel *control = SRV_Channels::get_channel_for(SRV_Channel::k_richenpower_control);
if (control == nullptr) {
if (_servo_channel != nullptr) {
gcs().send_text(MAV_SEVERITY_INFO, "RichenPower: no control channel");
_servo_channel = nullptr;
}
return;
}
if (_servo_channel != nullptr &&
_servo_channel->get_function() == SRV_Channel::k_richenpower_control) {
// note that _servo_channel could actually be == control here
return;
}
// gcs().send_text(MAV_SEVERITY_INFO, "RP: using control channel");
_servo_channel = control;
}
void AP_Generator_RichenPower::Log_Write()
{
if (last_logged_reading_ms == last_reading_ms) {
return;
}
last_logged_reading_ms = last_reading_ms;
AP::logger().Write(
"GEN",
"TimeUS,runTime,maintTime,errors,rpm,ovolt,ocurr,mode",
"s-------",
"F-------",
"QIIHHffB",
AP_HAL::micros64(),
last_reading.runtime,
last_reading.seconds_until_maintenance,
last_reading.errors,
last_reading.rpm,
last_reading.output_voltage,
last_reading.output_current,
last_reading.mode
);
}
bool AP_Generator_RichenPower::pre_arm_check(char *failmsg, uint8_t failmsg_len) const
{
if (uart == nullptr) {
// not configured in serial manager
return true;
}
if (last_reading_ms == 0) {
// allow optional use of generator
snprintf(failmsg, failmsg_len, "no RichenPower generator present");
return true;
}
const uint32_t now = AP_HAL::millis();
if (now - last_reading_ms > 2000) { // we expect @1Hz
snprintf(failmsg, failmsg_len, "no messages in %ums", unsigned(now - last_reading_ms));
return false;
}
if (last_reading.seconds_until_maintenance == 0) {
snprintf(failmsg, failmsg_len, "requires maintenance");
return false;
}
if (last_reading.errors) {
for (uint16_t i=0; i<16; i++) {
if (last_reading.errors & (1U << (uint16_t)i)) {
if (i < (uint16_t)Errors::LAST) {
snprintf(failmsg, failmsg_len, "error: %s", error_strings[i]);
} else {
snprintf(failmsg, failmsg_len, "unknown error: 1U<<%u", i);
}
}
}
return false;
}
if (last_reading.mode != Mode::RUN && last_reading.mode != Mode::CHARGE && last_reading.mode != Mode::BALANCE) {
snprintf(failmsg, failmsg_len, "not running");
return false;
}
if (runstate != RunState::RUN) {
snprintf(failmsg, failmsg_len, "requested state is not RUN");
return false;
}
return true;
}
bool AP_Generator_RichenPower::voltage(float &v) const
{
if (last_reading_ms == 0) {
return false;
}
v = last_reading.output_voltage;
return true;
}
bool AP_Generator_RichenPower::current(float &curr) const
{
if (last_reading_ms == 0) {
return false;
}
curr = last_reading.output_current;
return true;
}
bool AP_Generator_RichenPower::healthy() const
{
const uint32_t now = AP_HAL::millis();
if (last_reading_ms == 0 || now - last_reading_ms > 2000) {
return false;
}
if (last_reading.errors) {
return false;
}
return true;
}
//send generator status
void AP_Generator_RichenPower::send_generator_status(const GCS_MAVLINK &channel)
{
uint64_t status = 0;
switch (last_reading.mode) {
case Mode::IDLE:
break;
case Mode::RUN:
status |= MAV_GENERATOR_STATUS_FLAG_GENERATING;
break;
case Mode::CHARGE:
status |= MAV_GENERATOR_STATUS_FLAG_GENERATING;
status |= MAV_GENERATOR_STATUS_FLAG_CHARGING;
break;
case Mode::BALANCE:
status |= MAV_GENERATOR_STATUS_FLAG_GENERATING;
status |= MAV_GENERATOR_STATUS_FLAG_CHARGING;
break;
}
if (last_reading.rpm == 0) {
status |= MAV_GENERATOR_STATUS_FLAG_OFF;
}
if (last_reading.errors & (uint8_t)Errors::Overload) {
status |= MAV_GENERATOR_STATUS_FLAG_OVERCURRENT_FAULT;
}
if (last_reading.errors & (uint8_t)Errors::LowVoltageOutput) {
status |= MAV_GENERATOR_STATUS_FLAG_REDUCED_POWER;
}
if (last_reading.errors & (uint8_t)Errors::MaintenanceRequired) {
status |= MAV_GENERATOR_STATUS_FLAG_MAINTENANCE_REQUIRED;
}
if (last_reading.errors & (uint8_t)Errors::StartDisabled) {
status |= MAV_GENERATOR_STATUS_FLAG_START_INHIBITED;
}
if (last_reading.errors & (uint8_t)Errors::LowBatteryVoltage) {
status |= MAV_GENERATOR_STATUS_FLAG_BATTERY_UNDERVOLT_FAULT;
}
mavlink_msg_generator_status_send(
channel.get_chan(),
status,
last_reading.rpm, // generator_speed
std::numeric_limits<double>::quiet_NaN(), // battery_current; current into/out of battery
last_reading.output_current, // load_current; Current going to UAV
std::numeric_limits<double>::quiet_NaN(), // power_generated; the power being generated
last_reading.output_voltage, // bus_voltage; Voltage of the bus seen at the generator
INT16_MAX, // rectifier_temperature
std::numeric_limits<double>::quiet_NaN(), // bat_current_setpoint; The target battery current
INT16_MAX, // generator temperature
UINT32_MAX, // seconds this generator has run since it was rebooted
INT32_MAX // seconds until this generator requires maintenance
);
}
/*
* Get the AP_Generator singleton
*/
AP_Generator_RichenPower *AP_Generator_RichenPower::_singleton;
AP_Generator_RichenPower *AP_Generator_RichenPower::get_singleton()
{
return _singleton;
}
namespace AP {
AP_Generator_RichenPower *generator()
{
return AP_Generator_RichenPower::get_singleton();
}
};
#endif