《从零开始配置树莓派控制机械臂》专栏传送门

【分享】从零开始在树莓派4B上搭建环境,使用ros控制dofbot机械臂(一)

【分享】从零开始在树莓派4B上搭建环境,使用ros控制dofbot机械臂(二)

【分享】从零开始在树莓派4B上搭建环境,使用ros控制dofbot机械臂(三)

【分享】从零开始在树莓派4B上搭建环境,使用ros控制dofbot机械臂(四)

【分享】从零开始在树莓派4B上搭建环境,使用ros控制dofbot机械臂(五)

【分享】从零开始在树莓派4B上搭建环境,使用ros控制dofbot机械臂(六)

1. 安装OLED驱动,显示系统信息

oled是外接在树莓派上的一块显示屏,可以用来展示或输出信息。

输入命令安装oled驱动

sudo pip3 install Adafruit-SSD1306
sudo apt-get install python3-rpi.gpio

新建jupyter文件,运行下面程序。

#!/usr/bin/env   python3
#coding=utf-8
import   time
import   os

import   Adafruit_SSD1306 

from   PIL import Image
from   PIL import ImageDraw
from   PIL import ImageFont

import   subprocess

#   Raspberry Pi pin configuration:
RST   = None     # on the PiOLED this pin   isnt used

#   128x32 display with hardware I2C:
disp   = Adafruit_SSD1306.SSD1306_128_32(rst=RST, i2c_bus=1, gpio=1)

#   Initialize library.
disp.begin()

#   Clear display.
disp.clear()
disp.display()

#   Create blank image for drawing.
#   Make sure to create image with mode '1' for 1-bit color.
width   = disp.width
height   = disp.height
image   = Image.new('1', (width, height))

#   Get drawing object to draw on image.
draw   = ImageDraw.Draw(image)

#   Draw a black filled box to clear the image.
draw.rectangle((0,0,width,height),   outline=0, fill=0)

#   Draw some shapes.
#   First define some constants to allow easy resizing of shapes.
padding   = -2
top   = padding
bottom   = height-padding
#   Move left to right keeping track of the current x position for drawing   shapes.
x   = 0

#   Load default font.
font   = ImageFont.load_default()

#定义读取CPU占用率的函数
def   getCPULoadRate():
    f1 = os.popen("cat /proc/stat",   'r')
    stat1 = f1.readline()
    count = 10
    data_1 = []
    for i in range (count):
        data_1.append(int(stat1.split()[i+1]))
    total_1 =   data_1[0]+data_1[1]+data_1[2]+data_1[3]+data_1[4]+data_1[5]+data_1[6]+data_1[7]+data_1[8]+data_1[9]
    idle_1 = data_1[3]

    time.sleep(1)

    f2 = os.popen("cat /proc/stat",   'r')
    stat2 = f2.readline()
    data_2 = []
    for i    in range (count):
        data_2.append(int(stat2.split(' ')[i+2]))
    total_2 =   data_2[0]+data_2[1]+data_2[2]+data_2[3]+data_2[4]+data_2[5]+data_2[6]+data_2[7]+data_2[8]+data_2[9]
    idle_2 = data_2[3]

    total = int(total_2-total_1)
    idle = int(idle_2-idle_1)
    usage = int(total-idle)
#       print("idle:"+str(idle)+"  total:"+str(total))
    usageRate = int(float(usage  / total) * 100)
    str_CPU =   "CPU:"+str(usageRate)+"%"
    # print(str_CPU)
    return str_CPU
#读取系统时间
def   getSystemTime():
    cmd = "date +%H:%M:%S"
    date_time = subprocess.check_output(cmd,   shell = True )
    str_Time = str(date_time).lstrip('b\'')
    str_Time = str_Time.rstrip('\\n\'')
    # print(date_time)
    return str_Time

#读取空闲的内存 / 总内存
def   getFreeRAM():
    cmd = """free -h | awk 'NR==2{printf   "RAM: %.1f/%.1fGB ", $7,$2}'"""
    FreeRam = subprocess.check_output(cmd,   shell = True )
    str_FreeRam = str(FreeRam).lstrip('b\'')
    str_FreeRam = str_FreeRam.rstrip('\'')
    return str_FreeRam

#读取空闲的TF卡空间 / TF卡总空间
def   getFreeDisk():
    cmd = """df -h | awk   '$NF=="/"{printf "Disk:%.1f/%.1fGB", $4,$2}'"""
    Disk = subprocess.check_output(cmd, shell   = True )
    str_Disk = str(Disk).lstrip('b\'')
    str_Disk = str_Disk.rstrip('\'')
    return str_Disk

#读取当前IP地址
def   getLocalIP():
    cmd = "hostname -I | cut -d' '   -f1"
    IP = subprocess.check_output(cmd, shell =   True )
    str_IP = str(IP).lstrip('b\'')
    str_IP = str_IP.rstrip('n\'')
    # print(str_IP)
    return str_IP

import matplotlib.pyplot as plt 
from IPython import display

#   Load font.
font   = ImageFont.load_default()
# font = ImageFont.truetype("./Fonts/simsun.ttc", 14)

def   main():
    while True:
        # Draw a black filled box to clear   the image.
        draw.rectangle((0,0,width,height),   outline=0, fill=0)

        #读取系统信息
        str_CPU = getCPULoadRate()
        str_Time = getSystemTime()
        str_FreeRAM = getFreeRAM()
        str_Disk = getFreeDisk()
        str_IP = getLocalIP() 

        #OLED加载显示缓存信息
        # draw.text((x+2, top+2), '我的机械臂',   font=font, fill=255)
        # draw.text((x+2, top+19), 'time:' + str_Time,   font=font, fill=255)
        draw.text((x, top), str_CPU,   font=font, fill=255)
        draw.text((x+50, top), str_Time,   font=font, fill=255)
        draw.text((x, top+8),   str_FreeRAM,  font=font, fill=255)
        draw.text((x, top+16), str_Disk,  font=font, fill=255)
        draw.text((x, top+24),   "ip:" + str_IP,  font=font,   fill=255) 

        # Display image.
        plt.imshow(image)
        display.clear_output(wait=True)
        plt.show()
        disp.image(image)
        disp.display()
        # time.sleep(.5)

try   :
    main()
except   KeyboardInterrupt:
    print(" Program closed! ")
    pass

可以看到我们将系统的参数在oled上实时显示出来。

如果需要展示中文信息,则需要添加中文字体库,网上没找到简单的安装方法,我就在windows电脑上的C:\Windows\Fonts文件夹拷贝到程序子目录下,并导入字体实现的。