Skip to content

Commit a97fb70

Browse files
Merge pull request #11045 from functionpointer/smartport_batt_sensor
New feature: SmartPort battery sensor
2 parents 0204c18 + d7e196c commit a97fb70

File tree

6 files changed

+86
-10
lines changed

6 files changed

+86
-10
lines changed

docs/Settings.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ This sets the output voltage to current scaling for the current sensor in 0.1 mV
634634

635635
### current_meter_type
636636

637-
ADC , VIRTUAL, NONE. The virtual current sensor, once calibrated, estimates the current value from throttle position.
637+
ADC, VIRTUAL, FAKE, ESC, SMARTPORT, NONE. The virtual current sensor, once calibrated, estimates the current value from throttle position.
638638

639639
| Default | Min | Max |
640640
| --- | --- | --- |
@@ -6514,7 +6514,7 @@ Maximum voltage per cell in 0.01V units, default is 4.20V
65146514

65156515
### vbat_meter_type
65166516

6517-
Vbat voltage source. Possible values: `NONE`, `ADC`, `ESC`. `ESC` required ESC telemetry enebled and running
6517+
Vbat voltage source. Possible values: `NONE`, `ADC`, `SMARTPORT`, `ESC`. `ESC` requires ESC telemetry enabled and running. `SMARTPORT` requires SmartPort Master enabled and running.
65186518

65196519
| Default | Min | Max |
65206520
| --- | --- | --- |

src/main/fc/settings.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ tables:
3333
- name: failsafe_procedure
3434
values: ["LAND", "DROP", "RTH", "NONE"]
3535
- name: current_sensor
36-
values: ["NONE", "ADC", "VIRTUAL", "FAKE", "ESC"]
36+
values: ["NONE", "ADC", "VIRTUAL", "FAKE", "ESC", "SMARTPORT"]
3737
enum: currentSensor_e
3838
- name: voltage_sensor
39-
values: ["NONE", "ADC", "ESC", "FAKE"]
39+
values: ["NONE", "ADC", "ESC", "FAKE", "SMARTPORT"]
4040
enum: voltageSensor_e
4141
- name: imu_inertia_comp_method
4242
values: ["VELNED", "TURNRATE","ADAPTIVE"]
@@ -976,7 +976,7 @@ groups:
976976
headers: ["sensors/battery_config_structs.h"]
977977
members:
978978
- name: vbat_meter_type
979-
description: "Vbat voltage source. Possible values: `NONE`, `ADC`, `ESC`. `ESC` required ESC telemetry enebled and running"
979+
description: "Vbat voltage source. Possible values: `NONE`, `ADC`, `SMARTPORT`, `ESC`. `ESC` requires ESC telemetry enabled and running. `SMARTPORT` requires SmartPort Master enabled and running."
980980
condition: USE_ADC
981981
default_value: ADC
982982
field: voltage.type
@@ -1008,7 +1008,7 @@ groups:
10081008
min: -32768
10091009
max: 32767
10101010
- name: current_meter_type
1011-
description: "ADC , VIRTUAL, NONE. The virtual current sensor, once calibrated, estimates the current value from throttle position."
1011+
description: "ADC, VIRTUAL, FAKE, ESC, SMARTPORT, NONE. The virtual current sensor, once calibrated, estimates the current value from throttle position."
10121012
default_value: "ADC"
10131013
field: current.type
10141014
table: current_sensor

src/main/io/smartport_master.c

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ enum
9898
#define SMARTPORT_BYTESTUFFING_MARKER 0x7D
9999
#define SMARTPORT_BYTESTUFFING_XOR_VALUE 0x20
100100

101-
#define SMARTPORT_SENSOR_DATA_TIMEOUT 500 // ms
101+
#define SMARTPORT_SENSOR_DATA_TIMEOUT (500*1000) // us
102102

103103
#define SMARTPORT_FORWARD_REQUESTS_MAX 10
104104

@@ -128,6 +128,10 @@ typedef struct {
128128
timeUs_t altitudeTimestamp;
129129
int16_t vario;
130130
timeUs_t varioTimestamp;
131+
int16_t current;
132+
timeUs_t currentTimestamp;
133+
int16_t voltage;
134+
timeUs_t voltageTimestamp;
131135
} smartportSensorsData_t;
132136

133137
typedef struct {
@@ -378,6 +382,17 @@ static void decodeVarioData(uint32_t sdata)
378382
sensorsData.vario = sdata * 2; // mm/s
379383
}
380384

385+
static void decodeCurrentData(uint32_t sdata)
386+
{
387+
// data comes in 100mA steps
388+
sensorsData.current = ((int16_t)sdata) * 10;
389+
}
390+
391+
static void decodeVoltageData(uint32_t sdata)
392+
{
393+
sensorsData.voltage = (int16_t)sdata;
394+
}
395+
381396
static void processSensorPayload(smartPortPayload_t *payload, timeUs_t currentTimeUs)
382397
{
383398
switch (payload->valueId) {
@@ -400,6 +415,16 @@ static void processSensorPayload(smartPortPayload_t *payload, timeUs_t currentTi
400415
decodeVarioData(payload->data);
401416
sensorsData.varioTimestamp = currentTimeUs;
402417
break;
418+
419+
case DATAID_CURRENT:
420+
decodeCurrentData(payload->data);
421+
sensorsData.currentTimestamp = currentTimeUs;
422+
break;
423+
424+
case DATAID_VFAS:
425+
decodeVoltageData(payload->data);
426+
sensorsData.voltageTimestamp = currentTimeUs;
427+
break;
403428
}
404429
sensorPayloadCache[currentPolledPhyID] = *payload;
405430
}
@@ -568,6 +593,24 @@ vs600Data_t *smartportMasterGetVS600Data(void)
568593
return &sensorsData.vs600;
569594
}
570595

