Skip to content

Commit 480325c

Browse files
authored
Merge pull request #12 from iNavFlight/master
Merge from master
2 parents c3a85b9 + 0af4298 commit 480325c

36 files changed

+1031
-22
lines changed

docs/Programming Framework.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ IPF can be edited using INAV Configurator user interface, of via CLI
7373
| 37 | MAP_OUTPUT | Scales `Operand A` from [`0` : `1000`] to [`0` : `Operand B`]. Note: input will be constrained and then scaled |
7474
| 38 | RC_CHANNEL_OVERRIDE | Overrides channel set by `Operand A` to value of `Operand B` |
7575
| 39 | SET_HEADING_TARGET | Sets heading-hold target to `Operand A`, in degrees. Value wraps-around. |
76+
| 40 | MOD | Divide `Operand A` by `Operand B` and returns the remainder |
7677

7778

7879
### Operands

docs/Settings.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
| failsafe_throttle_low_delay | 100 | If failsafe activated when throttle is low for this much time - bypass failsafe and disarm, in 10th of seconds. 0 = No timeout |
102102
| fixed_wing_auto_arm | OFF | Auto-arm fixed wing aircraft on throttle above min_check, and disarming with stick commands are disabled, so power cycle is required to disarm. Requires enabled motorstop and no arm switch configured. |
103103
| flaperon_throw_offset | 200 | Defines throw range in us for both ailerons that will be passed to servo mixer via input source 14 (`FEATURE FLAPS`) when FLAPERON mode is activated. |
104+
| flip_over_after_crash_power_factor | 65 | flip over after crash power factor |
104105
| fpv_mix_degrees | | |
105106
| frsky_coordinates_format | 0 | D-Series telemetry only: FRSKY_FORMAT_DMS (default), FRSKY_FORMAT_NMEA |
106107
| frsky_default_latitude | 0.000 | D-Series telemetry only: OpenTX needs a valid set of coordinates to show compass value. A fake value defined in this setting is sent while no fix is acquired. |

