Vierbeiniger Roboter mit PCA9685 und 12 Servomotoren - Teil 2 - AZ-Delivery

In Part 1 We have already seen this project on how to assemble our robot, to program the first movements of the legs and the sequence of movements. In this part 2 of the project we will explain how we install and activate the SR04 ultrasonic sensor to recognize obstacles. We will also install an infrared receiver to receive our commands from an infrared transmitter. Since we also want it to move autonomously, we will install batteries and let it off the line.

Required hardware components

12

MG90S micro servo engine


1

PCA9685 16 channel 12 bit PwM servo driver


1

AZ-ATMEGA328DIP board microcontroller Board ATMEGA16U2

Contained in the electronics of super starter kit

1

Prototype Shield Mini Breadboard for UNO R3

Contained in the electronics of super starter kit

1

HC-SR04 Ultrasound module

Contained in the electronics of super starter kit

1

KY-022 SET IR Recipient Module

Contained in the electronics of super starter kit

1

IR remote control

Contained in the electronics of super starter kit

various

Jumper wire cable

Contained in the electronics of super starter kit

Required materials

Required software and sketches

Circuit diagram and description

Circuit diagram download

An HC-SR04 ultrasonic sensor was added to recognize the distance to an obstacle. The supply voltage of 5 VDC is supplied by the batteries. Instead of the USB socket, you can also use the VIN PIN or the Power Supply. Then you have to make sure that you create more than 5V, because the VIN PIN is connected to a voltage controller.

The echo-pin of the sensor is connected to the digital PIN 8 of the microcontroller, the trig pin with the digital pin 9. Echo sends an ultrasound pulse, trigger it receives it. The sensor of the infrared receiver KY-022 is connected to PIN 11 of the microcontroller. This sensor receives the infrared signal of the remote control. The necessary voltage for this module is also 5 VDC, which can be fed with the power supply or the battery.

Description of the sketch quadruped_robot_walk_sonic.ino

With the installation of the HC-SR04 ultrasonic sensor, we want to implement the property in the robot, to carry out an action if it recognizes an obstacle at a certain distance. In this case, the action is to get into the starting position of Home () to go when an obstacle is recognized at a distance of less than 30 centimeters.

So that our robot can carry out this action, we work out with the last sketch Part 1, quadruped_robot_walk.inowith which our robot could run. In order to activate the ultrasonic sensor, the library "SR04.H" is included at the beginning of the sketch. It contains the necessary functions for the correct operation of the sensor.

#include "SR04.h"

Next we have to tell the microcontroller which pins the sensor is connected to. To do this, we first define two constants: trig_pin_front on the digital PIN 9 and Echo_pin_front on the digital pin 8. Then we have to implement an object from the SR04 library (ultrasonics_ensor_front) and handed over the names of the constants as parameters. To save the distance data, we will be a global variable of the type long define.

#define Trig_pin_front 9
#define Echo_pin_front 8
SR04 ultrasonics_ensor_front = SR04(Echo_pin_front,Trig_pin_front);
long Front_Distance;

The sensor should now be functional. If you have problems, you should write a short sketch and have the sensor values ​​output. In the set up()-Method we do not have to make any changes in relation to this sensor.

Maybe you remember that we are in the Loop () the function walk() have called. At this point we now have to insert a condition. As soon as an obstacle is recognized, the robot should stop. With the extended condition if-else  Let us make it run or move.

First we call in the Loop () the function Front_Distance_object () on.

Front_Distance_object();

It becomes the function Spacer() of ultrasonics_ensor_front-Objekt carried out by our HC-SR04 sensor. This function converts the signal received by the trig pin of the sensor to a distance in centimeters and stores this value in the global variable Front_Distance. Then the distance value via the serial monitor with the function Serial.print () displayed. The output only serves for test purposes. You can also comment on or remove the output on the serial monitor later.

void Front_Distance_object() {
      Front_Distance = ultrasonics_ensor_front.Spacer();
      Serial.print("Distance to Front Obstacles");
      Serial.print(Front_Distance);
      Serial.print("Cm");
      delay(100);
}

After returning to Loop ()-Function contains the global variable Front_Distance a value used for the following condition. We compare with the numerical value 30 (i.e. 30 cm). Depending on this distance is walk(), or Home () called. The robot is running or stops.

IF (Front_Distance >= 30) { walk(); }
          Else { home();}

All of this runs in the loop () continuously. With each run, the distance is newly measured and then decided whether they run or stop.

The rest of the code are the functions walk() and Home ()that already in Part 1 were described.

Download quadruped_robot_walk_sonic.ino

The robot is running at the moment and when he discovers an obstacle, it stops. We now want to expand the project with IR remote control. For this we use the IR sensor module. With the remote control, we will carry out a certain action by pressing a number. We use the previous sketches and summarize them all in this one sketch. Then we can run all the individual movements with the remote control.

Description of the sketch quadruped_robot_ir_sonic.ino

Let's start with the integration of the IR sensor. The right library is also included in this and an object is instantiated from it. We then need a variable in which we store the number of remote control. Depending on this value, the associated actions will be carried out later.

We also need the libraries for the motor driver, I2C communication and the ultrasonic sensor.

#include <Wire.H>
#include <Adafruit_pwmservodriver.H>
#include "Irremote.h"
#include "SR04.h"

We are addressed by the engine driver, they know Part 1. We declare two constants for the maximum value of the servos and the speed of movement, which is realized by breaks.

