RoArm-M3-S ESP-NOW Control

From Waveshare Wiki
Jump to: navigation, search

ESP-NOW SETTINGS

This tutorial primarily focuses on configuring ESP-NOW for the robotic arm. After configuration, the robotic arm can achieve one-to-one and one-to-many ESP-NOW communication control. Among these, JSON commands are used to implement this function.

ESP-NOW Communication Protocol

ESP-NOW is a fast and efficient wireless communication protocol developed by Espressif Systems, aimed at providing low-power, high-performance local area network (LAN) communication solutions. It was initially designed for Espressif's Wi-Fi chips such as ESP8266 and ESP32.

The features of ESP-NOW are as follows:

1. Quick Response: The ESP-NOW protocol does not require TCP/IP stack processing, therefore it has very low latency and is suitable for applications with high real-time requirements.

2. High Throughput: The ESP-NOW protocol uses frame broadcasting to transmit data with high transmission efficiency, enabling simultaneous transmission of the same information to multiple nodes.

3. Low-power: ESP-NOW protocol uses very low power in data transmission, and is suitable for battery power supply.

4. User-friendly: The configuration of ESP-NOW protocol is easy, only by defining the basic parameters such as the channels, encryption key, and data structure can it realize the wireless communication.

5. Multicast Support: ESP-NOW protocol supports one-to-many and one-to-all node communication, improving the network flexibility.

6. Expandability: ESP-NOW protocol supports the transmission of multiple data types, and more complex communication functions can be achieved by customizing the data structure.


ESP-NOW Functionalities

RoArm-M3-S comes with ESP-NOW functionality, and by default, is in Follower mode upon factory startup. In this mode, RoArm-M3-S can be controlled by other devices or can send ESP-NOW messages to other devices to control them.

ESP-NOW Communication Data Structure

Before explaining the meanings of various functional commands, let's first introduce the ESP-NOW communication data structure of RoArm-M3-S, as follows:

typedef struct struct_message {
  byte devCode;
  float base;
  float shoulder;
  float elbow;
  float hand;
  byte cmd;
  char message[210];
} struct_message;

In this part, the specific value for variables will be set in the JSON commands. Here, we introduce the meaning of variables:

  • devCode: this device code. We do not use this variable in the demo we use, so you can choose not to set this code (always set it as 0). You can set this variable to differentiate the devices controlled by JSON commands when there are many devices that should be controlled through broadcast rather than multicast. This interface is reserved for you to modify the OnDataRecv() function of esp_now_ctrl.h in the slave demo.
  • base: BASE joint angle (in radian)
  • shoulder: SHOULDER joint angle (in radian)
  • elbow: ELBOW joint angle (in radian)
  • hand: GRIPPER/WRIST joint angle (in radian)
  • cmd: ESP-NOW control message type. For specific valuing, you can refer to the following JSON command introductions.
  • message: A series of strings. When the ESP-NOW control message type is 1, this string will be used as a JSON-formatted command, but the command content must not be a command that causes long-term blocking.


Next, let's explore the specific meaning of the related JSON command for ESP-NOW configuration.

Set Broadcast Follower Mode

{"T":300,"mode":1}

{"T":300,"mode":0,"mac":"00:00:00:00:00:00"}
  • 3000: This command is CMD_BROADCAST_FOLLOWER for setting the broadcast follower mode of this device.
  • mode:
    • 1 - [factory default] This device can be controlled by the broadcast signal from other devices.
    • 0 - This device can not be controlled by the broadcast signal from other devices.
  • mac: the MAC address of the only Leader controlling the device when the mode =0.
    • The device can be controlled only by the ESP-NOW messages sent by the devices with a given MAC address.
    • If the device does not need to be controlled by other devices, you can randomly input a MAC address such as 00:00:00:00:00:00.


Set ESP-NOW Operation Mode

