Aula Jazmati
Published © MIT

Interfacing Hexabitz Digital Compass With Raspberry Pi 🧭

Build your own Hexabitz compass with Raspberry Pi.

IntermediateFull instructions provided2 hours200

Things used in this project

Story

Read more

Schematics

H0BR40 IMU FactSheet

Code

RPi Tkinter GUI Test code

Python
# -*- coding: utf-8 -*-
"""
Created on Fri Mar  8 14:43:52 2024

@author: AULA
"""

import tkinter as tk
from math import radians, cos, sin


def draw_compass(canvas):
    
    canvas.create_oval(10, 10, 200, 200, outline="black", fill="light blue", width=3)
    
    arrow = canvas.create_line(100, 100, 100, 50, arrow=tk.LAST, fill="red", width=3)
    return arrow

def add_labels(canvas):
    labels = ["N", "S", "E", "W"]
    positions = [(100, 20), (100, 190), (190, 100), (20, 100)]
    for label, position in zip(labels, positions):
        canvas.create_text(position, text=label, font=("Arial", 14))


def update_arrow(canvas, arrow, bearing):
    
    angle = radians(bearing)
    x_end = 100 + 60 * sin(angle)
    y_end = 100 - 60 * cos(angle)
    canvas.coords(arrow, 100, 100, x_end, y_end)


root = tk.Tk()
root.title("Hexabitz Compass")
ramadan_label = tk.Label(root, text="Ramadan Kareem", font=("Arial", 24), bg="gold", fg="black")
ramadan_label.pack(pady=20)
compass_canvas = tk.Canvas(root, width=210, height=210, bg="white")
compass_canvas.pack()

arrow = draw_compass(compass_canvas)

add_labels(compass_canvas)

update_arrow(compass_canvas, arrow, bearing=0)  

root.mainloop()

The Final Code (Interfacing Hexabitz Digital Compass With Raspberry Pi GUI Code)

Python
#Aula-Jazmati
import time
import serial
import struct
import math
from turtle import *
ser = serial.Serial(        
               port='/dev/ttyS0',
               baudrate = 921600,
               parity=serial.PARITY_NONE,
               stopbits=serial.STOPBITS_ONE,
               bytesize=serial.EIGHTBITS,
               timeout=0
           )
print(ser.name)
value = (0,0,0)
m=[0, 0, 0]
value1 = 0
value2 = 0
value3 = 0
def calculate_heading(mag_x, mag_y):
    heading_rad = math.atan2(mag_y, mag_x)
    heading_deg = math.degrees(heading_rad)
   # if heading_deg < 0:
       # heading_deg += 360
    return heading_deg
def Compass():
    hideturtle()
    tracer(0, 0) # switch off animation to build up compass fast!
    global compasspointer
    penup()
    right(90)
    forward(250)
    left(90)
    pendown()
    circle(250)
    penup()
    home()
    goto(260, 0)
    write("East", font=('Arial', 30, 'normal'), align="left")
    home()
    goto(0, 260)
    write("North", font = ('Arial', 30, 'normal'), align="center")
    home()
    goto(0, -290)
    write("South", font = ('Arial', 30, 'normal'), align="center")
    home()
    goto(-260, 0)
    write("West", font = ('Arial', 30, 'normal'), align="right")
    home()
    goto(0, -65)
    write("*Ramadan Kareem*", font = ('Arial', 22, 'bold'), align="center")
    home()
    bgcolor("light blue")
    compasspointer = Turtle()
    compasspointer.degrees(360) 
    compasspointer.home()
    compasspointer.settiltangle(0)
    compasspointer.shape("triangle")
    compasspointer.color("red")
    compasspointer.turtlesize(2, 15, 0)
    tracer(1, 1) # switch on animation, because compasspointer should turn
    compasspointer.speed(0)
angle = 0
Compass()
while 1:
    x=ser.read(12)
    if x and len(x) == 12:
        x = x[::-1]
        value = struct.unpack('fff', x)
        value1, value2, value3 = value
        #print(value)
        #time.sleep(1)
    if value  :
        m[0]= int(value1)# Store the z-axis value
        m[1]= int(value2)# Store the y-axis value
        m[2]= int(value3)# Store the x-axis value
    angle = calculate_heading(m[2], m[1])
    compasspointer.settiltangle(-angle +90)                                  
    print (angle)

