From 64467a8b81d87d2d396d4397ef86e4618daf54c1 Mon Sep 17 00:00:00 2001 From: Adam <12676475+adam-sampson@users.noreply.github.com> Date: Thu, 23 Aug 2018 23:02:15 -0400 Subject: [PATCH 1/7] Modified code to work on 32 bit Teensy platform Changed wire library to i2c_t3 (which comes with teensy) and changed references to int to references to short (to force the teensy to use 16 bit integers) --- I2CFunctions.h | 3 ++- LidarController.h | 9 ++++++--- LidarObject.h | 4 +++- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/I2CFunctions.h b/I2CFunctions.h index f08831b..6a5d664 100644 --- a/I2CFunctions.h +++ b/I2CFunctions.h @@ -2,7 +2,8 @@ #define I2C_FUNCTIONS_H #include -#include +// #include +#include #define STOP_CONDITION_I2C true class I2CFunctions { diff --git a/LidarController.h b/LidarController.h index d6c537d..5123937 100644 --- a/LidarController.h +++ b/LidarController.h @@ -3,7 +3,8 @@ #include "I2CFunctions.h" #include "LidarObject.h" -#include +// #include +#include // Wait between I2C transactions in µs // One bit every 10µs (2.5µs in 400kHz) @@ -312,13 +313,15 @@ class LidarController { if(biasCorrection) nack = I2C.write(lidars[Lidar]->address, REG_ACQ_COMMAND, DATA_MEASURE_WITH_BIAS); else nack = I2C.write(lidars[Lidar]->address, REG_ACQ_COMMAND, DATA_MEASURE_WITHOUT_BIAS); shouldIncrementNack(Lidar, nack); + return nack; }; /******************************************************************************* distance: - Read the measured value from data registers *******************************************************************************/ - uint8_t distance(uint8_t Lidar, int * data) { + //uint8_t distance(uint8_t Lidar, int * data) { + uint8_t distance(uint8_t Lidar, short * data) { uint8_t distanceArray[2]; uint8_t nackCatcher = I2C.readWord(lidars[Lidar]->address, MEASURED_VALUE_REGISTER, distanceArray); shouldIncrementNack(Lidar, nackCatcher); @@ -402,7 +405,7 @@ class LidarController { We could use the async() method in ACQUISITION_DONE, but it would need to spin one time more before starting the acquisition again *******************************************************************************/ - uint8_t distanceAndAsync(uint8_t Lidar, int * data) { + uint8_t distanceAndAsync(uint8_t Lidar, short * data) { uint8_t nackCatcher = distance(Lidar, data); // if error reading the value, try ONCE again if (nackCatcher) diff --git a/LidarObject.h b/LidarObject.h index ee54a7c..60ad664 100644 --- a/LidarObject.h +++ b/LidarObject.h @@ -1,5 +1,6 @@ #include -#include +//#include +#include #include "I2CFunctions.h" // We got a Lidar object per laser. @@ -128,6 +129,7 @@ class LidarObject { *******************************************************************************/ bool resetNacksCount(){ nacksCount = 0; + return 1; }; /******************************************************************************* From 4a4bad16841ffd57a7759312bfac2dbe074ea0cf Mon Sep 17 00:00:00 2001 From: Adam <12676475+adam-sampson@users.noreply.github.com> Date: Thu, 23 Aug 2018 23:55:36 -0400 Subject: [PATCH 2/7] Changed short to int16_t --- LidarController.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/LidarController.h b/LidarController.h index 5123937..2616473 100644 --- a/LidarController.h +++ b/LidarController.h @@ -321,7 +321,7 @@ class LidarController { - Read the measured value from data registers *******************************************************************************/ //uint8_t distance(uint8_t Lidar, int * data) { - uint8_t distance(uint8_t Lidar, short * data) { + uint8_t distance(uint8_t Lidar, int16_t * data) { uint8_t distanceArray[2]; uint8_t nackCatcher = I2C.readWord(lidars[Lidar]->address, MEASURED_VALUE_REGISTER, distanceArray); shouldIncrementNack(Lidar, nackCatcher); @@ -356,7 +356,7 @@ class LidarController { This has to be worked on, this is the original implementation without the blocking architecture *******************************************************************************/ - int velocity(uint8_t Lidar, int * data) { + int16_t velocity(uint8_t Lidar, int16_t * data) { // Set in velocity mode I2C.write(lidars[Lidar]->address, REG_ACQ_CONFIG, DATA_VELOCITY_MODE_DATA); // Write 0x04 to register 0x00 to start getting distance readings @@ -365,7 +365,7 @@ class LidarController { uint8_t velocityArray[1]; uint8_t nack = I2C.readByte(lidars[Lidar]->address, REG_VELOCITY, velocityArray); - return((int)((char)velocityArray[0])); + return((int16_t)((char)velocityArray[0])); }; /******************************************************************************* @@ -405,7 +405,7 @@ class LidarController { We could use the async() method in ACQUISITION_DONE, but it would need to spin one time more before starting the acquisition again *******************************************************************************/ - uint8_t distanceAndAsync(uint8_t Lidar, short * data) { + uint8_t distanceAndAsync(uint8_t Lidar, int16_t * data) { uint8_t nackCatcher = distance(Lidar, data); // if error reading the value, try ONCE again if (nackCatcher) From b22c0f5727b8acac407d7c4fc41bc9edfb5d0057 Mon Sep 17 00:00:00 2001 From: Adam <12676475+adam-sampson@users.noreply.github.com> Date: Thu, 23 Aug 2018 23:57:51 -0400 Subject: [PATCH 3/7] Describe changes on this fork --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index d9c0460..249397e 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,8 @@ Introduction -------------- This library tends to improve the **efficiency** and the **robustness** of the poor original acquisition library. It is originally released for the LidarLite v2 (deprecated) and compatible with the LidarLite v3 (release from Garmin) lasermeter. +This fork of the library has been modified to work on a Teensy 3.x (3.5) 32-bit board. It requires that you have i2c_t3.h library (https://github.com/nox771/i2c_t3) installed. + Improvements over the original library -------------------- - *Asynchronous acquisition* From 0c8894b69f4e9591038f618473bafe750843f942 Mon Sep 17 00:00:00 2001 From: Alexis Paques Date: Mon, 15 Oct 2018 08:55:40 +0200 Subject: [PATCH 4/7] Allow Teensy use --- I2CFunctions.h | 5 ++++- LidarController.h | 3 --- LidarObject.h | 2 -- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/I2CFunctions.h b/I2CFunctions.h index 6a5d664..7b59773 100644 --- a/I2CFunctions.h +++ b/I2CFunctions.h @@ -2,8 +2,11 @@ #define I2C_FUNCTIONS_H #include -// #include +#if defined(__MK20DX256__)|| defined(__MK20DX128__) #include +#else +#include +#endif #define STOP_CONDITION_I2C true class I2CFunctions { diff --git a/LidarController.h b/LidarController.h index 2616473..32ef87a 100644 --- a/LidarController.h +++ b/LidarController.h @@ -3,9 +3,6 @@ #include "I2CFunctions.h" #include "LidarObject.h" -// #include -#include - // Wait between I2C transactions in µs // One bit every 10µs (2.5µs in 400kHz) // Wait at least 5 bits to wait for slave answer diff --git a/LidarObject.h b/LidarObject.h index 60ad664..e4a71c2 100644 --- a/LidarObject.h +++ b/LidarObject.h @@ -1,6 +1,4 @@ #include -//#include -#include #include "I2CFunctions.h" // We got a Lidar object per laser. From b6cef4fc4aca2a5497b320f56809ccf4ed95ee51 Mon Sep 17 00:00:00 2001 From: Alexis Paques Date: Mon, 15 Oct 2018 08:57:29 +0200 Subject: [PATCH 5/7] Adding Teensy example --- example/Teensy/Teensy.ino | 47 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 example/Teensy/Teensy.ino diff --git a/example/Teensy/Teensy.ino b/example/Teensy/Teensy.ino new file mode 100644 index 0000000..90fbaf4 --- /dev/null +++ b/example/Teensy/Teensy.ino @@ -0,0 +1,47 @@ +#include +#include +#include +#include +#include + +#define WIRE400K true +// Trigger pin, can be unplugged +#define Z1_LASER_TRIG 11 +// Enable pin, IMPORTANT +#define Z1_LASER_EN 12 +// Mode pin, can be unplugged +#define Z1_LASER_PIN 13 +//Define address of lasers +//Thoses are written during initialisation +// default address : 0x62 +#define Z1_LASER_AD 0x6E + +#define NUMBER_OF_LASERS 1 + +// Create lasers +static LidarController Controller; +static LidarObject LZ1; + +void setup() +{ + Serial.begin(115200); + // Configure lasers + LZ1.begin(Z1_LASER_EN, Z1_LASER_PIN, Z1_LASER_TRIG, Z1_LASER_AD, 2, DISTANCE, 'A'); + LZ1.setCallbackDistance(&distance_callback); + // Add the laser to the Controller + Controller.add(&LZ1, 0); + + delay(100); + Controller.begin(WIRE400K); + delay(100); +} + +void distance_callback(LidarObject* self){ + Serial.println(self->distance); +} + +void loop() +{ + Controller.spinOnce(); + // Rest of your non blocking application. +} From 8f29a66b34cdc0e308bc8d756309819fb7dffbe9 Mon Sep 17 00:00:00 2001 From: Alexis Paques Date: Mon, 15 Oct 2018 09:00:17 +0200 Subject: [PATCH 6/7] Adding Adam Sampson in the credits --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 249397e..04d3a6a 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Introduction -------------- This library tends to improve the **efficiency** and the **robustness** of the poor original acquisition library. It is originally released for the LidarLite v2 (deprecated) and compatible with the LidarLite v3 (release from Garmin) lasermeter. -This fork of the library has been modified to work on a Teensy 3.x (3.5) 32-bit board. It requires that you have i2c_t3.h library (https://github.com/nox771/i2c_t3) installed. +The Teensy 3.x (3.5) 32-bit board support has been added by [adam-sampson](http://github.com/adam-sampson). If you use it, replace `#include ` by `#include ` in the examples and install the [i2c_t3 library](https://github.com/nox771/i2c_t3) Improvements over the original library -------------------- @@ -446,3 +446,4 @@ Pull requests are welcome :D Credits ------ * Alexis Paques (alexis[dot]paques[at]gmail[dot]com) +* Adam Sampson From 8146695ebbe05f78d240015b8ce50960e72b907b Mon Sep 17 00:00:00 2001 From: Alexis Paques Date: Mon, 15 Oct 2018 09:03:39 +0200 Subject: [PATCH 7/7] Using TEENSYDUINO define to know if this is a Teensy board --- I2CFunctions.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/I2CFunctions.h b/I2CFunctions.h index 7b59773..49d8836 100644 --- a/I2CFunctions.h +++ b/I2CFunctions.h @@ -2,7 +2,10 @@ #define I2C_FUNCTIONS_H #include -#if defined(__MK20DX256__)|| defined(__MK20DX128__) + +// For more defines for Teensy (granularity), see: +// https://arduino.stackexchange.com/questions/21137/arduino-how-to-get-the-board-type-in-code +#if defined(TEENSYDUINO) #include #else #include