{"T":301,"mode":3}
  • 301: This command is CMD_ESP_NOW_CONFIG for setting the operation mode of ESP-NOW for this device.
  • mode (byte): The code of the ESP-NOW operation mode.
    • 0 - Turn off.
    • 1 - Stream multicast Leader continuously sends its joint position information to devices in the peerList.
    • 2 - Stream unicast/Stream broadcast Leader, continuously sends its joint position messages to a Mac address. When the Mac address is set to FF:FF:FF:FF:FF:FF, it is in stream broadcast control.
    • 3 - [factory default] Follower, when the robotic arm is in this mode, it can receive the ESP-NOW message from other Leader devices, or can send the ESP-NOW message to other devices as the Leader device.

Note: Only in stream multicast, stream unicast/stream broadcast modes, the master control device continuously sends its joint position information to the controlled devices. The controlled devices will mimic the same movement when the master control device is manually rotated.


Get MAC Address of Device

{"T":302}
  • 302: This command is CMD_GET_MAC_ADDRESS for obtaining the MAC address of this device.

After inputting this command, the return value is shown below:

44:17:93:EE:FD:70

Each device has a unique MAC address. When using ESP-NOW features, except for broadcast control, you need to obtain the MAC address of the controlled device. By default, the OLED screen of each robotic arm displays its own MAC address.


Add MAC Address of Controlled Device to PeerList

{"T":303,"mac":"44:17:93:EE:FD:70"}
  • 303: This command is CMD_ESP_NOW_ADD_FOLLOWER for adding the MAC address of the controlled device to peerList, and peerList is for storing the MAC address.
  • mac: The MAC address of the controlled device to be added.

You can add dozens of MAC addresses to the peerList for multicast control. However, when using multicast control, the peerList should not include the MAC address for broadcast control: FF:FF:FF:FF:FF:FF.


Remove MAC Address of Controlled Device from PeerList

{"T":304,"mac":"44:17:93:EE:FD:70"}
  • 304: This command is CMD_ESP_NOW_REMOVE_FOLLOWER for removing the MAC address of the controlled device from peerList.
  • mac: The MAC address of the controlled device to be removed.


Multicast Control

# Multicast control, controls the device of peerList to rotate the joints to the specified angle
{"T":305,"dev":0,"b":0,"s":0,"e":1.57,"h":1.57,"cmd":0,"megs":"hello!"}

# Multicast control, sends the JSON command to turn on the LED to the devices in peerlist
{"T":305,"dev":0,"b":0,"s":0,"e":0,"h":0,"cmd":1,"megs":"{\"T\":114,\"led\":255}"}
  • 305: This command is CMD_ESP_NOW_GROUP_CTRL, which is used to send information through ESP-NOW multicast to all devices in the peerList.

Please refer to ESP-NOW Communication Data Structure. The various keys in this JSON command correspond to the structure variables as follows, with additional supplementary parts:

  • dev: the corresponding variable is devCode.
  • b, s, e, h: respectively correspond to the variable base, shoulder, elbow, and hand.
  • cmd: ESP-NOW control message type.
    • When cmd is 0, the controlled robotic arms will rotate to the specified angle of all joints.
    • When cmd is 1, b, s, e, and h are invalid. When a non-blocking JSON command is input into the message (megs), the robotic arm will execute the functionality corresponding to the JSON command.
  • megs: the corresponding variable is the message.


Unicast/Broadcast Control

# Unicast control, sends the command of controlling the joint angle to the specific device
{"T":306,"mac":"44:17:93:EE:FD:70","dev":0,"b":0,"s":0,"e":1.57,"h":1.57,"cmd":0,"megs":"hello!"}

# Broadcast control, sends the command of controlling the joint angle to all devices
{"T":306,"mac":"FF:FF:FF:FF:FF:FF","dev":0,"b":0,"s":0,"e":1.57,"h":1.57,"cmd":0,"megs":"hello!"}
  • 306: This command is CMD_ESP_NOW_SINGLE for controlling the device of peerList in unicast or broadcast control
  • mac: The MAC address of the controlled device. When the MAC address is FF:FF:FF:FF:FF:FF, the command is a broadcast signal and will be sent to all devices.

