Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 02:29 21 Nov 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Need help with picoMite & LIDAR

Author Message
Amnesie
Guru

Joined: 30/06/2020
Location: Germany
Posts: 720
Posted: 02:09pm 18 May 2022
Copy link to clipboard 
Print this post

Hello again,

I am struggeling with interfacing a LIDAR (TF Luna) I just bought. This sensor calculates the "time of flight" and is way more preceise than the ultrasonic thingy.

The problem is I am unable to do what I want in BASIC and the manual does not help in Appendix A to interface the LIDAR sensor via Serial COM. I just want to read Serial data comming in and decode it. Normally it should be pretty easy but for example I don't know how to count the number of incomming bytes and reset the buffer etc.

Here is a python script that does all the work, but I am not really able to translate this into MMBASIC:


ser = serial.Serial("/dev/serial0", 115200,timeout=0) # mini UART serial device
#
############################
# read ToF data from TF-Luna
############################
#
def read_tfluna_data():
   while True:
       counter = ser.in_waiting # count the number of bytes of the serial port
       if counter > 8:
           bytes_serial = ser.read(9) # read 9 bytes
           ser.reset_input_buffer() # reset buffer

           if bytes_serial[0] == 0x59 and bytes_serial[1] == 0x59: # check first two bytes
               distance = bytes_serial[2] + bytes_serial[3]*256 # distance in next two bytes
               strength = bytes_serial[4] + bytes_serial[5]*256 # signal strength in next two bytes
               temperature = bytes_serial[6] + bytes_serial[7]*256 # temp in next two bytes
               temperature = (temperature/8.0) - 256.0 # temp scaling and offset
               return distance/100.0,strength,temperature

if ser.isOpen() == False:
   ser.open() # open serial port if not open

distance,strength,temperature = read_tfluna_data() # read values
print('Distance: {0:2.2f} m, Strength: {1:2.0f} / 65535 (16-bit), Chip Temperature: {2:2.1f} C'.\
             format(distance,strength,temperature)) # print sample data
ser.close() # close serial port


Description what this does: In the above script, the serial port is being accessed for serial0 at a baudrate of 115200. The serial port is first opened before reading or writing any commands [ser.Open()]. Then in the test script, 9-bytes are read and the first two bytes are checked for the correct data format (0x59 and 0x59 are cited as the data return in the product manual). Finally, the data is printed out as distance, signal strength, and chip temperature.

And this is how far I got:


CLS
Option Explicit
MODE 2

SETPIN GP5, GP4, COM2 ' assign the I/O pins for the first serial port
OPEN "COM2:115200" AS #5 ' open the 2nd serial port w/ speed of 115200 bd


Do
 dat$ = INPUT$(9, #5) ' get up to 9 characters from the serial port
 PRINT dat$
Loop


I am getting a lot of Characters, so I am receiving, ok this is obvious, because I must translate the string into byte but ... Hm...  

Maybe anyone could help me a bit with this, since it should not be that complicated... only for me ... I am NOT expecting someone to translate all this python script into MMBASIC, but maybe one could help me to just get this counting of 9 bytes correct and display it, then resetting it. The decoding work (hopefully) I can do myself :)

Greetings
Daniel
Edited 2022-05-19 00:22 by Amnesie
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4147
Posted: 02:30pm 18 May 2022
Copy link to clipboard 
Print this post

The terminology "reset the ..." is bizarre. It seems to mean remove the data from the buffer, which is what happens by default.

To get exactly 9 bytes (characters, if you prefer the term) something like
dat$=""
do while len(dat$) < 9
dat$ = dat$ + input$(1, #5)
loop

You could check along the lines of
if left$(dat$, 2) = chr$(&h59)+chr$(&h59) then
...

John [sorry about the hurried post]
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3481
Posted: 02:32pm 18 May 2022
Copy link to clipboard 
Print this post

If you have the baud rate set right, the LOC function will tell you how many bytes are in the serial input buffer. You can pick them up in chunks with the INPUT$ function, or if the input is terminated with CR/LF, you can pick them up line by line with LINE INPUT.

How about providing a sample of what you are receiving?

Serial input is something which MMBasic makes very easy--all the buffering is done for you and you can check the status of the buffer at any time--at your convenience.

~
Edited 2022-05-19 00:36 by lizby
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
Frank N. Furter
Guru

Joined: 28/05/2012
Location: Germany
Posts: 1001
Posted: 07:23am 19 May 2022
Copy link to clipboard 
Print this post

Hi Daniel,

I have read a barcode scanner with this code snippet:
 Open "COM1:" As #1
 Zaehler=0
 Do
   If Loc(#1)>=15 Then 'Puffer auslesen wenn 15 Zeichen im Puffer
     dat$ = Input$(15,#1) 'Lese 15 Zeichen aus Puffer
     dat$ = Mid$(dat$,2,14)
     Print dat$        'gelesenen Barcode zu Terminal senden
     LCD 2,1, "                "
     LCD 2,1,datalt$   'Zeige letzten Barcode
     LCD 1,1, "                "
     LCD 1,1,dat$      'Zeige akt. Barcode
     datalt$ = dat$
     Exit Do
   Else
     Zaehler = Zaehler + 1 'Schleifenzaehler erhoehen
     Pause 1
   End If
 Loop Until Zaehler >= 1500 'Timerueberlauf oder Barcode gelesen
 Close #1  'Puffer loeschen"
 Pause 50
 Open "COM1:" As #1

I had problems with residues in the buffer - hence the closing and reopening of the interface...

Frank
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025