Bionic Spider Controller - Teil 1 - AZ-Delivery

In the Bionic Spider Robot Kit you have already learned how the bionic spider works and have already implemented a few projects with it. Most recently, control of the robot via a smartphone app was added.

Since the control via the touch display is not particularly nice, this blog post will present an alternative control with a joystick.

 

Hardware

There are two versions available as a joystick, one can KY-023 module can be used. However, since a lot of cabling would be necessary here, this is recommended PS2 Joystick Shield, which is also directly connected to a microcontroller Uno Layout can be inserted.

Since the microcontroller has to communicate with the robot, a microcontroller with WiFi functionality is required. Furthermore, two ADCs are required for each of the X and Y axes, which is why an ESP32 must be used here.

(The ESP8266 only has one ADC input.)

 

To realize the project you need:

ESP32 NodeMCU D1 R32

PS2 Joystick Shield

(optional) 9V block battery + Battery clip

and of course an assembled one Bionic Spider Robot Kit

 

Since the analog inputs of the two joystick axes are on GPIO 2 and 4, which are connected to the ADC2, they cannot be used at the same time with the WLAN module. The connections IO 34 and IO35 (ADC1) are located directly next to them, so functionality can be easily established using a wire bridge.

Figure 1: Wire bridge between the connections

Connect the connectors as shown above.
Since the on-board LED is connected to GPIO 2, the pin must be removed here, otherwise the connection would influence the value of the X-axis voltage.

The same applies to pin D12, as this disrupts the boot process and therefore booting with the shield attached would not be possible. The middle button of the joystick is located at this connection, which means it can no longer be used. If you still want to implement it, it must be reconnected to a free port.

 

Figure 2: Shield with modifications

 

The shield can simply be plugged onto the D1 R32. Make sure the little switch in the left corner is on 3v3 otherwise the ESP32 could be damaged by the excessive voltage of 5V.

Software

If this is your first time programming an ESP32/ESP8266 in the Arduino IDE, copy the following URLs in the Arduino IDE under: File->Preferences->Additional boards manager URLs:  https://dl.espressif.com/dl/package_esp32_index.json

http://arduino.esp8266.com/stable/package_esp8266com_index.json

 

and install the ESP32 and ESP8266 packages in the board administration.


 

The ESPNOW protocol, which is easy to use thanks to the integrated library, is suitable for transferring data between the microcontrollers.

The advantage of this protocol is that the data packets can also be transmitted without a server and WLAN, meaning communication is possible at any location and without additional hardware.

Furthermore, the packets are sent out simply, without complicated authentication as with WLAN, which means that very fast transmission can be achieved in almost real time.

 

The hardware addresses, the so-called MAC addresses (Media Access Control), of the two devices are required for communication. These are located in a protected part of the memory and can be clearly assigned to a device.

The packages can be sent to the correct recipient via this.

 

1 Determine the MAC addresses

Since the addresses are in protected memory, they can also be read out with a separate short program, as this does not change when the memory is rewritten.

 

Load the program onto the two microcontrollers:

#ifdef ESP32
  #
include <WiFi.h>
#
endif
#
ifdef ESP8266
  #
include <ESP8266WiFi.h>
#
endif

void setup() {
  
WiFi.mode(WIFI_STA);
  
Serial.begin(115200);
  
delay(2000);
  
Serial.println();
}

void loop() {
  
Serial.print("Mac Adress:");
  
Serial.println(WiFi.macAddress());
  
delay(5000);
}

 

You can get the code here(macAdress.ino) download.

 

After the programs have been loaded onto the microcontroller, the corresponding MAC address is now displayed in the serial monitor (baud rate: 115200). Write these down for the following program.

 

2 code controllers

The joystick module has 6 individual buttons and a joystick with two axes and an integrated button.

In order to keep the project as simple as possible, only the direction of movement or the pressed buttons are transmitted as a string as follows:

Joystick up: CMD_FWD

Joystick down: CMD_BWD

Joystick to the right: CMD_RGT

Joystick to the left: CMD_LFT

 

Button A: BTN_A

Button B: BTN_B

etc...

 

To ensure that a new command can only be sent if the previous one has been executed, an acknowledgment is sent by the recipient. Only when this has been received can a new command be sent.
To ensure that communication is not completely blocked in the event of an error, feedback is only required after successful data transmission to the recipient. Additionally, the status will be automatically reset after 10 seconds if no confirmation has been submitted.

 

Code:
In order to maintain clarity, only excerpts are shown here for explanation. The complete code can be downloaded at the end.

#define X 34
#
define Y 35

#
define A 26
#
define B 25
#
define C 17
#
define D 16
#
define E 27
#
define F 14

uint8_t receiverAddress[] = {0xA40xCF0x120xDC0xCF0x3A};

int calibY, calibX;
bool status = true;
long sentTime;

At the beginning, the GPIO pins of the respective button/joystick axes are defined so that they can be used more easily and clearly later in the program.
The MAC address of the receiver is then defined; this must be in the hexadecimal format shown with 0x as a prefix:
A4:CF:... -> 0xA4, 0xCF,...

The following variables are used for calibration and control to only send the commands if the previous one has already been executed.

void onDataRecv(const esp_now_recv_info_t *info, const uint8_t *data, int data_len) {
  
char dataTemp[data_len];
  memcpy(dataTemp, data, data_len);
//Copy data bytes in char Array
  
Serial.println(dataTemp);

  
if(String(dataTemp).indexOf("RDY") >= 0) {//data contains RDY
    
Serial.println("RST");
    status = true;
  }
}