Please refer to ESP-NOW Communication Data Structure. The various keys in this JSON command correspond to the structure variables as follows, with additional supplementary parts:

  • dev: the corresponding variable is devCode.
  • b, s, e, h: respectively correspond to the variable base, shoulder, elbow, and hand.
  • cmd: ESP-NOW control message type.
    • When cmd is 0, the controlled robotic arms will rotate to the specified angle of all joints.
    • When cmd is 1, b, s, e, and h are invalid. When a non-blocking JSON command is input into the message (megs), the robotic arm will execute the functionality corresponding to the JSON command.
  • megs: the corresponding variable is the message.

Note: Whether it is multicast, or unicast/broadcast, the MAC address of the controlled device should be added to peerList first.


ESP-NOW Usage

After learning the specific meaning and usage of JSON commands, the following describes the specific use of broadcast control, unicast control, and multicast control. Here we assume that we currently have four new mechanical arms that have not been configured with ESP-NOW-related functions, and we label these four mechanical arms as A, B, C, and D.

Turn on the power supply to the four robotic arms, turn on the switch, and after the start-up is completed, follow the steps of the following three control modes.

Broadcast Control

In this part, we use the robotic arm A to control the robotic arms B, C, and D to swing together. It is not necessary to obtain the MAC address of robotic arms B, C, and D for broadcast control, and it is sufficient to use the broadcast MAC address, and the example is as follows:

Connect the robotic arm A to the PC with a USB cable through the Type-C connector, and communicate with the robotic arm A through the serial port on the computer. We can use the serial port debugging assistant or the UART monitoring of the Arduino IDE to communicate with the robotic arm A or use the Python UART communication tutorial to communicate with the robotic arm A.


First, add the broadcast MAC address. Use the following command to add the broadcast MAC address to the peerList of robot arm A:

{"T":303,"mac":"FF:FF:FF:FF:FF:FF"}

Once added, you can send JSON command messages to robotic arms B, C, and D using the commands for broadcast control.


For example, use the following command to send the command to turn on the LED light to robotic arms B, C, and D via robotic arm A.

{"T":306,"mac":"FF:FF:FF:FF:FF:FF","dev":0,"b":0,"s":0,"e":0,"h":0,"cmd":1,"megs":"{\"T\":114,\"led\":255}"}

After the command is successfully sent, the LEDs of robotic arms B, C, and D will be on.

The above is just an example of sending the signal for broadcasting control, you can also send other JSON commands to robotic arms B, C, and D. Please refer to the meaning of broadcasting control commands of ESP-NOW function for specific modification.


Next, use the following command to turn off the torque lock on the robotic arm A, then you can manually swing it.

{"T":210,"cmd":0}


You can turn on [stream broadcast Leader] mode by setting the "ESP-NOW operation mode" of the robotic arm A. (When there is only the broadcast MAC address in the peerList, the original stream unicast mode is the stream broadcast mode.)

{"T":301,"mode":2}

At this time, the robot arms B, C, and D will swing to the same position as the robot arm A, and when you turn the joint of the robot arm A, the robot arms B, C, and D will also rotate accordingly.


Finally, by changing the "working mode of ESP-NOW" of the robotic arm A to the follower mode, i.e., turn off the [stream broadcast leader] mode. Enter the following command:

{"T":301,"mode":3}

Note: All of the above JSON commands are communicating with Robotic Arm A.


Unicast Control

In this part, we introduce how the robotic arm A controls the robotic arm B only. In the unicast control mode, it requires the MAC address of the controlled device, that is, the MAC address of the robotic arm B. The MAC address can be obtained from the OLED screen of the robotic arm B, such as BB:BB:BB:BB:BB:BB.

Connect the robotic arm A to the PC with a USB cable through the Type-C connector, and communicate with the robotic arm A through the serial port on the computer. We can use the serial port debugging assistant or the UART monitoring of the Arduino IDE to communicate with the robotic arm A or use the Python UART communication tutorial to communicate with the robotic arm A.


First, add the MAC address of robotic arm B. Use the following command to add the MAC address of robot arm B to the peerList of robot arm A:

{"T":303,"mac":"BB:BB:BB:BB:BB:BB"}
#Change the MAC address to the MAC address of the device to be controlled

After adding successfully, use the command of unicast control to send a JSON command message to the robotic arm.

