DGPS Mindstorms API

Hello from New Zealand

I have looked through the forum and various places online but found nothing. Is there an API/instructions for how to use the navigation aspects of the dGPS with Brickpi3 and in python? I’m building a robot to track over a list of waypoints and would prefer to use the inbuilt methods rather than write my own. Particularly around the:

"DGPS_CMD_DIST   = 0x08      # Fetch distance to destination" and,
"DGPS_CMD_ANGD   = 0x09      # Fetch angle to destination" 

functions of the dGPS. How do you set the destination and then how do you call to find the distance and direction?

(I’m guessing the distance/direction retrieval is BP.transact_i2c(GPS_PORT, DGPS_I2C_ADDR, [DGPS_CMD_DIST], 3) then value = BP.get_sensor(GPS_PORT) but thought I’d ask as well)


Hello @markacereid from the US! It’s been a long time since I’ve looked at the dGPS code, but I’ll give it a shot!

The registry for setting the destinations are here:

DGPS_CMD_SLAT   = 0x0B      # Set latitude of destination 
DGPS_CMD_SLONG = 0x0C       # Set longitude of destination

You would use these to set the destination you’re headed. So you could use the onboard calculator, but you would probably be better served to calculate it in Python on the Raspberry Pi (this would be faster too!). There’s a great stack overflow answer on this here. The fastest (and easiest to troubleshoot) implementation would be to use this code (copy and paste from so):

from math import sin, cos, sqrt, atan2, radians

# approximate radius of earth in km
R = 6373.0

lat1 = radians(52.2296756)
lon1 = radians(21.0122287)
lat2 = radians(52.406374)
lon2 = radians(16.9251681)

dlon = lon2 - lon1
dlat = lat2 - lat1

a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
c = 2 * atan2(sqrt(a), sqrt(1 - a))

distance = R * c

print("Result:", distance)
print("Should be:", 278.546, "km")

Does this help?

1 Like

Hi @JohnC.

Thanks for your reply. Thanks for the directions to use the Raspberry pi to calculate distance. I have used the formulas above it is working well. However, I’m still looking for more in depth instructions for setting values of the destination. Can you please clarify the commands for setting the latitude (for example) as I have put below? (much of it is copied and adjusted from the dGPS example):

BP = brickpi3.BrickPi3() # Create an instance of the BrickPi3 class. BP will be the BrickPi3 object.

GPS_PORT = BP.PORT_1 # GPS will be on sensor port 1


DGPS_CMD_SLAT   = 0x0B      # Set latitude of destination 

But whats next?

BP.DGPS_CMD_SLAT  = 36 (being the latitude)

or does it involve the below?

BP.transact_i2c(GPS_PORT, DGPS_I2C_ADDR, [DGPS_CMD_SLAT ], 4)
BP.get_sensor(GPS_PORT)  = 36

I am still lost on this aspect.

Thanks for your time.