Lidar-Lite with Netduino

Maybe I’m biting off more than I can chew, but I could use some serious kickstart on how to communicate with the Lidar-Lite (v2 specifically) in .NET C# style.

I have some code but have no idea how close or how far off I am. The docs for lidar-lite’s I2C protocol are very low-level oriented and I can’t figure out how to adapt that to what little I know of .NET I2C interactions. Could I trouble anyone for a quick blurb to read the distance?

Eg the .NET equivalent of this?

int llGetDistance(){
  llWriteAndWait(0x00,0x04); // Write 0x04 to register 0x00 to start getting distance readings
  byte myArray[2]; // array to store bytes from read function
  llReadAndWait(0x8f,2,myArray); // Read 2 bytes from 0x8f
  int distance = (myArray[0] << 8) + myArray[1];  // Shift high byte [0] 8 to the left and add low byte [1] to create 16-bit int
  return(distance);
}

PS Here is the code I had so far which notably seemed not to work and was one of a handful of iterations and I have no idea what I’m doing

byte[] buf = { 0x00 };
I2CDevice.I2CWriteTransaction wtx = I2CDevice.CreateWriteTransaction(new byte[] { 0, 0x04 });

// create a read from 0x8f
byte[] readAddr = new byte[1] { 0x8f };
byte[] dist = new byte[2];
I2CDevice.I2CWriteTransaction rwtx = I2CDevice.CreateWriteTransaction(readAddr);
I2CDevice.I2CReadTransaction rtx = I2CDevice.CreateReadTransaction(dist);

I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[2] { rwtx, rtx };
lidar.Execute(xActions, 6000);

Debug.Print(dist[0].ToString("X2") + " - " + dist[1].ToString("X2"));

After posting this, I realized I wasn’t writing the first write transaction, but it still didn’t work. Then I just found some code that suggests I was very much on the right track.

https://www.ghielectronics.com/communit … 243&page=4

So I’m running those code with some extra debug prints and I’m definitely getting to the part where it thinks it has a distance, but I’m getting 0 as the distance.

			//Create an I2C device for the Lidar with address 0x62 (v1 default Lidar address)
			I2CDevice lidarSensor = new I2CDevice(new I2CDevice.Configuration(0x62, 100));

			while(true)
			{
				//Tell Lidar to begin distance read by writing 0x04 to register 0x00 
				if(lidarSensor.Execute(new I2CDevice.I2CTransaction[]{
					I2CDevice.CreateWriteTransaction(new byte[] { 0x00, 0x04}) }, 1000) != 0)
				{
					Thread.Sleep(5); //Do not know if this really have any effects on the sensor.

					//A successful write was completed now begin reading the calculated distance stored in register 0x8f 
					var xTrans = new I2CDevice.I2CTransaction[2];
					var buffer = new byte[2]; //Holds value read from 0x8f;
					xTrans[0] = I2CDevice.CreateWriteTransaction(new byte[] { 0x8f });
					xTrans[1] = I2CDevice.CreateReadTransaction(buffer);

					if(lidarSensor.Execute(xTrans, 1000) != 0)
					{
						//read the distance for the buffer 
						var distnace = (buffer[0] <<  <img src="/img/forum/smilies/cool.gif" title="Cool" alt="Cool"> + buffer[1];
					}
				}
				Thread.Sleep(20); //Sleeps 20msec as lidar sensor defualt read is around 50hz per the datasheet 
			}

I also noticed I can only get it to do the first write successfully if I yank power to the sensor once during the loop and then reconnect it.

I wired up the enable pin to GPIO and issued a low/high with some sleep and that seems to make the sensor respond to that initial acquisition trigger write. I’m still getting 0 for the distance though

Well I tried pull ups and bi-directional level shifters to no avail. I switched to a sparkfun redboard and had it up in minutes.

¯_(ツ)_/¯

I still want to get it to work on my Netduino though, its a much faster board with more capabilities.