Note: If the MAC address of robotic arm B has been added before, adding it again will show Add Failure due to a duplicate MAC address, but as long as the MAC address of robotic arm B is correct, it will still be determined as the designated controlled device for unicast control. When the [Stream Unicast Leader] mode is turned on, only the robotic arm B will follow the joint position information from robotic arm A.


For example, use the following command to send the command to turn on the LED light to robotic arm B.

{"T":306,"mac":"BB:BB:BB:BB:BB:BB","dev":0,"b":0,"s":0,"e":0,"h":0,"cmd":1,"megs":"{\"T\":114,\"led\":255}"}

If you have operated the broadcast control, the LED of the robotic arm B is on, and you can use the following command to turn off the LED of the robotic arm B.

{"T":306,"mac":"BB:BB:BB:BB:BB:BB","dev":0,"b":0,"s":0,"e":0,"h":0,"cmd":1,"megs":"{\"T\":114,\"led\":0}"}


Next, use the following command to turn off the torque lock on the robotic arm A, then you can manually swing the robotic arm A.

{"T":210,"cmd":0}


You can turn on [stream unicast Leader] mode by setting the "ESP-NOW operation mode" of the robotic arm A. If you operate the broadcast control, the MAC address newly added is the MAC address of robotic arm B. Hence, the [stream unicast Leader] mode only sends the joint angle information to the robotic arm B.

{"T":301,"mode":2}

At this time, the robot arm B will swing to the same position as the robot arm A, and when you turn the joint of the robot arm A, the robot arm B will follow while the robotic arms C and D will not move.

Note: All of the above JSON commands are communicating with Robotic Arm A.


Multicast Control

In this part, we introduce how the robotic arm A controls the robotic arm B and C to swing. In the multicast control, it requires the MAC addresses of the controlled device, that is, the MAC addresses of the robotic arms B and C. The MAC addresses can be obtained from the OLED screens of the robotic arm B and robotic arm C, such as BB:BB:BB:BB:BB:BB and CC:CC:CC:CC:CC:CC.

Connect the robotic arm A to the PC with a USB cable through the Type-C connector, and communicate with the robotic arm A through the serial port on the computer. We can use the serial port debugging assistant or the UART monitoring of the Arduino IDE to communicate with the robotic arm A or use the Python UART communication tutorial to communicate with the robotic arm A.


First, add the MAC addresses of robotic arm B and robotic arm C. Use the following commands to add the MAC addresses of robotic arms B and C to the peerList of robotic arm A:

{"T":303,"mac":"BB:BB:BB:BB:BB:BB"}
{"T":303,"mac":"CC:CC:CC:CC:CC:CC"}
#Modify the MAC addresses to the MAC addresses of the two devices to be controlled

If you have operated the unicast control, you have added the MAC address of B to the peerList, then executing the JSON command to add the MAC address of robotic arm B may report an error, so just ignore it.

If you have operated the broadcast control and added the addresses, you need to remove the broadcast address. Use the following command to remove the broadcast address of robotic arm A from the peerList:

{"T":304,"mac":"FF:FF:FF:FF:FF:FF"}


You can use the following command to send the command to turn on the LED light to robotic arm B and robotic arm C.

{"T":305,"dev":0,"b":0,"s":0,"e":0,"h":0,"cmd":1,"megs":"{\"T\":114,\"led\":255}"}

If the LED is on, you can use the following command to turn off the LEDs of the robotic arm B and robotic arm C.

{"T":305,"dev":0,"b":0,"s":0,"e":0,"h":0,"cmd":1,"megs":"{\"T\":114,\"led\":0}"}


Next, use the following command to turn off the torque lock on the robotic arm A, then you can manually swing it.

{"T":210,"cmd":0}


Turn on [stream multicast Leader] mode by setting the "ESP-NOW operation mode" of the robotic arm A, and then the angle information of the robotic arm A will be sent to the robotic arms B and C.

{"T":301,"mode":1}

At this time, the robot arms B and C will swing to the same position as the robot arm A, and when you turn the joint of the robot arm A, the robot arms B and C will also rotate accordingly.

Note: All of the above JSON commands are communicating with Robotic Arm A.


RoArm-M3-S Tutorial