H0BR40 IMU Code

C/C++
/*
 BitzOS (BOS) V0.3.0 - Copyright (C) 2017-2024 Hexabitz
 All rights reserved

 File Name     : main.c
 Description   : Main program body.
 */
/* Includes ------------------------------------------------------------------*/
#include "BOS.h"
//float x, y, z;
//uint8_t MESG_x[5];
//uint8_t MESG_y[5];
//uint8_t MESG_z[5];
/* Private variables ---------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/

/* Main function ------------------------------------------------------------*/

int main(void){

	Module_Init();		//Initialize Module &  BitzOS

	//Don't place your code here.
	for(;;){}
}

/*--------------------------------------------------------------------------*/

/* User Task */
void UserTask(void *argument){

	// put your code here, to run repeatedly.
	while(1){
		uint8_t port = 3;
		uint8_t module = 0;
		SampleMagMGaussToPort (port, module);
		Delay_s(1);
	}
}

/*--------------------------------------------------------------------------*/

RPi turtle GUI Test code

Python
from turtle import *
import time
###############################################################
#       Functions for the turtle graphics                    #
###############################################################

def Compass():
    hideturtle()
    tracer(0, 0) # switch off animation to build up compass fast!
    global compasspointer
    penup()
    right(90)
    forward(250)
    left(90)
    pendown()
    circle(250)
    penup()
    home()
    goto(260, 0)
    write("East", font=('Arial', 30, 'normal'), align="left")
    home()
    goto(0, 260)
    write("North", font = ('Arial', 30, 'normal'), align="center")
    home()
    goto(0, -290)
    write("South", font = ('Arial', 30, 'normal'), align="center")
    home()
    goto(-260, 0)
    write("West", font = ('Arial', 30, 'normal'), align="right")
    home()
    goto(0, -65)
    write("*Ramadan Kareem*", font = ('Arial', 22, 'bold'), align="center")
    home()
    bgcolor("light blue")
    compasspointer = Turtle()
    compasspointer.degrees(360) #make sure to use angle in degree, a circle has 360 degrees
    compasspointer.home()
    compasspointer.settiltangle(0)
    compasspointer.shape("triangle")
    compasspointer.color("red")
    compasspointer.turtlesize(2, 15, 0)
    tracer(1, 1) # switch on animation, because compasspointer should turn
    compasspointer.speed(0)

###############################################################
#          Main programm Code                                 #
###############################################################

angle = 0

Compass()
while (True):
    
    try:
        angle = 0
        
        compasspointer.settiltangle(-angle+90) #turtles' zero position is not North, its East. But 0 degree equals N. (+90)
         
        angle = 90 
        compasspointer.settiltangle(-angle+90)                                       #Turtles' angle go counterclockwise, but we need it clockwise (-angle)
        print (angle)
        
    
    except:
        pass

Interfacing Hexabitz Digital Compass With Raspberry Pi Code

Python
import time
import serial
import struct
import math
ser = serial.Serial(        
               port='/dev/ttyS0',
               baudrate = 921600,
               parity=serial.PARITY_NONE,
               stopbits=serial.STOPBITS_ONE,
               bytesize=serial.EIGHTBITS,
               timeout=0
           )
print(ser.name)
value = (0,0,0)
m=[0, 0, 0]
value1 = 1
value2 = -1
value3 = 1
def calculate_heading(mag_x, mag_y):
    heading_rad = math.atan2(mag_y, mag_x)
    heading_deg = math.degrees(heading_rad)
    if heading_deg < 0:
        heading_deg += 360
    return heading_deg
while 1:
    x=ser.read(12)
    if x and len(x) == 12:
        x = x[::-1]
        value = struct.unpack('fff', x)
        value1, value2, value3 = value
        #print(value)
        #time.sleep(1)
    if value  :
        m[0]= int(value1)# Store the z-axis value
        m[1]= int(value2)# Store the y-axis value
        m[2]= int(value3)# Store the x-axis value
    print(calculate_heading(m[2], m[1])) 

H0BR4x-Firmware

Credits

Aula Jazmati

Aula Jazmati

49 projects • 193 followers
(PhD) in Electronic Engineering 2023 💡🕊️

Comments