0

I am building a small Jetson Nano robot and have done everything myself so far, from the battery pack up to spare parts for 12 V reduced engines, a Lidar, a night vision camera and an ADC to measure battery power. The script below I wrote to have a small App pop up in the desktop corner and show the actual battery, I have attached a small monitor to the robots back.

If I call the script with sudo, it works as intended, even can fork it as a process. Yet if I try to autostart it any other way, it does not work. Then I get the error:

"NameError: name 'I2C' is not defined"

The issue is related to the Python libs board, busio and adafruit accompanying the script. There is an issue about it on their GitHub and the solution is to either downgrade busio or upgrade the whole lib package to a newer version. In fact on the Jetson Nano I am running version 6.15, tho the actual version is 8 something. Problem is that pip doesn't allow greater than v6.15, I'd need to upgrade Python then. I don't want to do this on the Nano, since it's working perfectly with the current setup in image segmentation and therefore pathfinding. Downgrading busio I tried as far as I could go, had no effect though.

So I am basically trieing to find out a permission/recognition thing. If I execute the script with sudo as a user it runs. If I do the following, I get the error:

  • run the script as root
  • run it with pkexec
  • run it as a systemctl script

So it seems to always happen as root, what I haven't tried yet is a crontab, though prolly as root the same will happen.

Do you maybe have an idea, what else I could do? The only thing I basically want to achieve is that the script autostarts on a reboot.

#!/usr/bin/python3

import time
import board
import busio
import adafruit_ads1x15.ads1015 as ADS
from adafruit_ads1x15.analog_in import AnalogIn

from tkinter import Tk,Label

# Create the I2C bus
i2c = busio.I2C(board.SCL, board.SDA)

# Create the ADC object using the I2C bus
ads = ADS.ADS1015(i2c)

# Create single-ended input on channel 0
chan = AnalogIn(ads, ADS.P0)

# factor voltage divider
v_divider = 5.54546

# draw GUI

root = Tk()
root.title('Robot_No6_BatMon')

w = 150 # width for the Tk root
h = 50 # height for the Tk root

# get screen width and height
ws = root.winfo_screenwidth() # width of the screen
hs = root.winfo_screenheight() # height of the screen

# calculate x and y coordinates for the Tk root window
x = ws - (w)
y = 0

# set the dimensions of the screen 
# and where it is placed
root.geometry('%dx%d+%d+%d' % (w, h, x, y))

Label(root, text="Battery Percentage Left:", font=("Arial", 25)).pack()

def update_text():
    # Calculate bat stats
    bat_percent = chan.voltage - 1.81
    bat_percent = bat_percent * 100
    curr_voltage = v_divider * chan.voltage
    curr_wattage = curr_voltage * 3.1

    if (bat_percent > 100):
        l.config(text="100 % / 16.8 Volt\n52 Wh")
    else:
        l.config(text="{:.0f} % / {:.1f} Volt\n{:.0f} Wh (52 Wh Design)".format(bat_percent, curr_voltage, curr_wattage))
    root.after(10000, update_text)

l = Label(root, text="", font=("Arial", 25))
l.pack()

update_text()
root.mainloop()

Bat GUI

Robot

0andriy
  • 4,183
  • 1
  • 24
  • 37
ntropy
  • 1
  • 1

0 Answers0