diff --git a/Makefile b/Makefile index ffdd45c..4592e0c 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,5 @@ all: mkdir -p bin g++ examples/llv3.cpp src/lidarlite_v3.cpp -I . -o bin/llv3.out + g++ examples/llv3_2.cpp src/lidarlite_v3.cpp -I . -o bin/llv3_2.out + g++ examples/llv3_stream_all.cpp src/lidarlite_v3.cpp -I . -o bin/llv3_stream_all.out diff --git a/examples/llv3.cpp b/examples/llv3.cpp index 515ff08..a398157 100644 --- a/examples/llv3.cpp +++ b/examples/llv3.cpp @@ -26,6 +26,7 @@ LIDARLite_v3 myLidarLite; int main() { __u16 distance; + __u16 signalstrength; __u8 busyFlag; // Initialize i2c peripheral in the cpu core @@ -46,8 +47,9 @@ int main() // This method will result in faster I2C rep rates. myLidarLite.takeRange(); distance = myLidarLite.readDistance(); + signalstrength = myLidarLite.readsignalstrength(); - printf("%4d\n", distance); + printf("%4d,%4d\n", distance,signalstrength); } } } diff --git a/examples/llv3_2.cpp b/examples/llv3_2.cpp new file mode 100644 index 0000000..7a54d66 --- /dev/null +++ b/examples/llv3_2.cpp @@ -0,0 +1,119 @@ +/*------------------------------------------------------------------------------ + This example illustrates how to use the LIDAR-Lite library to gain quick + access to the basic functions of LIDAR-Lite via the Raspberry Pi interface. + Additionally, it provides users of any platform with a template for their + own application code. + + Copyright (c) 2019 Garmin Ltd. or its subsidiaries. + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +------------------------------------------------------------------------------*/ + +#include +#include + +#include + +LIDARLite_v3 myLidarLite; + +int main() +{ + __u16 distance; + __u16 signalstrength; + __u8 busyFlag; + //__u32 distance1; + //__u32 signalstrength1; + double distance1; + double signalstrength1; + double i_avg; + int i; + i_avg = 25; + i = -1; + distance1 = 0; + signalstrength1 = 0; + + // Initialize i2c peripheral in the cpu core + myLidarLite.i2c_init(); + + // Optionally configure LIDAR-Lite + myLidarLite.configure(0); + + + while(1) + { + // Each time through the loop, check BUSY + busyFlag = myLidarLite.getBusyFlag(); + + if (busyFlag == 0x00) + { + i = i + 1; + // When no longer busy, immediately initialize another measurement + // and then read the distance data from the last measurement. + // This method will result in faster I2C rep rates. + myLidarLite.takeRange(); + distance = myLidarLite.readDistance(); + signalstrength = myLidarLite.readsignalstrength(); + //distance1 = distance1 + (double)distance/100; + //signalstrength1 = signalstrength1 + (double)signalstrength/100; + //printf("%4d,%4d\n", distance, signalstrength); + + distance1 = distance1 + ((double)distance)/i_avg; + signalstrength1 = signalstrength1 + ((double)signalstrength)/i_avg; + //printf("%d,%d\n", distance1, signalstrength1); + + if (i>i_avg-1){ + //printf("%d,%d\n", distance1, signalstrength1); + printf("%f,%f\n", distance1, signalstrength1); + fflush(stdout); + i = -1; + + distance1 = 0; + signalstrength1 = 0; + } + + + + } + } +} + + +/* + * ================================================================ + * The set of instructions below illustrates one method of setting + * an alternate I2C device address in the LIDAR-Lite v3. See the + * operator manual and C++ libraries for further details. + * ================================================================ + */ + +/* +#define i2cSecondaryAddr 0x44 // Set I2C address of LIDAR-Lite v3 to 0x44 + +int main() +{ + __u16 distance; + + // Initialize i2c peripheral in the cpu core + myLidarLite.i2c_init(); + + // Set an alternate i2c address in the LIDAR-Lite + // The 2nd argument, if non-zero, disables the default addr 0x62 + myLidarLite.setI2Caddr(i2cSecondaryAddr, true); + + while(1) + { + myLidarLite.waitForBusy(i2cSecondaryAddr); + myLidarLite.takeRange(i2cSecondaryAddr); + distance = myLidarLite.readDistance(i2cSecondaryAddr); + + printf("%4d\n", distance); + } +} +*/ diff --git a/examples/llv3_stream_all.cpp b/examples/llv3_stream_all.cpp new file mode 100644 index 0000000..8b0372a --- /dev/null +++ b/examples/llv3_stream_all.cpp @@ -0,0 +1,59 @@ +/*------------------------------------------------------------------------------ + This example illustrates how to use the LIDAR-Lite library to gain quick + access to the basic functions of LIDAR-Lite via the Raspberry Pi interface. + Additionally, it provides users of any platform with a template for their + own application code. + + Copyright (c) 2019 Garmin Ltd. or its subsidiaries. + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +------------------------------------------------------------------------------*/ + +#include +#include + +#include + +LIDARLite_v3 myLidarLite; + +int main() +{ + __u16 distance; + __u16 signalstrength; + __u8 busyFlag; + + // Initialize i2c peripheral in the cpu core + myLidarLite.i2c_init(); + + // Optionally configure LIDAR-Lite + myLidarLite.configure(0); + + + while(1) + { + // Each time through the loop, check BUSY + busyFlag = myLidarLite.getBusyFlag(); + + if (busyFlag == 0x00) + { + // When no longer busy, immediately initialize another measurement + // and then read the distance data from the last measurement. + // This method will result in faster I2C rep rates. + myLidarLite.takeRange(); + distance = myLidarLite.readDistance(); + signalstrength = myLidarLite.readsignalstrength(); + + printf("%d,%d\n", distance, signalstrength); + fflush(stdout); + } + } +} + + diff --git a/include/lidarlite_v3.h b/include/lidarlite_v3.h index 6bcadb6..f171d7e 100644 --- a/include/lidarlite_v3.h +++ b/include/lidarlite_v3.h @@ -28,6 +28,7 @@ #define LLv3_STATUS 0x01 #define LLv3_SIG_CNT_VAL 0x02 #define LLv3_ACQ_CONFIG 0x04 +#define LLv3_SIGNAL_STR 0x0e #define LLv3_DISTANCE 0x0f #define LLv3_REF_CNT_VAL 0x12 #define LLv3_UNIT_ID_HIGH 0x16 @@ -50,6 +51,7 @@ class LIDARLite_v3 void configure (__u8 configuration = 0, __u8 lidarliteAddress = LIDARLITE_ADDR_DEFAULT); void setI2Caddr (__u8 newAddress, __u8 disableDefault, __u8 lidarliteAddress = LIDARLITE_ADDR_DEFAULT); __u16 readDistance(__u8 lidarliteAddress = LIDARLITE_ADDR_DEFAULT); + __u16 readsignalstrength(__u8 lidarliteAddress = LIDARLITE_ADDR_DEFAULT); void waitForBusy (__u8 lidarliteAddress = LIDARLITE_ADDR_DEFAULT); __u8 getBusyFlag (__u8 lidarliteAddress = LIDARLITE_ADDR_DEFAULT); void takeRange (__u8 lidarliteAddress = LIDARLITE_ADDR_DEFAULT); diff --git a/src/lidarlite_v3.cpp b/src/lidarlite_v3.cpp index f8397a6..beaa83b 100644 --- a/src/lidarlite_v3.cpp +++ b/src/lidarlite_v3.cpp @@ -275,6 +275,27 @@ __u16 LIDARLite_v3::readDistance(__u8 lidarliteAddress) return ((distBytes[0] << 8) | distBytes[1]); } /* LIDARLite_v3::readDistance */ +/*------------------------------------------------------------------------------ + Read Signal Strength + Read and return result of signal strength measurement. + + Parameters + ------------------------------------------------------------------------------ + lidarliteAddress: Default 0x62. Fill in new address here if changed. See + operating manual for instructions. +------------------------------------------------------------------------------*/ +__u16 LIDARLite_v3::readsignalstrength(__u8 lidarliteAddress) +{ + __u8 strengthBytes[2] = {0}; + + // Read two bytes from register 0x0f and 0x10 (autoincrement) + i2cRead((LLv3_SIGNAL_STR | 0x80), strengthBytes, 2, lidarliteAddress); + + // Shift high byte and OR in low byte + return ((strengthBytes[0] << 8) | strengthBytes[1]); +} /* LIDARLite_v3::readsignalstrength */ + + /*------------------------------------------------------------------------------ Write Perform I2C write to device.