11/*
2- * This file is part of Cleanflight .
2+ * This file is part of INAV .
33 *
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.
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/.
87 *
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.
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.
1320 *
1421 * You should have received a copy of the GNU General Public License
15- * along with Cleanflight. If not, see < http://www.gnu.org/licenses/> .
22+ * along with this program. If not, see http://www.gnu.org/licenses/.
1623 */
17-
1824#include <stdbool.h>
1925#include <stdint.h>
2026
2430
2531#if defined(USE_RANGEFINDER ) && defined(USE_RANGEFINDER_TERARANGER_EVO_I2C )
2632
33+ //MANUAL
34+ //https://www.mouser.lt/datasheet/2/944/User_Manual_for_TeraRanger_Evo_single_point_distan-1729051.pdf?srsltid=AfmBOork4pNRMVMmnZ12zIk3yt79aeE066Hq5VpcBHv4wAnv-FSBLAKc
35+
2736#include "build/build_config.h"
2837
2938#include "common/crc.h"
4958#define TERARANGER_EVO_VALUE_TOO_CLOSE 0x0000
5059#define TERARANGER_EVO_VALUE_OUT_OF_RANGE 0xffff
5160
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 ];
5761
58- static void teraRangerInit (rangefinderDev_t * rangefinder ){
59- busWriteBuf (rangefinder -> busDev , TERARANGER_EVO_I2C_REGISTRY_TRIGGER_READING , triggerValue , 1 );
60- }
62+ static struct {
63+ int32_t teraRangerMeasurementCm ;
64+ uint8_t dataBuff [3 ];
65+ } teraRangerEvo = {};
6166
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 ]));
6567
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- }
8368
84-
85- int32_t teraRangerGetDistance (rangefinderDev_t * rangefinder ){
86- UNUSED (rangefinder );
87- return teraRangerMeasurementCm ;
69+ static void teraRangerInit (rangefinderDev_t * rangefinder ){
70+ busWrite (rangefinder -> busDev , TERARANGER_EVO_I2C_REGISTRY_TRIGGER_READING , 0x00 ); //request measure to be data prepared for first call of teraRangerUpdate
8871}
8972
9073static bool deviceDetect (busDevice_t * busDev ){
@@ -102,6 +85,37 @@ static bool deviceDetect(busDevice_t * busDev){
10285 return false;
10386}
10487
88+ static bool checkCrc (void ){
89+ return teraRangerEvo .dataBuff [2 ] == crc8_update (0 , teraRangerEvo .dataBuff , 2 );
90+ }
91+
92+ void teraRangerUpdate (rangefinderDev_t * rangefinder ){
93+ if (busReadBuf (rangefinder -> busDev , TERARANGER_EVO_I2C_REGISTRY_TRIGGER_READING , teraRangerEvo .dataBuff , 3 )) {
94+ if (!checkCrc ()) {
95+ teraRangerEvo .teraRangerMeasurementCm = RANGEFINDER_NO_NEW_DATA ;
96+ return ;
97+ }
98+
99+ const int32_t teraRangerMeasurementMM = ((int32_t )teraRangerEvo .dataBuff [0 ] << 8 | (int32_t )teraRangerEvo .dataBuff [1 ]);
100+ if (teraRangerMeasurementMM == TERARANGER_EVO_VALUE_TOO_CLOSE || teraRangerMeasurementMM == TERARANGER_EVO_VALUE_OUT_OF_RANGE ) {
101+ teraRangerEvo .teraRangerMeasurementCm = RANGEFINDER_OUT_OF_RANGE ;
102+ return ;
103+ }
104+
105+ teraRangerEvo .teraRangerMeasurementCm = MILLIMETERS_TO_CENTIMETERS (teraRangerMeasurementMM );
106+ } else {
107+ teraRangerEvo .teraRangerMeasurementCm = RANGEFINDER_HARDWARE_FAILURE ;
108+ }
109+
110+ busWrite (rangefinder -> busDev , TERARANGER_EVO_I2C_REGISTRY_TRIGGER_READING , 0x00 ); //request to next measure, scheduler is much slower than 500uS to we need to wait between write and read
111+ }
112+
113+
114+ int32_t teraRangerGetDistance (rangefinderDev_t * rangefinder ){
115+ UNUSED (rangefinder );
116+ return teraRangerEvo .teraRangerMeasurementCm ;
117+ }
118+
105119bool teraRangerDetect (rangefinderDev_t * rangefinder ){
106120 rangefinder -> busDev = busDeviceInit (BUSTYPE_I2C , DEVHW_TERARANGER_EVO_I2C , 0 , OWNER_RANGEFINDER );
107121 if (rangefinder -> busDev == NULL ) {
0 commit comments