Skip to content

Commit 4626dbb

Browse files
committed
* terarange evo lidar
1 parent 9ae8e29 commit 4626dbb

File tree

11 files changed

+192
-11
lines changed

11 files changed

+192
-11
lines changed

docs/Rangefinder.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Following rangefinders are supported:
2020
* UIB - experimental
2121
* MSP - experimental
2222
* TOF10120 - small & lightweight laser range sensor, usable up to 200cm
23+
* TERARANGER EVO - 30cm to 600cm, depends on version https://www.terabee.com/sensors-modules/lidar-tof-range-finders/#individual-distance-measurement-sensors
2324

2425
## Connections
2526

src/main/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@ main_sources(COMMON_SRC
237237
drivers/rangefinder/rangefinder_us42.h
238238
drivers/rangefinder/rangefinder_tof10120_i2c.c
239239
drivers/rangefinder/rangefinder_tof10120_i2c.h
240+
drivers/rangefinder/rangefinder_teraranger_evo.c
241+
drivers/rangefinder/rangefinder_teraranger_evo.h
240242

241243
drivers/resource.c
242244
drivers/resource.h

src/main/common/maths.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
#define CENTIMETERS_TO_CENTIFEET(cm) (cm / 0.3048f)
6060
#define CENTIMETERS_TO_FEET(cm) (cm / 30.48f)
6161
#define CENTIMETERS_TO_METERS(cm) (cm / 100.0f)
62+
#define MILLIMETERS_TO_CENTIMETERS(mm) (mm / 10.0f)
6263

6364
#define METERS_TO_CENTIMETERS(m) (m * 100)
6465

src/main/drivers/bus.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ typedef enum {
135135
DEVHW_VL53L1X,
136136
DEVHW_US42,
137137
DEVHW_TOF10120_I2C,
138+
DEVHW_TERARANGER_EVO_I2C,
138139

139140
/* Other hardware */
140141
DEVHW_MS4525, // Pitot meter
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* This file is part of Cleanflight.
3+
*
4+
* Cleanflight is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Cleanflight is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
#include <stdbool.h>
19+
#include <stdint.h>
20+
21+
#include "platform.h"
22+
23+
#include "common/maths.h"
24+
25+
#if defined(USE_RANGEFINDER) && defined(USE_RANGEFINDER_TERARANGER_EVO_I2C)
26+
27+
#include "build/build_config.h"
28+
29+
#include "common/crc.h"
30+
31+
#include "drivers/time.h"
32+
#include "drivers/bus_i2c.h"
33+
34+
#include "drivers/rangefinder/rangefinder.h"
35+
#include "drivers/rangefinder/rangefinder_teraranger_evo.h"
36+
37+
#include "build/debug.h"
38+
39+
#define TERARANGER_EVO_DETECTION_CONE_DECIDEGREES 900
40+
#define TERARANGER_EVO_DETECTION_CONE_EXTENDED_DECIDEGREES 900
41+
42+
#define TERARANGER_EVO_I2C_ADDRESS 0x31
43+
#define TERARANGER_EVO_I2C_REGISTRY_TRIGGER_READING 0x00 // Write this command to the TeraRanger Evo and after a wait of approximately 500us read the 3 byte distance response
44+
#define TERARANGER_EVO_I2C_REGISTRY_WHO_AM_I 0x01 // Write this value to TeraRanger Evo via I2C and the device responds with 0xA
45+
#define TERARANGER_EVO_I2C_REGISTRY_CHANGE_BASE_ADDR 0xA2 // This command assigns a base address that will be memorised by the TerRanger Evo ie. power cycling the Evo will not restore the default I2C address.
46+
47+
#define TERARANGER_EVO_I2C_ANSWER_WHO_AM_I 0xA1
48+
49+
#define TERARANGER_EVO_VALUE_TOO_CLOSE 0x0000
50+
#define TERARANGER_EVO_VALUE_OUT_OF_RANGE 0xffff
51+
52+
static int16_t minimumReadingIntervalMs = 50;
53+
uint8_t triggerValue[0];
54+
volatile int32_t teraRangerMeasurementCm;
55+
static uint32_t timeOfLastMeasurementMs;
56+
static uint8_t dataBuff[3];
57+
58+
static void teraRangerInit(rangefinderDev_t *rangefinder){
59+
busWriteBuf(rangefinder->busDev, TERARANGER_EVO_I2C_REGISTRY_TRIGGER_READING, triggerValue, 1);
60+
}
61+
62+
void teraRangerUpdate(rangefinderDev_t *rangefinder){
63+
if (busReadBuf(rangefinder->busDev, TERARANGER_EVO_I2C_REGISTRY_TRIGGER_READING, dataBuff, 3)) {
64+
teraRangerMeasurementCm = MILLIMETERS_TO_CENTIMETERS(((int32_t)dataBuff[0] << 8 | (int32_t)dataBuff[1]));
65+
66+
if(dataBuff[2] == crc8_update(0, &dataBuff, 2)){
67+
if (teraRangerMeasurementCm == TERARANGER_EVO_VALUE_TOO_CLOSE || teraRangerMeasurementCm == TERARANGER_EVO_VALUE_OUT_OF_RANGE) {
68+
teraRangerMeasurementCm = RANGEFINDER_OUT_OF_RANGE;
69+
}
70+
}
71+
} else {
72+
teraRangerMeasurementCm = RANGEFINDER_HARDWARE_FAILURE;
73+
}
74+
75+
const timeMs_t timeNowMs = millis();
76+
if (timeNowMs > timeOfLastMeasurementMs + minimumReadingIntervalMs) {
77+
// measurement repeat interval should be greater than minimumReadingIntervalMs
78+
// to avoid interference between connective measurements.
79+
timeOfLastMeasurementMs = timeNowMs;
80+
busWriteBuf(rangefinder->busDev, TERARANGER_EVO_I2C_ADDRESS, triggerValue, 1);
81+
}
82+
}
83+
84+
85+
int32_t teraRangerGetDistance(rangefinderDev_t *rangefinder){
86+
UNUSED(rangefinder);
87+
return teraRangerMeasurementCm;
88+
}
89+
90+
static bool deviceDetect(busDevice_t * busDev){
91+
for (int retry = 0; retry < 5; retry++) {
92+
uint8_t whoIamResult;
93+
94+
delay(150);
95+
96+
bool ack = busRead(busDev, TERARANGER_EVO_I2C_REGISTRY_WHO_AM_I, &whoIamResult);
97+
if (ack && whoIamResult == TERARANGER_EVO_I2C_ANSWER_WHO_AM_I) {
98+
return true;
99+
}
100+
};
101+
102+
return false;
103+
}
104+
105+
bool teraRangerDetect(rangefinderDev_t *rangefinder){
106+
rangefinder->busDev = busDeviceInit(BUSTYPE_I2C, DEVHW_TERARANGER_EVO_I2C, 0, OWNER_RANGEFINDER);
107+
if (rangefinder->busDev == NULL) {
108+
return false;
109+
}
110+
111+
if (!deviceDetect(rangefinder->busDev)) {
112+
busDeviceDeInit(rangefinder->busDev);
113+
return false;
114+
}
115+
116+
rangefinder->delayMs = RANGEFINDER_TERA_EVO_TASK_PERIOD_MS;
117+
rangefinder->maxRangeCm = RANGEFINDER_TERA_EVO_MAX_RANGE_CM;
118+
rangefinder->detectionConeDeciDegrees = TERARANGER_EVO_DETECTION_CONE_DECIDEGREES;
119+
rangefinder->detectionConeExtendedDeciDegrees = TERARANGER_EVO_DETECTION_CONE_EXTENDED_DECIDEGREES;
120+
121+
rangefinder->init = &teraRangerInit;
122+
rangefinder->update = &teraRangerUpdate;
123+
rangefinder->read = &teraRangerGetDistance;
124+
125+
return true;
126+
}
127+
128+
#endif
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* This file is part of INAV.
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+
#pragma once
25+
26+
#define RANGEFINDER_TERA_EVO_TASK_PERIOD_MS 70
27+
#define RANGEFINDER_TERA_EVO_MAX_RANGE_CM 600 // max distance depends on model https://www.terabee.com/sensors-modules/lidar-tof-range-finders/#individual-distance-measurement-sensors
28+
29+
bool teraRangerDetect(rangefinderDev_t *dev);

src/main/fc/settings.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ tables:
77
values: ["NONE", "AUTO", "MPU6000", "MPU6500", "MPU9250", "BMI160", "ICM20689", "BMI088", "ICM42605", "BMI270","LSM6DXX", "FAKE"]
88
enum: accelerationSensor_e
99
- name: rangefinder_hardware
10-
values: ["NONE", "SRF10", "VL53L0X", "MSP", "BENEWAKE", "VL53L1X", "US42", "TOF10120_I2C", "FAKE"]
10+
values: ["NONE", "SRF10", "VL53L0X", "MSP", "BENEWAKE", "VL53L1X", "US42", "TOF10120_I2C", "FAKE", "TERARANGER_EVO"]
1111
enum: rangefinderType_e
1212
- name: mag_hardware
1313
values: ["NONE", "AUTO", "HMC5883", "AK8975", "MAG3110", "AK8963", "IST8310", "QMC5883", "MPU9250", "IST8308", "LIS3MDL", "MSP", "RM3100", "VCM5883", "MLX90393", "FAKE"]

src/main/sensors/rangefinder.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "drivers/rangefinder/rangefinder_vl53l1x.h"
4242
#include "drivers/rangefinder/rangefinder_virtual.h"
4343
#include "drivers/rangefinder/rangefinder_us42.h"
44+
#include "drivers/rangefinder/rangefinder_teraranger_evo.h"
4445
#include "drivers/rangefinder/rangefinder_tof10120_i2c.h"
4546

4647
#include "fc/config.h"
@@ -88,6 +89,14 @@ static bool rangefinderDetect(rangefinderDev_t * dev, uint8_t rangefinderHardwar
8889
#endif
8990
break;
9091

92+
case RANGEFINDER_TERARANGER_EVO:
93+
#if defined(USE_RANGEFINDER_TERARANGER_EVO_I2C)
94+
if (teraRangerDetect(dev)) {
95+
rangefinderHardware = RANGEFINDER_TERARANGER_EVO;
96+
rescheduleTask(TASK_RANGEFINDER, TASK_PERIOD_MS(RANGEFINDER_TERA_EVO_TASK_PERIOD_MS));
97+
}
98+
#endif
99+
break;
91100
case RANGEFINDER_VL53L0X:
92101
#if defined(USE_RANGEFINDER_VL53L0X)
93102
if (vl53l0xDetect(dev)) {

src/main/sensors/rangefinder.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,16 @@
2222
#include "drivers/rangefinder/rangefinder.h"
2323

2424
typedef enum {
25-
RANGEFINDER_NONE = 0,
26-
RANGEFINDER_SRF10 = 1,
27-
RANGEFINDER_VL53L0X = 2,
28-
RANGEFINDER_MSP = 3,
29-
RANGEFINDER_BENEWAKE = 4,
30-
RANGEFINDER_VL53L1X = 5,
31-
RANGEFINDER_US42 = 6,
32-
RANGEFINDER_TOF10102I2C = 7,
33-
RANGEFINDER_FAKE = 8,
25+
RANGEFINDER_NONE = 0,
26+
RANGEFINDER_SRF10 = 1,
27+
RANGEFINDER_VL53L0X = 2,
28+
RANGEFINDER_MSP = 3,
29+
RANGEFINDER_BENEWAKE = 4,
30+
RANGEFINDER_VL53L1X = 5,
31+
RANGEFINDER_US42 = 6,
32+
RANGEFINDER_TOF10102I2C = 7,
33+
RANGEFINDER_FAKE = 8,
34+
RANGEFINDER_TERARANGER_EVO = 9,
3435
} rangefinderType_e;
3536

3637
typedef struct rangefinderConfig_s {

src/main/target/common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
#define USE_RANGEFINDER_VL53L0X
8282
#define USE_RANGEFINDER_VL53L1X
8383
#define USE_RANGEFINDER_US42
84-
#define USE_RANGEFINDER_TOF10120_I2C
84+
#define USE_RANGEFINDER_TERARANGER_EVO_I2C
8585

8686
// Allow default optic flow boards
8787
#define USE_OPFLOW

0 commit comments

Comments
 (0)