src/main/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ main_sources(COMMON_SRC
4848
common/typeconversion.h
4949
common/uvarint.c
5050
common/uvarint.h
51+
common/circular_queue.c
52+
common/circular_queue.h
5153

5254
config/config_eeprom.c
5355
config/config_eeprom.h
@@ -154,6 +156,8 @@ main_sources(COMMON_SRC
154156
drivers/compass/compass_mpu9250.h
155157
drivers/compass/compass_qmc5883l.c
156158
drivers/compass/compass_qmc5883l.h
159+
drivers/compass/compass_rm3100.c
160+
drivers/compass/compass_rm3100.h
157161
drivers/compass/compass_msp.c
158162
drivers/compass/compass_msp.h
159163

src/main/common/circular_queue.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* This file is part of INAV Project.
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
6+
* You can obtain one at http://mozilla.org/MPL/2.0/.
7+
*
8+
* Alternatively, the contents of this file may be used under the terms
9+
* of the GNU General Public License Version 3, as described below:
10+
*
11+
* This file is free software: you may copy, redistribute and/or modify
12+
* it under the terms of the GNU General Public License as published by the
13+
* Free Software Foundation, either version 3 of the License, or (at your
14+
* option) any later version.
15+
*
16+
* This file is distributed in the hope that it will be useful, but
17+
* WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
19+
* Public License for more details.
20+
*
21+
* You should have received a copy of the GNU General Public License
22+
* along with this program. If not, see http://www.gnu.org/licenses/.
23+
*/
24+
25+
#include "circular_queue.h"
26+
27+
void circularBufferInit(circularBuffer_t *circular_buffer, uint8_t *buffer, size_t buffer_size,
28+
size_t buffer_element_size) {
29+
circular_buffer->buffer = buffer;
30+
circular_buffer->bufferSize = buffer_size;
31+
circular_buffer->elementSize = buffer_element_size;
32+
circular_buffer->head = 0;
33+
circular_buffer->tail = 0;
34+
circular_buffer->size = 0;
35+
}
36+
37+
void circularBufferPushElement(circularBuffer_t *circularBuffer, uint8_t *element) {
38+
if (circularBufferIsFull(circularBuffer))
39+
return;
40+
41+
memcpy(circularBuffer->buffer + circularBuffer->tail, element, circularBuffer->elementSize);
42+
43+
circularBuffer->tail += circularBuffer->elementSize;
44+
circularBuffer->tail %= circularBuffer->bufferSize;
45+
circularBuffer->size += 1;
46+
}
47+
48+
void circularBufferPopHead(circularBuffer_t *circularBuffer, uint8_t *element) {
49+
memcpy(element, circularBuffer->buffer + circularBuffer->head, circularBuffer->elementSize);
50+
51+
circularBuffer->head += circularBuffer->elementSize;
52+
circularBuffer->head %= circularBuffer->bufferSize;
53+
circularBuffer->size -= 1;
54+
}
55+
56+
int circularBufferIsFull(circularBuffer_t *circularBuffer) {
57+
return circularBufferCountElements(circularBuffer) * circularBuffer->elementSize == circularBuffer->bufferSize;
58+
}
59+
60+
int circularBufferIsEmpty(circularBuffer_t *circularBuffer) {
61+
return circularBuffer==NULL || circularBufferCountElements(circularBuffer) == 0;
62+
}
63+
64+
size_t circularBufferCountElements(circularBuffer_t *circularBuffer) {
65+
return circularBuffer->size;
66+
}

src/main/common/circular_queue.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* This file is part of INAV Project.
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
6+
* You can obtain one at http://mozilla.org/MPL/2.0/.
7+
*
8+
* Alternatively, the contents of this file may be used under the terms
9+
* of the GNU General Public License Version 3, as described below:
10+
*
11+
* This file is free software: you may copy, redistribute and/or modify
12+
* it under the terms of the GNU General Public License as published by the
13+
* Free Software Foundation, either version 3 of the License, or (at your
14+
* option) any later version.
15+
*
16+
* This file is distributed in the hope that it will be useful, but
17+
* WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
19+
* Public License for more details.
20+
*
21+
* You should have received a copy of the GNU General Public License
22+
* along with this program. If not, see http://www.gnu.org/licenses/.
23+
*/
24+
25+
#ifndef INAV_CIRCULAR_QUEUE_H
26+
#define INAV_CIRCULAR_QUEUE_H
27+
28+
#include "stdint.h"
29+
#include "string.h"
30+
31+
typedef struct circularBuffer_s{
32+
size_t head;
33+
size_t tail;
34+
size_t bufferSize;
35+
uint8_t * buffer;
36+
size_t elementSize;
37+
size_t size;
38+
}circularBuffer_t;
39+
40+
void circularBufferInit(circularBuffer_t * circularBuffer, uint8_t * buffer, size_t bufferSize, size_t bufferElementSize);
41+
void circularBufferPushElement(circularBuffer_t * circularBuffer, uint8_t * element);
42+
void circularBufferPopHead(circularBuffer_t * circularBuffer, uint8_t * element);
43+
int circularBufferIsFull(circularBuffer_t * circularBuffer);
44+
int circularBufferIsEmpty(circularBuffer_t *circularBuffer);
45+
size_t circularBufferCountElements(circularBuffer_t * circularBuffer);
46+
47+
#endif //INAV_CIRCULAR_QUEUE_H

src/main/common/maths.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@
8888
#define _ABS_I(x, var) _ABS_II(x, var)
8989
#define ABS(x) _ABS_I(x, _CHOOSE_VAR(_abs, __COUNTER__))
9090

91+
#define power3(x) ((x)*(x)*(x))
92+
9193
// Floating point Euler angles.
9294
typedef struct fp_angles {
9395
float roll;

src/main/drivers/bus.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ typedef enum {
118118
DEVHW_QMC5883,
119119
DEVHW_MAG3110,
120120
DEVHW_LIS3MDL,
121+
DEVHW_RM3100,
121122

122123
/* Temp sensor chips */
123124
DEVHW_LM75_0,
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
/*
2+
* This file is part of INAV Project.
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
6+
* You can obtain one at http://mozilla.org/MPL/2.0/.
7+
*
8+
* Alternatively, the contents of this file may be used under the terms
9+
* of the GNU General Public License Version 3, as described below:
10+
*
11+
* This file is free software: you may copy, redistribute and/or modify
12+
* it under the terms of the GNU General Public License as published by the
13+
* Free Software Foundation, either version 3 of the License, or (at your
14+
* option) any later version.
15+
*
16+
* This file is distributed in the hope that it will be useful, but
17+
* WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
19+
* Public License for more details.
20+
*
21+
* You should have received a copy of the GNU General Public License
22+
* along with this program. If not, see http://www.gnu.org/licenses/.
23+
*/
24+
25+
#include <stdbool.h>
26+
#include <stdint.h>
27+
28+
#include <math.h>
29+
30+
#include "platform.h"
31+
32+
#ifdef USE_MAG_RM3100
33+
34+
#include "build/build_config.h"
35+
#include "build/debug.h"
36+
37+
#include "common/axis.h"
38+
#include "common/maths.h"
39+
#include "common/utils.h"
40+
41+
#include "drivers/time.h"
42+
#include "drivers/bus_i2c.h"
43+
44+
#include "sensors/boardalignment.h"
45+
#include "sensors/sensors.h"
46+
47+
#include "drivers/sensor.h"
48+
#include "drivers/compass/compass.h"
49+
50+
#include "drivers/compass/compass_rm3100.h"
51+
52+
#define RM3100_REG_POLL 0x00
53+
#define RM3100_REG_CMM 0x01
54+
#define RM3100_REG_CCX1 0x04
55+
#define RM3100_REG_CCX0 0x05
56+
#define RM3100_REG_CCY1 0x06
57+
#define RM3100_REG_CCY0 0x07
58+
#define RM3100_REG_CCZ1 0x08
59+
#define RM3100_REG_CCZ0 0x09
60+
#define RM3100_REG_TMRC 0x0B
61+
#define RM3100_REG_MX 0x24
62+
#define RM3100_REG_MY 0x27
63+
#define RM3100_REG_MZ 0x2A
64+
#define RM3100_REG_BIST 0x33
65+
#define RM3100_REG_STATUS 0x34
66+
#define RM3100_REG_HSHAKE 0x35
67+
#define RM3100_REG_REVID 0x36
68+
69+
#define RM3100_REVID 0x22
70+
71+
#define CCX_DEFAULT_MSB 0x00
72+
#define CCX_DEFAULT_LSB 0xC8
73+
#define CCY_DEFAULT_MSB CCX_DEFAULT_MSB
74+
#define CCY_DEFAULT_LSB CCX_DEFAULT_LSB
75+
#define CCZ_DEFAULT_MSB CCX_DEFAULT_MSB
76+
#define CCZ_DEFAULT_LSB CCX_DEFAULT_LSB
77+
#define CMM_DEFAULT 0x71 // Continuous mode
78+
#define TMRC_DEFAULT 0x94
79+
80+
81+
static bool deviceInit(magDev_t * mag)
82+
{
83+
busWrite(mag->busDev, RM3100_REG_TMRC, TMRC_DEFAULT);
84+
85+
busWrite(mag->busDev, RM3100_REG_CMM, CMM_DEFAULT);
86+
87+
busWrite(mag->busDev, RM3100_REG_CCX1, CCX_DEFAULT_MSB);
88+
busWrite(mag->busDev, RM3100_REG_CCX0, CCX_DEFAULT_LSB);
89+
90+
busWrite(mag->busDev, RM3100_REG_CCY1, CCY_DEFAULT_MSB);
91+
busWrite(mag->busDev, RM3100_REG_CCY0, CCY_DEFAULT_LSB);
92+
93+
busWrite(mag->busDev, RM3100_REG_CCZ1, CCZ_DEFAULT_MSB);
94+
busWrite(mag->busDev, RM3100_REG_CCZ0, CCZ_DEFAULT_LSB);
95+
96+
return true;
97+
}
98+
99+
static bool deviceRead(magDev_t * mag)
100+
{
101+
uint8_t status;
102+
103+
#pragma pack(push, 1)
104+
struct {
105+
uint8_t x[3];
106+
uint8_t y[3];
107+
uint8_t z[3];
108+
} rm_report;
109+
#pragma pack(pop)
110+
111+
mag->magADCRaw[X] = 0;
112+
mag->magADCRaw[Y] = 0;
113+
mag->magADCRaw[Z] = 0;
114+
115+
/* Check if new measurement is ready */
116+
bool ack = busRead(mag->busDev, RM3100_REG_STATUS, &status);
117+
118+
if (!ack || (status & 0x80) == 0) {
119+
return false;
120+
}
121+
122+
ack = busReadBuf(mag->busDev, RM3100_REG_MX, (uint8_t *)&rm_report, sizeof(rm_report));
123+
if (!ack) {
124+
return false;
125+
}
126+
127+
int32_t xraw;
128+
int32_t yraw;
129+
int32_t zraw;
130+
131+
/* Rearrange mag data */
132+
xraw = ((rm_report.x[0] << 24) | (rm_report.x[1] << 16) | (rm_report.x[2]) << 8);
133+
yraw = ((rm_report.y[0] << 24) | (rm_report.y[1] << 16) | (rm_report.y[2]) << 8);
134+
zraw = ((rm_report.z[0] << 24) | (rm_report.z[1] << 16) | (rm_report.z[2]) << 8);
135+
136+
/* Truncate to 16-bit integers and pass along */
137+
mag->magADCRaw[X] = (int16_t)(xraw >> 16);
138+
mag->magADCRaw[Y] = (int16_t)(yraw >> 16);
139+
mag->magADCRaw[Z] = (int16_t)(zraw >> 16);
140+
141+
return true;
142+
}
143+
144+
#define DETECTION_MAX_RETRY_COUNT 5
145+
static bool deviceDetect(magDev_t * mag)
146+
{
147+
for (int retryCount = 0; retryCount < DETECTION_MAX_RETRY_COUNT; retryCount++) {
148+
uint8_t revid = 0;
149+
bool ack = busRead(mag->busDev, RM3100_REG_REVID, &revid);
150+
151+
if (ack && revid == RM3100_REVID) {
152+
return true;
153+
}
154+
}
155+
156+
return false;
157+
}
158+
159+
bool rm3100MagDetect(magDev_t * mag)
160+
{
161+
busSetSpeed(mag->busDev, BUS_SPEED_STANDARD);
162+
163+
mag->busDev = busDeviceInit(BUSTYPE_ANY, DEVHW_RM3100, mag->magSensorToUse, OWNER_COMPASS);
164+
if (mag->busDev == NULL) {
165+
return false;
166+
}
167+
168+
if (!deviceDetect(mag)) {
169+
busDeviceDeInit(mag->busDev);
170+
return false;
171+
}
172+
173+
mag->init = deviceInit;
174+
mag->read = deviceRead;
175+
176+
return true;
177+
}
178+
179+
#endif
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* This file is part of INAV Project.
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
6+
* You can obtain one at http://mozilla.org/MPL/2.0/.
7+
*
8+
* Alternatively, the contents of this file may be used under the terms
9+
* of the GNU General Public License Version 3, as described below:
10+
*
11+
* This file is free software: you may copy, redistribute and/or modify
12+
* it under the terms of the GNU General Public License as published by the
13+
* Free Software Foundation, either version 3 of the License, or (at your
14+
* option) any later version.
15+
*
16+
* This file is distributed in the hope that it will be useful, but
17+
* WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
19+
* Public License for more details.
20+
*
21+
* You should have received a copy of the GNU General Public License
22+
* along with this program. If not, see http://www.gnu.org/licenses/.
23+
*/
24+
25+
#pragma once
26+
27+
bool rm3100MagDetect(magDev_t *mag);

0 commit comments

Comments
 (0)