afr2903 / FrED-factory

Tec de Monterrey's FrED factory 3rd generation
4 stars 1 forks source link

Gripper control #12

Closed afr2903 closed 6 months ago

afr2903 commented 6 months ago

Gripper control with ESP32

Control of the dynamic positions of the gripper using the ESP32 board and the UDP port, sending instructions via internet and Python.

afr2903 commented 6 months ago

ESP32 setup Arduino CLI

Currently, I'm testing the ESP32S board in Windows 11 and Arduino CLI.

Additional boards

This guide was followed for setting up the board with Arduino CLI. Inside the team2 folder, a file arduino-cli.yaml was created where the additional_urls were changed to include the ESP32 boards:

board_manager:
  additional_urls:
    - https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json

This commands were executed inside the root of the team2 folder:

arduino-cli core update-index --config-file arduino-cli.yaml
arduino-cli core install esp32:esp32
arduino-cli compile --fqbn esp32:esp32:esp32 .
arduino-cli upload --fqbn esp32:esp32:esp32 -p COM4 .

The following error appeared:

A serial exception error occurred: Write timeout
Note: This error originates from pySerial. It is likely not a problem with esptool, but with the hardware connection or drivers.
For troubleshooting steps visit: https://docs.espressif.com/projects/esptool/en/latest/troubleshooting.html
Failed uploading: uploading error: exit status 1

I will test the same configurations in Ubuntu 22.04

afr2903 commented 6 months ago

Wifi test ESP32

After some research, I found the initial test when developing with ESP32 is to test the Wifi connections. Inside the wifi-test folder I created an wifi-test.ino script that scans for the Wifi networks.

I switched to Ubuntu 22.04 and repeated the steps above for importing the esp32 boards. The development was smoother. Before uploading the code, I executed:

arduino-cli board list

And added the port permissions to the port displayed:

sudo chmod a+rw /dev/ttyUSB0

The code was downloaded successfully, and to watch the serial port I used the command screen:

screen /dev/ttyUSB0 115200

This is the output: image

afr2903 commented 6 months ago

Servo test

The first attempt was to use the library Servo.h, but this was incompatible with the esp32. Another library ESP32Servo.h was found here. The example file was copied and modified in servo-test.ino with the physical values from the datasheet of our servomotor PDI-6225MG-300:

After testing the limits of both DOFs, the edge values were found:

Big fingers gripper

Small fingers gripper

afr2903 commented 6 months ago

Dual Servo state config

In the main.ino file I started creating a struct and Enum to set there predefined positions names.

The predefined positions debugging in the serial monitor make sense, and are adjusted to the limits, but when connected to the ESP the servos don't move.

Is more likely an issue with the electronics, until that is solved, I'll check the UDP port. However, for testing it may be better to use the sweep program servo-test.ino and make a dual sweep before jumping to the final code with states and well formatted.

afr2903 commented 6 months ago

UDP Port testing

This script is used as a base for testing the Wifi UDP port with the hostpot in my cellphone. The developments are in the udp-test.ino file.

ESP connection via UDP

WIth this IP and the network information I was able to successfully connect to the Wifi hotspot of my phone:

const char* ssid = "afr2903";
const char* password = "12345678"; // Set password
const int udpPort = 1234;

WiFiUDP udp;

IPAddress staticIP(192, 168, 18, 23); // Set the desired static IP address
IPAddress gateway(192, 168, 18, 1);    // Set the gateway of your network
IPAddress subnet(255, 255, 255, 0);    // Set the subnet mask of your network

This script prints the information received via UDP into the Serial monitor, to open it type:

screen /dev/ttyUSB0 115200

If the ESP already connected to the Wifi, it won't display anything until a new command is received. If it has problems connecting, it will continuosly display Connecting to WiFi... Recurrent bug: Sometimes the wifi-scan.ino has problems detecting my network, and I need to reboot the hotspot.

Python send commands via Socket

To send commands via the socket library to the ESP IP address, it was created the script wifi-test.py, it has to be connected to the same network. I'm iterating through a dictionary that associates the states to numbers:

instructions = {
    "HOME": 0,
    "PICK_ARDUINO": 1,
    "PLACE_ARDUINO": 2,
    "PICK_RAMPS": 3,
    "PLACE_RAMPS": 4,
    "PICK_DRIVER": 5,
    "PLACE_DRIVER": 6,
    "PICK_WIRE": 7,
    "PLACE_WIRE": 8,
    "PICK_ASSEMBLY": 9,
    "PLACE_ASSEMBLY": 10
}

This is the final result: image

afr2903 commented 6 months ago

Complete WiFi state control

The main.ino file was updated and a refactorization of the teleop.ino file, receiving numeric positions via UDP. Then, the main.py file was created as a OOP script, to send predefined states to the gripper:

GripperStates = {
    "HOME": [100, 40],
    "PICK_ARDUINO": [80, 40],
    "PLACE_ARDUINO": [110, 40]
}

Then, to test these behavior, a simple routine iterating these states and sending the commands via the following function was implemented and tested successfully:

def send_gripper_state(self, state):
    """Send the data to the ESP32 via socket"""
    board_position, wire_position = self.GripperStates[state]
    print(f"Sending gripper to state: {state}")
    # Send board finger position
    message = 'b' + str(board_position)
    self.sock.sendto(message.encode(), (UDP_IP, UDP_PORT))
    time.sleep(0.2)
    # Send wire finger position
    message = 'w' + str(wire_position)
    self.sock.sendto(message.encode(), (UDP_IP, UDP_PORT))
    time.sleep(0.2)

Now, the following step is to add xArm movements during the routine and create a simple state machine