Arduino Forum

Using Arduino => Interfacing w/ Software on the Computer => Topic started by: vova1989 on Mar 26, 2019, 01:17 pm

Title: Serial communication Python to arduino
Post by: vova1989 on Mar 26, 2019, 01:17 pm
Hello,
I have a python code that i get information that i need from rplidar a1m8.
the data i need is angle and distance.
I need a help to get this data on arduino and use it.

Python code:

import sys
import time
from rplidar import RPLidar
import serial
import struct

arduino = serial.Serial('com6', 115200)

angleoffset = 0

PORT_NAME = 'COM5'  # this is for the Lidar


def run():
    '''Main function'''
    lidar = RPLidar(PORT_NAME)
    lidar.start_motor()
    time.sleep(1)
    info = lidar.get_info()
    print(info)
    try:
        print('Recording measurments... Press Crl+C to stop.')
        try:
            for measurment in lidar.iter_measurments():

                if (measurment[2] > 0 and measurment[2] < 360):
                    if (measurment[3] < 2000 and measurment[3] > 100):
                        angle = measurment[2]
                        distance = measurment[3]
                       

                        print("Angle: ", angle - angleoffset)
                        print("Distance: ", distance)



        except KeyboardInterrupt:
            print('Stopping.')
    except KeyboardInterrupt:
        print('Stopping.')
    lidar.stop()
    lidar.stop_motor()

    lidar.disconnect()


if __name__ == '__main__':
    run()
Title: Re: Serial communication Python to arduino
Post by: Robin2 on Mar 26, 2019, 10:16 pm
This simple Python - Arduino demo (http://forum.arduino.cc/index.php?topic=598209.) should help get you started.

See also Serial Input Basics (http://forum.arduino.cc/index.php?topic=396450.0) - simple reliable ways to receive data.

...R