Raspberry Pi Robotic Arm API Controller

by bongowindsor in Circuits > Raspberry Pi

310 Views, 5 Favorites, 0 Comments

Raspberry Pi Robotic Arm API Controller

1611642860539157.png
Screen Shot 2023-12-28 at 10.53.51 PM.png

This guide covers how to set up a simple HTTP server to remotely control a Raspberry Pi robotic arm by accessing the API functions. The robot arm and library (for coding) come from https://www.adeept.com/learn/tutorial-284.html. This is done through a few simple steps. 1) building the robotic arm (not covered, you can just follow the guide in the link or utilize your own 3-motor robotic arm), 2) coding an HTTP server and setting up API, and 3) setting up the server and running commands to the robotic arm remotely.

The diagram above shows the networking access for the most part, but once you run the server you can run commands to the motors using curl in the terminal or by typing in POST/GET requests through the search bar and raspberry pi IP

Supplies

Raspberry Pi, any robotic arm (# of motors don't really matter as you can adapt the code to it anyway), a computer

Building the Robotic Arm and Downloading Prerequisite Libraries

Follow https://www.adeept.com/learn/tutorial-296.html for how to build the robotic arm we will use (you can remove the claw if wanted, or kept if desired).

Set up your Raspberry Pi so that you can SSH to it and control the Raspberry Pi remotely from your computer terminal line (you can follow any tutorial, https://www.onlogic.com/company/io-hub/how-to-ssh-into-raspberry-pi/, this one is an example).

Make a folder within the Pi for this project using the mkdir or any other terminal command that works

Install the needed library by copy-pasting it into the terminal:

sudo git clone https://github.com/adeept/adeept_rasparms.git

Install code to run an HTTP server by copy-pasting this into the terminal:

sudo git clone https://github.com/BongoWindsor/robotArmAPI.git

Explaining the Server Code

The code can be accessed here: https://github.com/BongoWindsor/robotArmAPI/blob/main/web.py

Essentially, all APIs work through two types of queries: POST requests and GET requests

GET usually just takes information from the API, while POST sends information

Utilizing these general functions, we create many POST functions that utilize the robotic arm library to be able to control the arm and GET requests to receive the position of the arm

The code has 9 simple functions of POST/GET type, and here they are:

post_ServoA(), post_ServoB(), and post_ServoC() allow for individual control of each motor when given an angle between -90 and 90 degrees

get_ServoA(), get_ServoB(), and get_ServoC() allow the user to read the current angle of each motor

get_Status() allows the user to check if the HTTP server is up and running well with no problems

post_Reset() resets all motors back to a default degree of 0

post_Multiple() allows for control of all 3 motors, utilizing an input of oxxoxxoxx, where o is either 0 or -, indicating whether the degree is positive or negative, and xx is a 2-digit number referring to the degrees of rotation, and thus total being a string of 3 2 digit numbers positive/negative

Running the Server

Screen Shot 2023-12-28 at 11.22.39 PM.png

SSH onto the Pi, and enter the folder holding web.py (or whatever you named it).

Type into terminal: python3 web.py runserver

This sets up the server, and you can begin accessing the APIs through many methods; here are two such ways to access the API and control the robotic arm.

Method 1 (curl):

Opening up another terminal, we can utilize the curl command to access the API.

Running GET requests requires just using curl with the domain name + the request name, which is either Servo_A, Servo_B, Servo_C, or status

Running POST commands requires data input, so here is the format:

curl --data "oxxoxxoxx" http://ipORlocalhost/multiple

This example utilizes curl to run the post_Multiple function to move all 3 motors at the same time, but the same can be done by utilizing Servo_A or any of the other commands with the appropriate data.

Method 2 (Search Bar):

Instead of using curl, you can just run the HTTP link in your search bar with the localhost domain name, and it should work the same as curl

You cannot use the private IP of the raspberry pi to access the API this way, however.