chipKIT® Development Platform

Inspired by Arduino™

Lidar laser v2 not working on Max32?

Created Sun, 26 May 2019 22:06:29 +0000 by aldoz


aldoz

Sun, 26 May 2019 22:06:29 +0000

Hi everyone, I'm using the laser lidar with the arduino MEGA2560 with excellent results and I'm trying to make that laser work even with the max32 without succeeding though. The laser has 4 cables: 5v, gnd, sda and scl. Unfortunately, when I run the code (which, as I said, works perfectly on Arduino) I get a block on the card. Nothing appears in the terminal; it seems that the serial communication is totally blocked. Tried to do many tests but nothing! do you have a solution?

#include <Wire.h>
#define    LIDARLite_ADDRESS   0x62          // Default I2C Address of LIDAR-Lite.
#define    RegisterMeasure     0x00          // Register to write to initiate ranging.
#define    MeasureValue        0x04          // Value to initiate ranging.
#define    RegisterHighLowB    0x8f          // Register to get both High and Low bytes in 1 call.


void setup(){
  Serial.begin(9600); //Opens serial connection at 9600bps.     
  Wire.begin();          // Opens & joins the irc bus as master
  delay(100);        // Waits to make sure everything is powered up before sending or receiving data 
}

void loop()
{
  // Write 0x04 to register 0x00
  uint8_t nackack = 100; // Setup variable to hold ACK/NACK resopnses     
  while (nackack != 0)
  { // While NACK keep going (i.e. continue polling until sucess message (ACK) is received )
    Wire.beginTransmission(LIDARLite_ADDRESS);
    Wire.write(RegisterMeasure);
    Wire.write(MeasureValue);
    nackack = Wire.endTransmission();
    delay(1); // Wait 1 ms to prevent overpolling
  }

  byte distanceArray[2]; // array to store distance bytes from read function
  
  // Read 2byte distance from register 0x8f
  nackack = 100; // Setup variable to hold ACK/NACK resopnses     
  while (nackack != 0)
  { // While NACK keep going (i.e. continue polling until sucess message (ACK) is received )
    Wire.beginTransmission(LIDARLite_ADDRESS);
    Wire.write(RegisterHighLowB);
    nackack = Wire.endTransmission();
    if (0 == nackack) 
    {
        Wire.requestFrom(LIDARLite_ADDRESS, 2);
        if (Wire.available() == 2)
        {
            distanceArray[0] = Wire.read();
            distanceArray[1] = Wire.read();
        }
        else
        {
            nackack = 100;
        }
    }   
    
    delay(1); // Wait 1 ms to prevent overpolling
  }
  int distance = (distanceArray[0] << 8) + distanceArray[1];  // Shift high byte [0] 8 to the left and add low byte [1] to create 16-bit int

  Serial.println(distance);
}

majenko

Mon, 27 May 2019 11:39:49 +0000

The Wire library is, by its very design, a blocking library. If it can't communicate with the device it just locks up. It's very badly designed (you can blame Arduino for that, and we're stuck with it).

Our Wire library is merely a wrapper around the DTWI0 object, which uses the SCL/SDA pins (duplicated with pins 21/20 respectively), so make sure you're using the right pins.

Also I would recommend using the DTWI library directly. It's not as easy to use, but far more flexible and gets around the problems of crashing due to locking up waiting for a response that never arrives.

Secondly, our serial / USB system is somewhat more complex that Arduino. You have the ability to configure Serial over USB, which you don't get on an Arduino. It should, by default, be set to "Custom / Disabled" (in the hardware menu), which means "Serial" is the actial serial port. However if that has been changed to "Serial" or "Serial, Keyboard & Mouse" then "Serial" becomes a USB port (for which you would need to add the ethernet shield or some other similar board that breaks out the USB pins to a socket), and the real serial port becomes "Serial0" - so check that setting is correct.


aldoz

Mon, 27 May 2019 12:47:02 +0000

Hi majenko and thank you for help!

Our Wire library is merely a wrapper around the DTWI0 object, which uses the SCL/SDA pins (duplicated with pins 21/20 respectively), so make sure you're using the right pins.

I am just using SDA and SCL pin in the Max32. (obviously 5v and gnd too).

Also I would recommend using the DTWI library directly. It's not as easy to use, but far more flexible and gets around the problems of crashing due to locking up waiting for a response that never arrives.

Mmm at least this is an active option!.. but never used DTWI library.. can this library do the same things as the wire library?

Secondly, our serial / USB system is somewhat more complex that Arduino. You have the ability to configure Serial over USB, which you don't get on an Arduino. It should, by default, be set to "Custom / Disabled" (in the hardware menu), which means "Serial" is the actial serial port. However if that has been changed to "Serial" or "Serial, Keyboard & Mouse" then "Serial" becomes a USB port (for which you would need to add the ethernet shield or some other similar board that breaks out the USB pins to a socket), and the real serial port becomes "Serial0" - so check that setting is correct.

Yeah in my Arduino IDE I selected "Custom / Disabled"!

I'm a bit confused and frustrated because with the max32 I was able to make both a gyroscope,servos (using an adafruit 16ch servo driver card), force sensors .. but the laser no way ..


majenko

Mon, 27 May 2019 16:08:54 +0000

Sure, DTWI can do everything that Wire can - it just does it in a different way. It's more broken down into individual steps, so you have greater control over what you do with it.

I have a number of libraries written specially to use it. Here's one, where you can see the "readRegister" and "writeRegister" functions that I commonly use in most of my implementations:


aldoz

Mon, 27 May 2019 19:26:25 +0000

Sure, DTWI can do everything that Wire can - it just does it in a different way. It's more broken down into individual steps, so you have greater control over what you do with it. I have a number of libraries written specially to use it. Here's one, where you can see the "readRegister" and "writeRegister" functions that I commonly use in most of my implementations:

Mmm really interesting and really thank you majenko but this is going far over my capabilities :
Just can't convert my wire library code using your library commands!. Wire.beginTransmission(x); Wire.write(x); Wire.requestFrom(x, x); Wire.available(); just not presents, in these forms, in your library!


majenko

Mon, 27 May 2019 22:04:24 +0000

At a guess I suppose you may be getting stuck in your while loop waiting for nackack to be anything but zero.

You should examine what is returned from the Wire functions at each stage. The different numbers mean different things. It will shed some light on what is happening.


aldoz

Tue, 28 May 2019 16:23:45 +0000

At a guess I suppose you may be getting stuck in your while loop waiting for nackack to be anything but zero. You should examine what is returned from the Wire functions at each stage. The different numbers mean different things. It will shed some light on what is happening.

thank you again majenko!