Thursday, September 26, 2024

Assembling a Robotic arm

Research

Now that we have a robotic car, I'm considering attaching more devices to it. What about attaching a robotic arm? The tutorial for the car has a section about controlling the robotic arm but unfortunately, my car didn't have one.
I started researching about robotic arms and learning what kind of servos they need. I found that any arm needs at least 4 servos to operate, I looked through dozens of arm models to find the best price/quality model. There are tons of cheap plastic arms but I wanted something more durable and also not expensive. I found one model named 6DOF Robot Mechanical Arm Clamp Claw Kit DOF Manipulator Industrial Robot Parts. It looks a little bit big for my car but I was willing to buy and assemble it. The learning experience is worth 37 bucks spent on the robotic arm :). Oh...the product description didn't say anything about instructions so I probably won't expect any assembling instructions. I found 6DOF Robotic Arm Assembly Tutorial which explains how to assemble the arm. Now I'm confident that my 37$ will not be wasted :)

Assembling

I started assembling the arm following the video tutorial and it went smoothly. The video tutorial is clear and simple to follow. Just until the first servo is installed. The problem is that we need to rotate this servo to 90 degrees.

I could turn it manually as suggested in another tutorial here but that doesn't look very precise. Why don't attach the servo to the car and turn it?

Turn the servo to 90 degrees

Connect the servo to Port 1. See port numbers in the Motor HAT doc 
Turn on the car, connect in VSCode and run this python program:

import Adafruit_PCA9685 # Import the library used to communicate with PCA9685

pwm = Adafruit_PCA9685.PCA9685() # Instantiate the object used to control the PWM
pwm.set_pwm_freq(50) # Set the frequency of the PWM signal

pwm.set_pwm(1, 0, int((540 + 72) / 2)) # range is between 72 and 539



What is PWM?

PWM - Pulse Width Modulation. There are plenty of videos about it. For example here.
Our servo MG996R has duty cycles 1-2ms where 1ms is 0 degrees, 2ms is 180 degrees.
To control the servo you need to send a "magic" number to the pwm object. Unfortunately, Adafruit_PCA9685 is an old and deprecated library which doesn't provide to set degrees 0-180. I played with the servo and found that the most left position is at 72 and the most right is at 539. Which probably corresponds to 0 and 180. Here is the article that explains what is a duty cycles and how to convert them to degrees.
Here is whole process in a few secs:


Connecting robotic arm to the car

 I connected all 6 servos to the car:


Then connected from VSCode and played with the servos. Unfortunately, the shoulder and elbow servos couldn't hold the weight of the arm :(. I'll deal with it later. Here is a script to set arm into default position (like on the picture above):

import Adafruit_PCA9685 # Import the library used to communicate with PCA9685

pwm = Adafruit_PCA9685.PCA9685() # Instantiate the object used to control the PWM
pwm.set_pwm_freq(50) # Set the frequency of the PWM signal

#servo 2 - shoulder in almost open position (horizontal back) at 500
pwm.set_pwm(2, 0, 400) # ~45 degrees turned back
pwm.set_pwm(3, 0, 350) # ~60 degrees to shoulder
pwm.set_pwm(4, 0, 150) # ~100 degrees to elbow
pwm.set_pwm(5, 0, 300) # ~palm parallel to ground
pwm.set_pwm(6, 0, 250) # ~palm wide open

Resources

RC Servo PWM and Frequency Demonstration


Raspberry Pi: Install development environment

I'm used to nice and powerful IDEs. I looked on Thonny IDE and decided to find a better approach to develop on the Raspberry Pi.  I found this video tutorial that helped me to setup VSCode to develop remotely on my car: How to set up remote development for the Raspberry Pi using Visual Studio Code

Setup IDE (VSCode)


Steps are simple
- Install VSCode
- Install Remote Development extension pack to VSCode
- Create a new SSH connection and connect to Raspberry Pi from VSCode
It will take some time for VSCode to install the server on the Pi machine. 

Clone Github repository

Click clone repository...
Provide github URL. I used https version of the repo I forked: https://github.com/szelenin/Adeept_AWR.git
Provide existing folder where to clone
 


Configure Git

In the terminal set user name and email for git:

git config --global user.name user name
git config --global user.email your@email.com

Commit and Push

Change smth and commit from the source control: 

Click Sync Changes button to push.

Run Python with sudo

add "sudo": true into launch.json


Intellij Keybinding

Install IntelliJ IDEA Keybindings plugin

Make Raspberry use whole SD card's space

I used the provided image for the car so maybe because of that my Raspberry didn't expand the file system on the first boot. I noticed this when I tried to install Python Extension. 
To fix this open the terminal and run raspi-config:

sudo raspi-config

Then select Expand Filesystem in the Advanced Options:

After the car restarts, connect again and see how much space on the device. From the terminal run df -H command.
 

Sunday, September 22, 2024

Raspberry Pi: first encounter

It's been almost 13 years since my last post here. Quite a lot of things happened this time...

I got this Adeept AWR 4WD WiFi Smart Robot Car Kit for Raspberry Pi. It is built on Raspberry Pi and requires assembling. There are no instructions provided and after I opened the box I felt like I was going to overcome many challenges. 

Assembling and first start

I found the tutorials and samples (here) and they looked pretty good. After I installed an operation system, car server, and all dependencies I tried to start a car's webserver. The server returned an error: 'Could not determine default I2C'. I googled it and found that the webserver uses a deprecated library: Adafruit_Python_GIRO. The recommendation was to use a new library called Blinka

I installed Blinka but it didn't help to start the server. I looked in the server's code and tried to hack it so it works with the new library. No success here as well - the server requires many changes to migrate to the new library. So disappointing...

I was desperate and about to scrap everything when I suddenly realized the manufacturer had included the image file with Raspberry Pi OS and the webserver (here is the link). The tutorial described this option as a "not recommended" way to install the system. So I followed that "not recommended" approach and everything worked smoothly! My servo motor reacted and I started assembling the car following the instructions. 

Useful tip: Start from this step

Install cooling for Raspberry Pi! See Useful Tip 2 below. Then proceed with the next steps. 
 
Follow "Not recommended approach" in section 2.1.3 of the tutorial if you want your car to work :)

I configured my home WiFi in the Imager settings. I enabled ssh and configured the user name and password. Although the tutorial says you can skip it for "not recommended approach", I did it because I wasn't able to find credentials for pre-configured ssh access.

After that follow the assembling instructions from Section 4. You do not need to install anything as everything is already preinstalled in the provided image file.

Here is what I got (Front)

And back


I turned on my car, logged into it by ssh and started the server: 
sudo python3 Adeept_AWR/server/webServer.py


I ignored all errors and warnings and pointed my browser to the server : http://<server>.local:5000. I was able to control the car, camera works fine and able to move up and down.  

Here is a control screen


Useful tip 2:

The Raspberry Pi quickly overheats. Especially if there are several devices on it like servos, motors etc.  In my case after I started the car's temperature went above 65 degrees after 5 mins or so of driving. It really affects the driving experience because to cool down the processor Raspberry Pi slows down itself. I bought and installed Copper cooling for Raspberry Pi to resolve the overheating problem.

Useful links

Adeept AWR 4WD WiFi Smart Robot Car Kit for Raspberry PI

Image file (local copy)

Motor HAT documentation

Tutorials and samples (local copy)

Github repo with the car code  

Copper cooling for Raspberry Pi


Next I will install dev environment and will try to write some code...