596+
int16_t *smartportMasterGetCurrentData(void)
597+
{
598+
if (micros() - sensorsData.currentTimestamp > SMARTPORT_SENSOR_DATA_TIMEOUT) {
599+
return NULL;
600+
}
601+
602+
return &sensorsData.current;
603+
}
604+
605+
int16_t *smartportMasterGetVoltageData(void)
606+
{
607+
if (micros() - sensorsData.voltageTimestamp > SMARTPORT_SENSOR_DATA_TIMEOUT) {
608+
return NULL;
609+
}
610+
611+
return &sensorsData.voltage;
612+
}
613+
571614
bool smartportMasterPhyIDIsActive(uint8_t phyID)
572615
{
573616
return phyIDIsActive(phyID);

src/main/io/smartport_master.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ typedef struct {
4343
int16_t voltage[6];
4444
} cellsData_t;
4545

46+
typedef struct {
47+
int16_t amperage;
48+
int16_t vfas;
49+
} batteryData_t;
50+
4651
typedef enum {
4752
VS600_BAND_A,
4853
VS600_BAND_B,
@@ -83,5 +88,7 @@ bool smartportMasterNextForwardResponse(uint8_t phyID, smartPortPayload_t *paylo
8388
// Returns latest Cells data or NULL if the data is too old
8489
cellsData_t *smartportMasterGetCellsData(void);
8590
vs600Data_t *smartportMasterGetVS600Data(void);
91+
int16_t *smartportMasterGetCurrentData(void);
92+
int16_t *smartportMasterGetVoltageData(void);
8693

8794
#endif /* USE_SMARTPORT_MASTER */

src/main/sensors/battery.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@
6262
#if defined(USE_FAKE_BATT_SENSOR)
6363
#include "sensors/battery_sensor_fake.h"
6464
#endif
65+
#if defined(USE_SMARTPORT_MASTER)
66+
#include "io/smartport_master.h"
67+
#endif
6568

6669
#define ADCVREF 3300 // in mV (3300 = 3.3V)
6770

@@ -291,6 +294,17 @@ static void updateBatteryVoltage(timeUs_t timeDelta, bool justConnected)
291294
vbat = fakeBattSensorGetVBat();
292295
break;
293296
#endif
297+
298+
#if defined(USE_SMARTPORT_MASTER)
299+
case VOLTAGE_SENSOR_SMARTPORT:
300+
int16_t * smartportVoltageData = smartportMasterGetVoltageData();
301+
if (smartportVoltageData) {
302+
vbat = *smartportVoltageData;
303+
} else {
304+
vbat = 0;
305+
}
306+
break;
307+
#endif
294308
case VOLTAGE_SENSOR_NONE:
295309
default:
296310
vbat = 0;
@@ -598,7 +612,16 @@ void currentMeterUpdate(timeUs_t timeDelta)
598612
}
599613
break;
600614
#endif
601-
615+
#if defined(USE_SMARTPORT_MASTER)
616+
case CURRENT_SENSOR_SMARTPORT:
617+
int16_t * smartportCurrentData = smartportMasterGetCurrentData();
618+
if (smartportCurrentData) {
619+
amperage = *smartportCurrentData;
620+
} else {
621+
amperage = 0;
622+
}
623+
break;
624+
#endif
602625
#if defined(USE_FAKE_BATT_SENSOR)
603626
case CURRENT_SENSOR_FAKE:
604627
amperage = fakeBattSensorGetAmerperage();

src/main/sensors/battery_config_structs.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,17 @@ typedef enum {
3131
CURRENT_SENSOR_VIRTUAL,
3232
CURRENT_SENSOR_FAKE,
3333
CURRENT_SENSOR_ESC,
34-
CURRENT_SENSOR_MAX = CURRENT_SENSOR_FAKE
34+
CURRENT_SENSOR_SMARTPORT,
35+
CURRENT_SENSOR_MAX = CURRENT_SENSOR_SMARTPORT
3536
} currentSensor_e;
3637

3738
typedef enum {
3839
VOLTAGE_SENSOR_NONE = 0,
3940
VOLTAGE_SENSOR_ADC,
4041
VOLTAGE_SENSOR_ESC,
41-
VOLTAGE_SENSOR_FAKE
42+
VOLTAGE_SENSOR_FAKE,
43+
VOLTAGE_SENSOR_SMARTPORT,
44+
VOLTAGE_SENSOR_MAX = VOLTAGE_SENSOR_SMARTPORT
4245
} voltageSensor_e;
4346

4447
typedef enum {

0 commit comments

Comments
 (0)