Adafruit_pwmservodriver Servodriver_Module = Adafruit_pwmservodriver();
#define Servomin  100
#define Servomax  500

intimately movement_speed = 50;

We declare a variable with the pin number to which the IR sensor is connected. We also instance an object from the Irremote Library, which we hand over this PIN. Next we need a variable in which we save the pressed button. This is an object of the Decode_Results class, the address of which can later be handed over to the decoder method.

intimately Receiver_ir_Module = 11;
Irrecv Action_Selected(Receiver_ir_Module);
decode_results number;

In the next four lines we use the same code as before to be able to use the ultrasonic sensor. Trigger and echo-pin, as well as an SR04 object and the front distance-Variable in which the distance is saved.

#define Trig_pin_front 9
#define Echo_pin_front 8
SR04 ultrasonics_ensor_front = SR04(Echo_pin_front,Trig_pin_front);
long Front_Distance;

In the set up()-Function is initialized by the serial monitor and the servo driver module. The PWM frequency of the engines is 50 Hz. Then the Home ()-Function of the robots brought into its basic position. We also take over from the first part. Finally, we initiate the infrared module.

void set up() {
      Serial.Begin(9600);
      Servodriver_Module.Begin();
      Servodriver_Module.Setpwmfreq(50);
      home();
      Action_Selected.enableirin();
}

In the Loop ()-Function is regularly checked which value is received by the infrared sensor. The function Decode () If the address of the DECODE_Results object is handed over and checks in the IF statement whether the received value is a number. If so, Translateir () is called up and then reset the IR reception in order to obtain a new value. If no number has been recognized, translateir () is still called, but with the old value.

void loop() {
      
      IF (Action_Selected.decode(&number)) {
            translateir();
            Action_Selected.resume();
      } Else {
            translateir();
      }      
}

A Switch case statement is carried out in the Translateir () function. Depending on the received value of the remote control, the various functions for the movement of the robot are called up.

void translateir() {
      switch(number.value) {
            case 0xff6897: home();
                break;
            case 0xff30cf: pushups();
                break;
            case 0xff18e7: shoulder();
                break;
            case 0xff7a85: walk();
                break;   
            default: Serial.print("Other button");
      }
}

The functions that are called up here were in Part 1 this article described, namely: Home (), Pushups (), Shoulder () and walk(). The numbers that are pressed to carry out the above methods are:

  • number 0xff6897 (Number 0 on the remote control) leads the function Home () out of
  • number 0xff30cf (Number 1 on the remote control) leads the function Pushups () out of
  • number 0xff18e7 (Number 2 on the remote control) leads the function Shoulder () out of
  • Number 0xff7a85 (number 3 on the remote control) leads the function walk() out of

IMPORTANT NOTE: The hexadecimal values ​​of the remote control keys change depending on the manufacturer. The hexadecimal numbers of the remote control of the Electronics Super Starter Kit are:

  • 0xff629d corresponds to the "Up" button.
  • 0xff22DD corresponds to the "left" button.
  • 0xff02FD corresponds to the "OK" key.
  • 0xFFC23D corresponds to the "right" button.
  • 0xffa857 corresponds to the "AB" button.
  • 0xff9867 corresponds to the "2" button.
  • 0xFFB04F corresponds to the "3" button.
  • 0xff6897 corresponds to the "1" button.
  • 0xff30cf corresponds to the "4" button.
  • 0xff18e7 corresponds to the "5" button.
  • 0xff7a85 corresponds to the "6" button.
  • 0xff10ef corresponds to the "7" button.
  • 0xff38c7 corresponds to the "8" button.
  • 0xff5aa5 corresponds to the "9" button.
  • 0xff42BD corresponds to the "*" button.
  • 0xff4ab5 corresponds to the "0" button.
  • 0xff52ad corresponds to the "#" button.

If you use a different remote control, use a sketch that shows you the values ​​in the serial monitor and take it over for your source code.

Of the four functions called have neither Home (), still Pushups (), still Shoulder () compared to what in Part 1 of the project was changed. The walk()-Method is expanded by the distance measurement with the SR04 ultrasonic sensor. I will now show you how I have implemented it in this sketch.

First becomes Front_Distance_object () called up that measures the distance to the next frontal obstacle and the value in the global variables Front_Distance saved. It is now compared again whether the value of the larger or equal 30 (cm) is. If the condition is true, the movement is carried out. Otherwise the Home ()-Function called up and the robot stops until the obstacle is out of rich.

void walk() {

      Front_Distance_object();

      IF (Front_Distance >= 30) {

            Servodriver_Module.setpwm(0, 0, 450);
            delay(movement_speed);
      . . . . .
      . . . . .
      . . . . .
            Servodriver_Module.setpwm(4, 0, 400);
            delay(movement_speed);
      
      } Else {
            home();   
      }
}

So that the robot can now move freely, I have installed two battery packs and switched in parallel. This results in a current of 2A at 5 VDC. I connected it to the USB port, as can be seen in the circuit diagram above.

So we have now arrived at the end of the project. I cordially invite you to expand the project. For example, you could implement movement sequences that change the direction of the robot. As a result, he could then autonomously avoid the obstacles.

We hope that you liked this project.

Für arduinoProjekte für anfängerStromversorgung

Laisser un commentaire

Tous les commentaires sont modérés avant d'être publiés

Articles de blog recommandés

  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