-
Notifications
You must be signed in to change notification settings - Fork 0
/
xts1_functions.hpp
173 lines (153 loc) · 5.07 KB
/
xts1_functions.hpp
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
#include "Arduino.h"
/**************************************************************************************
* Copyright (C) 2024 JMKO
*
* Author: Jonathan Olund
*Pins from bottom lens pointing to right
* Lidar 328p Micro/pro
* Vcc 5v Vcc
* Gnd Gnd Gnd
* SCL SCL(ADC5) 2
* SDA SDA(ADC4) 3
* Mode Gnd Gnd
***************************************************************************************/
#pragma once
#include <Wire.h>
#include "sen0585.h"
int nDevices = 0;
uint16_t timeout_count = 0;
uint16_t readlimit = 0;
uint16_t dist[2];
int inch;
int sensAddr[2] = {0x10, 0x11};
/*************************kalman Filter*******************************/
bool filters_klm_on = true;
uint16_t klm_factor = 300;
uint16_t klm_threshold = 300;
int last_time = 0;
uint16_t last_distance = 0;
/*************************kalman Filter*******************************/
uint16_t doKlmFilter(uint16_t & distance)
{
if(filters_klm_on)
{
bool bfilterstart = false;
int currentTime = millis();
if(abs(currentTime - last_time)<300)//300ms
bfilterstart = true;
last_time = currentTime;
if((distance < 64000)&&(last_distance < 64000)&& bfilterstart)
{
int32_t diff = distance - last_distance;
if (abs(diff) < klm_threshold)
{
uint32_t result = 0;
result = (((distance * klm_factor) + (last_distance * (1000-klm_factor))) / 1000);
distance = result;
}
}
last_distance = distance;
}
}
/********************************************************/
uint8_t distance(int sensorNum) {
//uint8_t data[10];
//(set register address)
Wire.beginTransmission(sensAddr[sensorNum]);
Wire.write(byte(23));
Wire.write(byte(0));
Wire.endTransmission();
Wire.requestFrom(sensAddr[sensorNum], 2);//请求从设备读取2个字节的数据(Request to read 2 bytes of data from the device)
if (Wire.available() >= 2) {
int lowByte = Wire.read(); //Read low byte
int highByte = Wire.read(); //Read high byte
uint16_t distance = (highByte << 8) | lowByte;
if(nDevices > 0)
{
timeout_count = 0;
Serial.print(nDevices);
Serial.print(" ");
Serial.println("connected");
doKlmFilter(distance);
if(distance < 64000)
{
inch = distance/25;
Serial.print(inch);
Serial.println(" on left");
}else
{
Serial.print("ErrorCode:");
Serial.println(distance);
}
dist[sensorNum] = inch;
}
}
return inch;
}
uint16_t sens_readReg(int sens, uint8_t regaddr)
{
int LSB;
int MSB;
Wire.beginTransmission(sensAddr[sens]);
Wire.write(byte(regaddr));
//Wire.write(byte(0));
Wire.endTransmission();
Wire.requestFrom(sens, 2);
delay(200);
if (Wire.available() >=2){
LSB = Wire.read();
MSB = Wire.read();
}
uint16_t output = ((MSB << 8) | LSB);
//Serial.print(output,HEX);
return output;
}
uint16_t sens_writeReg(int sens, uint8_t regaddr, uint8_t value)
{
Wire.beginTransmission(sensAddr[sens]);
Wire.write(byte(regaddr));
Wire.write(byte(0));
Wire.write(byte(value & 0xFF));
Wire.write(byte(value >> 8));
Wire.endTransmission();
//Wire.requestFrom(sensAddr[sens],2);
//byte LSB = Wire.read();
//byte MSB = Wire.read();
//uint8_t output = ((MSB << 8) | LSB);
//Serial.print("Wrote: 0x");
//Serial.print(output, HEX);
//Serial.print(" to ");
//Serial.println(regaddr);
return Wire.endTransmission();
}
void startDist(int sens)
{
sens_writeReg(sens, 0, 0); //电流寄存器设置为0(Set the current register to 0)
sens_writeReg(sens, 61, 0x000); //设置为主动读取结果 (Set the outgoing mode to 0x0000 for active reading result mode).
sens_writeReg(sens, 66, 200);//周期寄存器 (Set the measurement cycle register to 200ms)
sens_writeReg(sens, 3, 0x0001); //start measuring
}
void stopDist(int sens)
{
sens_writeReg(sens, 0, 0); //电流寄存器设置为0(Set the current register to 0)
sens_writeReg(sens, 61, 0x000); //设置为主动读取结果 (Set the outgoing mode to 0x0000 for active reading result mode).
sens_writeReg(sens, 66, 200);//周期寄存器 (Set the measurement cycle register to 200ms)
sens_writeReg(sens, 3, 0x0002); //stop measuring
}
uint16_t initdev(int SensEnable) {
Serial.print("checking: 0x");
Serial.println(sensAddr[0],HEX);
sen0585_init(); // Initialize I2C communication with SEN0585
// Read the 16-bit value from the register
uint16_t value = sen0585_read16BitRegister(SEN0585_DISTANCE_REGISTER);
Serial.print("Original value: 0x");
Serial.println(value, HEX); // Print original value in hexadecimal
// Increment the low byte by 0x01
uint16_t newValue = sen0585_incrementLowByte(value);
Serial.print("New value: 0x");
Serial.println(newValue, HEX); // Print updated value in hexadecimal
// Write the new value back to the register
sen0585_write16BitRegister(SEN0585_DISTANCE_REGISTER, newValue);
Serial.println(" init done");
return nDevices;
}