void onDataSent(const wifi_tx_info_t *tx_info, esp_now_send_status_t sendStatus)
{
    
Serial.print("Sendestatus: ");
    
Serial.println(sendStatus == ESP_NOW_SEND_SUCCESS ? "Success" : "Error");
    
if(sendStatus == ESP_NOW_SEND_SUCCESS) {
      status = false; 
//marking sent
      sentTime = 
millis(); //store time for limitation
    }
}

These two methods are the callback methods for receiving and sending via ESPNOW.

The data received from the reception function is first stored in a char Array copied in order to then check the contents for the keyword. If this is received, the status will be restored true set and commands can be sent again.

The send callback function checks the status of the dispatch and sets the status variable false.false, so that a command is not sent again immediately afterwards. In addition, the current program running time is buffered using the millis() function, as the status should be reset after 10 seconds.

 

In setup() only the configuration commands of the ESPNOW library are called, followed by the calibration of the joystick axes and the GPIO configuration.

In loop() the values ​​of the joystick axes are measured and the status is reset after 10 seconds.

The following logic evaluates the position of the joystick axes:

if(status) {
    
if(valX < -50 || valY < -50 || valX > 50 || valY > 50) {
      
Serial.println("trig");
      
if(abs(valX) > abs(valY)) {
        
if(valX < 0) {
          
Serial.println("LEFT");
          
String msg = "CMD_LFT";
          esp_now_send(receiverAddress, (uint8_t *)msg.c_str(), msg.length());
        }
        
else {
          
Serial.println("Right");
          
String msg = "CMD_RGT";
          esp_now_send(receiverAddress, (uint8_t *)msg.c_str(), msg.length());
        }
      }
      
else {
        
if(valY < 0) {
          
Serial.println("BWD");
          
String msg = "CMD_BWD";
          esp_now_send(receiverAddress, (uint8_t *)msg.c_str(), msg.length());
        }
        
else {
          
Serial.println("FWD");
          
String msg = "CMD_FWD";
          esp_now_send(receiverAddress, (uint8_t *)msg.c_str(), msg.length());
        }
      }
    }

First, it is checked whether the values of the axes exceed a limit value of 50. This is necessary because both the potentiometers through which the position of the axes is read out and the analog-digital converter (ADC) move minimally around the zero value (so-called drift), even in the zero position.

If an axis is above the threshold value, the values of the magnitude of the two axes are compared with each other. In this way, the axis that was moved the most and is therefore indicative can be determined.

After selecting the axis, you only need to check whether the value is positive or negative in order to provide the exact direction instruction ESPNOW to be able to send.

 

You can see the complete program here(Controller.ino) Download and load it onto the ESP32 by selecting the correct board and port.

3 code robots

The basic structure and in particular the ESPNOW Callback methods are almost equivalent to the previous controller program.
The main difference here is that in the receive callback method the data is not compared directly but is stored in a string

cmd = String(dataTemp);

and then evaluated in loop().

 

In setup() the servo motors are also initialized and set to the zero position.

 

The evaluation then follows in the loop() cmd Strings:

if(cmd.length() > 0) {
    
if(cmd.indexOf("CMD_FWD") >= 0) {
      
Serial.println("FWD");
      forward();
    }
    
else if(cmd.indexOf("CMD_BWD") >= 0) {
      
Serial.println("BWD");
      back();
    }
    
else if(cmd.indexOf("CMD_RGT") >= 0) {
      
Serial.println("RGT");
      rightmove();
    }
    
else if(cmd.indexOf("CMD_LFT") >= 0) {
      
Serial.println("LFT");
      leftmove();
    }

    
else if(cmd.indexOf("BTN_A") >= 0) {
      
Serial.println("A");
      dance1();
    }
    
else if(cmd.indexOf("BTN_B") >= 0) {
      
Serial.println("B");
      dance2();
    }
    
else if(cmd.indexOf("BTN_C") >= 0) {
      
Serial.println("C");
      hello();
    }
    
else if(cmd.indexOf("BTN_D") >= 0) {
      
Serial.println("D");
      pushup();
    }
    
else if(cmd.indexOf("BTN_E") >= 0) {
      
Serial.println("D");
      turnright();
    }
    
else if(cmd.indexOf("BTN_F") >= 0) {
      
Serial.println("D");
      turnleft();
    }
   

    cmd = 
"";
    
const char* ack = "RDY";
    esp_now_send(senderAddress, (uint8_t*)ack, strlen(ack));
    
Serial.println("ACK gesendet");
  }

If the length of the string is greater than 0, it means that a new command has been written into the string. The content is then compared again with the respective command and the associated function of the robot is carried out.

After the evaluation, the confirmation is sent back to the controller.

 

In particular, the functions of the 6 buttons can be assigned any functions of the robot.

 

The methods of movements of the bionic robot are in a separate file from the last robot lesson. You can do this here (Commands.h) Download it and then copy it to the project directory.

 

You can see the complete program here (Spider.ino) Download and load it onto the ESP8266 by selecting the correct board and port. Don't forget to copy the robot movements file mentioned above into the directory

 

Conclusion

In this blog post, the joystick shield and an ESP32 D1 R32 became a fully functional controller for the Bionic Spider built, which can transmit commands to the robot via WiFi with ESPNOW.

The project also leaves a lot of potential for your own redesign, for example other commands can be carried out using the individual buttons, but programmed movement patterns can also be carried out at the push of a button.

Have fun replicating it :)

Esp32Esp8266Projekte für anfänger

Leave a comment

All comments are moderated before being published

Recommended blog posts

  1. ESP32 jetzt über den Boardverwalter installieren - AZ-Delivery
  2. Internet-Radio mit dem ESP32 - UPDATE - AZ-Delivery
  3. Arduino IDE - Programmieren für Einsteiger - Teil 1 - AZ-Delivery
  4. ESP32 - das Multitalent - AZ-Delivery