Robot Control using Raspberry Pi

Lecture 7 - Understand Board support package (BSP) and Gemini

Li Bai

Temple University

September 24, 2024

What is a Raspberry Pi?

Rapberry Pi Family

  • Rapsberry Pi Pico ($4)

  • No OS

  • raspberry pi 4 computing module

  • $25 -$200

  • raspberry 4B
  • $35 - $200

Mangdang Minpuppers

  • raspberry pi 4

  • motors are older, ROS1 and ROS2

  • raspberry 4 computing module
  • better motors, ROS2 support mainly

Software needed (BSP)…

Audio Demo Test

# Audio record parameters
fs = 48000  # 48KHz,Audio sampling rate
duration = 5  # Recording duration in seconds

# Set the default speaker volume to maximum
# Headphone number is 0 without HDMI output
# Headphone number is 1 when HDMI connect the display
os.system("amixer -c 0 sset 'Headphone' 100%")
  • audio sample rate
  • using amixer if you do not have it in the system
sudo apt install alsa-utils -y
  • change duration

  • make the program reading from command arguments

  • check how to use args

  • check microphone for plughw
arecord -l
  • record a wav file using plughw:1,0 or plughw:0,0
arecord -D plughw:1,0 -d 5 -f S16_LE -t wav -r 16000 -c 2 output.wav
  • with a specified file in .asoundrc with plughw
arecord  -d 5 -f S16_LE -t wav -r 16000 -c 2 output.wav
aplay -l
  • record a wav file using plughw:1,0 or plughw:0,0
arecord -D plughw:1,0 -d 5 -f S16_LE -t wav -r 16000 -c 2 output.wav
  • with a specified file in .asoundrc with plughw
arecord  -d 5 -f S16_LE -t wav -r 16000 -c 2 output.wav
pcm.!default {
    type asym
    playback.pcm {
        type plug
        slave.pcm "hw:1,0"  # Adjust based on your output device
    }
    capture.pcm {
        type plug
        slave.pcm "hw:0,0"  # Adjust based on your input device
    }
}

IMU

#!/usr/bin/python
from MangDang.mini_pupper.ESP32Interface import ESP32Interface
import time

esp32 = ESP32Interface()

while True:
    print(esp32.imu_get_data())
    time.sleep(1 / 20)  # 20 Hz
  • run the code as
python python_api_getimu.py

Touch Sensor

import time
import RPi.GPIO as GPIO

# There are 4 areas for touch actions
# Each GPIO to each touch area
touchPin_Front = 6
touchPin_Left  = 3
touchPin_Right = 16
touchPin_Back  = 2
...

# Detection Loop
while True:
    touchValue_Front = GPIO.input(touchPin_Front)
    touchValue_Back  = GPIO.input(touchPin_Back)
    touchValue_Left  = GPIO.input(touchPin_Left)
    touchValue_Right = GPIO.input(touchPin_Right)
    display_sting = ''
    if not touchValue_Front:
        display_sting += ' Front'
    if not touchValue_Back:
        display_sting += ' Back'

...
  • run the code as
python touch_test.py

LCD Test

#!/usr/bin/python
from MangDang.mini_pupper.display import Display, BehaviorState
import time

disp = Display()

disp.show_image('/var/lib/mini_pupper_bsp/test.png')
time.sleep(5)
disp.show_state(BehaviorState.REST)
time.sleep(5)
disp.show_state(BehaviorState.TROT)
time.sleep(5)
disp.show_state(BehaviorState.LOWBATTERY)
time.sleep(5)
disp.show_ip()
  • run this code using
python python_api_display.py

Gemini

  • Multimodel LLM

Terminologies used

  • tokens, prompt and role

Token Cost

as the price shown on 9/27/2024

Google Gemini Basic Process for credits

  • Go to Canvas, request your Google credit using your temple.edu email address
  • Apply the credit using your gmail account using the redeem link provided in your email
  • Link the credit to a project
  • Setup a service account with the project
  • Enable google gemini APIs for the project

Google Gemini

  • Go to Canvas, request your Google credit using your temple.edu email address

  • save the coupon code
  • copy redeem link
  • open a new chrome tab and login your own email
  • create a new project using the redeem link

  • use the redeem link in a new tab
  • select your project and copy the coupon code

setup API

Tip

  • Vertex AI API
  • Cloud Speech-to-Text API
  • Cloud Text-to-Speech API

check credits

Save the credential

  • create a hidden folder
mkdir ~/.gemini
mv creds.json ~/.gemini

Check Gemini Vision

  • upload your gemini service credential json file

  • clone the mangdang folder as follows

  • git clone http://github.com/lbaitemple/mangdang
    cd mangdang/gemini
  • open food.ipynb

  • place a food item in the front of the camera and analyze

  • modify the prompt

Setup the soundrc file

  • setup soundrc file
cd ~/mangdang
bash ./create_asound_conf.sh 

Google Gemini Demo

  • Git clone the gemini code
cd ~
git clone https://github.com/mangdangroboticsclub/apps-md-robots
cd apps-md-robots/ai-app/
sudo apt-get install -y python3-pyaudio
sudo pip install -r requirements.txt
  • replace credential in env.sample
API_KEY_PATH=<Your own api key PATH>

as

API_KEY_PATH=/home/ubuntu/.gemini/creds.json

Set up Environment variable

cd ..
cp env.sample .env
  • run code
cd ~/apps-md-robots
python  ai-app/ai_app.py 
  • other environment variable and save it to .bashrc
export GOOGLE_APPLICATION_CREDENTIALS=/home/ubuntu/.gemini/creds.json

Cloud Text to Speech Language Support

Run Services

sudo systemctl status ai.service
sudo systemctl status joy.service
sudo systemctl status robot.service

Save MicroSD Card to an Image

You can save your MicroSD card to an image file

  • using terminal windows to find disk numer
diskutil list
  • say if you have microSD read as /dev/disk4
sudo dd if=/dev/disk4 of=/Users/name/imagefile.img

PiShrink

  • you can save the file smalelr usign pishrink
 pishrink imagefile.img [newimagefile.img]
 pishrink imagefile.img [newimagefile.img]