Getting Distance From Raspberry Pi

Jackman,

I used USB to connect to my Pi.

Yes, you run the Python code from the Raspberry Pi.

Here is the code, modified to remove the SQL parts and to fetch the distance from the first anchor

#DWM Serial Parser by Brian H. | Updated 3/6/19
import serial
import time
import datetime


DWM=serial.Serial(port="/dev/ttyACM0", baudrate=115200)
print("Connected to " +DWM.name)
DWM.write("\r\r".encode())
time.sleep(1)
DWM.write("lec\r".encode())
time.sleep(1)
while True:
    try:
        line=DWM.readline()
        if(line):
            if len(line)>=80:
                parse=line.decode().split(",")
                pos_AN0=(parse[parse.index("AN0")+2],parse[parse.index("AN0")+3],parse[parse.index("AN0")+4])
                dist_AN0=parse[parse.index("AN0")+5]
                print(datetime.datetime.now().strftime("%H:%M:%S"),pos_AN0,":",dist_AN0)
            else:
                print("Distance not calculated: ",line.decode())
    except Exception as ex:
        print(ex)
        break
DWM.write("\r".encode())
DWM.close()
1 Like

hello @brianh
I did the same,I powered my raspi,connected a tag to raspi with usb,but no power coming to tag(DWM1001)device,it’s led not blinking no power it it, Am I missing something.
sorry,if I am annoying you, please let me know where I a wrong.

Also If I am connecting DWM1001 with header pins and running the above code I am getting following output:

Traceback (most recent call last):
File “/home/pi/DWM.py”, line 7, in
DWM=serial.Serial(port="/dev/ttyACM0", baudrate=115200)
File “/usr/lib/python2.7/dist-packages/serial/serialutil.py”, line 236, in init
self.open()
File “/usr/lib/python2.7/dist-packages/serial/serialposix.py”, line 268, in open
raise SerialException(msg.errno, “could not open port {}: {}”.format(self._port, msg))
SerialException: [Errno 2] could not open port /dev/ttyACM0: [Errno 2] No such file or directory: ‘/dev/ttyACM0’

I dom’t know where I am wrong
Thanks a lot

Hi jackman,

If the DWM1001 works through USB on your PC but not on the Pi with the same cable the only thing I can think of is a defective USB port on your Pi.

If you are connecting through header, you will have to change line

DWM=serial.Serial(port="/dev/ttyACM0", baudrate=115200)

The GPIO serial port is /dev/ttyAMA0 so change it to

DWM=serial.Serial(port="/dev/serial0", baudrate=115200)

hey,
changing the command help me in connected,
But the output is as in the

picture.

It’s not giving distance.
let me tell you I have only powered the initiator

Looks like you are using Python 2. The problem might be because my code is written in Python 3.

Use the terminal run command python3 DWM.py to see if there is any difference.

hey @brianh
I changed to Python 3,but the output is as same.Does it depend on the version installed on my laptop(python2.7 is installed on my laptop) .
I don’t know what to do,help me.

Can you show me the output when you run the lec command from your PC?

I have 3 anchors powered and a tag connected to PC,with USB,this is the output of lec command.

Screenshot%20(115)

Try changing the serial line to:

DWM=serial.Serial(port="/dev/serial0", baudrate=115200)

I got output like this @brianh,I think we are close
,

Good to see that.

Just change line 17 to:

            if len(line)>=5:

Also, you are missing the “pos_AN0=…” line from my code above. Not sure if you meant to do that but it will cause an error in the print statement later on.

as told by u

see the last line of output
I have powered all 3 anchors (does it matter if we do no fix the anchor at their position set in DRTLS android app)

Try with these changes:

#DWM Serial Parser by Brian H. | Updated 3/6/19
import serial
import time
import datetime


DWM=serial.Serial(port="/dev/serial0", baudrate=115200)
print("Connected to " +DWM.name)
DWM.write("\r\r".encode())
time.sleep(1)
DWM.write("lec\r".encode())
time.sleep(1)
while True:
    try:
        line=DWM.readline()
        if(line):
            parse=line.decode().split(",")
            if parse[0]=="DIST":
                pos_AN0=(parse[parse.index("AN0")+2],parse[parse.index("AN0")+3],parse[parse.index("AN0")+4])
                dist_AN0=parse[parse.index("AN0")+5]
                print(datetime.datetime.now().strftime("%H:%M:%S"),pos_AN0,":",dist_AN0)
            else:
                print("Distance not calculated: ",line.decode())
    except Exception as ex:
        print(ex)
        break
DWM.write("\r".encode())
DWM.close()

@brianh Got this

Looks like it’s working. Are you able to figure out the rest on your own?

@brianh
thanks a lot, I don’t know how to thank you, you are a hero.
One thing more I want to ask you can we modify dist_AN0 with some algoritm(least sq) to get position with that(tel me if I am wrong)?
Also can you share me the document where you have code the above program,I too want to discover more,it is not given on DECAWAVE.

@brianh
Also the method you taught me is different from UART,SPI.
what’s this method called.please share some document related to it
Thanks a lot

@jackman You are welcome.

I can’t comment on the least squares method, I only used the position already calculated by the RTLS. (Why don’t you use this since it’s already available?) I am sure it is possible though, just do more research. Here is an article about trilateration in Python that you can get started with: https://www.alanzucconi.com/2017/03/13/positioning-and-trilateration/

The method I showed you uses the UART (serial) interface.

You can read the pySerial documentation I linked above for more info: https://pyserial.readthedocs.io/en/latest/shortintro.html

There are lots of Python tutorials available online, here are a few:
https://docs.python.org/3/tutorial/
https://www.w3schools.com/python/

1 Like

Thanks a lot.You made my work possible.

@jackman That is a fairly involved project and I don’t think this forum is the appropriate place to ask for help with that. I would recommend checking out the Raspberry Pi Forums.