Tuesday, 11 December 2018

Arduino Ant Hexapod Robot

In this tutorial I will show you how I built an Arduino hexapod. As the same suggests, the hexapod has 6 legs but in addition to that, it also has a tail or abdomen, a head, antennas, mandibles and even functional eyes. All of this, makes the hexapod look like an ant, so therefore we can also call it an Arduino Ant Robot.

Overview

For controlling the robot I made a custom-built Android application. The app has 4 buttons through which we can command the robot to move forward or backwards, as well as turn left or right. Along with these main functions, the robot can also move its head and tail, as well as it can bite, grab and drop things and even attack.

Arduino Ant Robot Hexapod - Android App Control

As I mentioned earlier, the robot has functional eyes, or I specifically designed the head to fit an ultrasonic sensor. So if we try to touch the robot head or get our hand close to the sensor, the robot will initially prepare for an attack.

Arduino based Ant Robot - Attack mode - object detection

If we move back, the robot will dismiss the attack, but in case we get our hand closer to it, it will attack and bite us. So how cool is that? Stick around and you will find out exactly how I build it and how everything works.

Arduino Hexapod – Ant Robot 3D Model

As usual, I started by designing the hexapod using a 3D modeling software. The hexapod has 6 legs and each of them consist of 3 joints or 3 servos. That means we need total of 18 servos and in my case I used the MG996R servos.

Hexapod 3D Model

On the back side we have the tail which is driven by another MG996R servo. The head of this ant robot has two degrees of freedom, or it can roll and tilt, and again it’s driven by two more servos. So we need total of 21 servos for this project, type MG996R and additionally one smaller SG90 micro servo for the mandibles.

Arduino Ant Robot 3D Model

Here we also have the eyes of the ant which are design to fit an HC-SR04 ultrasonic sensor. All of the parts are assembled between two plates and additionally I made an interesting curved cover for the top plate in order to hide all of the wiring, the Arduino and the battery between the two plates.

You can download the below:

Solidworks files:

STEP file:

STL files:

 

3D Printing the Parts

I guess you already know what’s next, and that’s 3D printing the robot parts. I used the Creality CR-10 3D Printer for all of the prints and it did a great job. The most difficult part to print was the head, because I wanted to be a single print.

3D Printing the ant robot head - cura settings

For that purpose I had to use a Support Overhang Angle of 60 degrees as well as some support blockers. Anyway, the Creality CR-10 did a great job and the head turned out perfectly.

Creality CR-10 printing 60 degrees overhang angle

Assembling the Arduino Hexapod

Once we have all of the parts printed we can move on with assembling the hexapod. I started with assembling the legs. For securing the servos to the printed parts I used M3 bolts and nuts, as well as spring washers. The length of the bolts needs to be at least 12 mm and in my case I used 16mm long bolts.

Assembling the hexapod legs with MG996R servos

We need around 200 bolts for the whole assembly. For connecting the links to each other we use the round horns which come along with servo package as accessories. However, we need to drill 3mm holes on each of them so that the bolts can pass through, or we can use metal round horns which have M3 threads and can be bought separately.

MG996R servo round horns

When securing the links to the servos we need to make sure that we always connect them at the same position and they have the full range of motion.

We should notice here that the servos have a small triangular support on the top side which needs to be removed. I used a simple utility knife to do that, so in that way the servos can be placed flash with the printed parts. Before inserting the third servo in place, we first need to insert an M4 bolt which will be used for connecting the leg with the base plate.

Here’s how the leg assembly should look like. It’s actually very easy to assembly it but now we need five more of them.

Hexapod leg with 3 servos

Once we have all of the legs ready, we can move on with installing them to the body of the robot, or the two plates. First we need to secure the round horns onto the top platform using the same method as previously, with M3 bolts and nuts. Then we can simply connect them to the servos shafts using the bolts, but before we do that we need adjust the position of the servos to be exactly in the middle.

attaching the legs to the hexapod body

This is needed so that we can get the full range of motion of the servos and also will reduce the adjustment or the calibration process when programing the Arduino.

After securing the legs, this project already started to taking shape and look like a hexapod.

builing the arduino hexapod robot

There is one more round horn of the back side and that’s for the tail servo which also needs to be secured that this point.

Next we need place the hexapod upside down and so we will be able to insert the bottom plate through the M4 bolts of the legs. Then I secured the legs to it using some plain washers and self-locking nuts. We should be careful how much we tighten these bolts because these are actually the pivot joints and the legs should also be able to rotate while being secure enough.

tightening the pivot joints

Next we need to install the first servo for the head and that’s for the roll movement. This servo should be placed perpendicular to base plates, so for that purpose I made two small plates which are first secured to the servo. Then we can insert the servo between the two plates and we can easily secure it with the M3 bolts and nuts.

securing the roll servo motor

Then we have a U shaped bracket and here we need attach two round horns for connecting the two servos for the head. Again before securing the bracket to the roll servo we need to make sure that the position of the servo is in the middle so it can rotates 90 degrees in both directions.

making the head mechanism

Next is the tilt servo bracket. So here first we need to insert an M4 bolt and secure it to U shaped bracket. Before mounting the tilt motor we also need to insert four M3 bolts which will be later used for securing head. Then we can insert the tilt servo to the bracket and carefully fit the shaft to the round horn. It’s a bit tight here but the U shaped bracket can helpfully flex a little bit.

securing the tilt servo

Lastly we need to secure the servo with the M3 bolts and with that the head mechanism is completed. Now it can both roll and tilt.

Before attaching the head to the mechanism we need to preassemble it, or attach the mandibles with the small SG90 servo and the ultrasonic sensor. Again it’s a bit tight here but I still managed to insert the first mandible and secure it to the head using a M4 bolt.

assembling the arduino ant robot head

You can notice that there is a small hole at the eyes area which is specifically designed so we can pass through a screwdriver to tighten the bolt.

securing the ant mandible

Next goes the micro servo in place and it is secured using two screws. On the second mandible we first need to attach a small arm horn for the SG90 servo.

securing the S90 micro servo for the mandibles mechanism

Then we can insert the mandible in place, pair the two gears and secure it to the motor shaft using a screwdriver.

Next we can put the ultrasonic sensor in place. The eyes holes are made exactly to fit the ultrasonic sensor so I simply used few drops of AC glue to secure the sensor to the head.

inserting the ultrasonic sensor for object detection

There is one more detail to be added to the head and that’s the antennas. For that purpose I used 3mm tick wire, which I cut it around 13cm in length and I slightly bend it to get the desired shape. Again I used few drops of AC glue to secure them to the head. Finally we can attach the head to the roll and tilt mechanism with the four bolts we previously inserted.

Arduino ant robot head with mandebles, ultrasonic sensor and antennas

The head is now fully functional, it can roll, it can tilt and even it can bite. There are two more 3D printed parts to be installed. That’s the tail which can simply slide into the tail bracket and the curved cover which we will actually use it at the end to cover up the electronics. So this is the final appearance of our ant robot and I really like how it turned out in this blue and white color combination.

3D Printed Arduino Ant Robot

Arduino Ant Robot Circuit Diagram

Ok, now we can move on with the electronics. Here’s the circuit diagram of this project which is actually simple although it looks a bit complex because of the many servos connections.

Arduino Hexapod Ant Robot Circuit Diagram Schematics

 

Besides the 22 servos we need an HC-05 Bluetooth module for the smartphone communication and some capacitors and resistors. Of course the brain of the robot is an Arduino board, and in this case that’s the Arduino Mega because it’s the only board that can control more than 12 servos using the Servo library.

Related Tutorial: How Servo Motors Work & How To Control Servos using Arduino

For powering the robot I will use a 3S LiPo battery which has a voltage of around 12V. LiPo batteries can handle higher amount of current draw, so they are suitable for this project because if all servos are engaged at the same time at full load they can draw around 10 amps of current.  However, the servos operating voltage is limited from 4.8 to 7.2V, which means that I need to use a DC-DC buck converter to convert the 12V to 5V. Even in case we use 2S LiPo battery which has a voltage around 7.4V or 8.4V when fully loaded we still need to use a buck converter. The buck converter that I will use for this project can handle up to 8 amps of current but I would recommend to use one from 10 to 15A just to be sure you will have enough power and it won’t overheat. In my case, the maximum current draw that I noticed from the robot when moving was around 6 amp.

You can get the components needed for this project from the links below:

  • MG996R Servo Motor……………………….…. Amazon / Banggood
  • SG90 Micro Servo Motor ……..…….….……. Amazon / Banggood
  • HC-05 Bluetooth Module ………………….… Amazon / Banggood
  • Arduino Mega Board ……………………….…. Amazon / Banggood
  • 3S LiPo Battery ………………………..……….. Amazon / Banggood
  • DC-DC Buck Converter ………………………. Amazon / Banggood

*Please note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

Designing a PCB for the Arduino Hexapod

Now if we try connect everything together it will be quite a mess because of the many servo connections. Therefore I designed a custom PCB using the EasyEDA free online circuit design software. This PCB will be actually act as an Arduno Mega Shield for the Hexapod because we will be able to directly connect it on top of the the Arduino Mega Board. I arranged the servos connections close to each other and included two big capacitors next to them to keep the voltage more stable. Also I included a connection for a NRF24L01 transceiver module in case we want to control the robot using a radio control. There are several digital and analog pin connections, 5V and ground connections, two LED connections, as well as a connection for the monitoring the battery voltage. The 12V battery voltage will pass through a voltage divider made up of two resistor R1 and R2 which will reduce the voltage below 5V so that the analog pin can safely read it. In this way we will know when the battery will need to be recharged.

EasyEDA for making a custom PCB

Here’s a link to the project files of this PCB design. So once I finished the design, I generated the Gerber file need for manufacturing the PCB.

Gerber file:

Then I ordered the PCB from JLCPCB which are actually the sponsor of this video.

Here we can simply drag and drop the Gerber file and once uploaded, we can review our PCB in the Gerber viewer. If everything is all right then we can go on and select the properties that we want for our PCB. In this case I chose the PCB color to be blue in order to match with the Arduino board color. And that’s it, now we can order simply our PCB at a reasonable price. Note that if it’s your first order from JLCPCB, you can get up to 10 PCBs for only $2.

PCB order from JLCPCB

After several days the PCBs have arrived. The quality of the PCBs is great and everything is exactly the same as in the design.

Arduino Hexapod Custom Design PCB for 24 servos

Assembling the PCB

Ok, now we can move on and assemble the PCB. I started by soldering male pin headers to the PCB which are used connecting it to the Arduino board. Once we place the pin headers on the bottom side, we can use some kind plate hold the pins and flip the board. Now we need to solder all of them to the PCB. Once finished with that, we can move on with the servo connections for which we also need male pin headers.

24 channel custom design PCB for Arduino hexapod

At this point we can actually insert the pin headers for all connections and use the same method to flip the PCB and solder all of the pins to it. At the end we need to solder the resistors, the capacitors and the terminal blocks. And that’s it, the Arduino Mega Shield for our Ant Robot is now ready. Now we can simply insert it onto the Arduino board.

Custom design Arduino Mega shield for hexapod

Next we need to set the buck converter output voltage to 5V. We can do that by adjusting the potentiometer of the buck converter. You can notice here that I added a power switch to the input and connected the battery 12V to the 12V pin on the PCB which will be used only for monitoring the battery voltage.

5V input to the PCB using a buck converter

Remember that the main input to the PCB must be 5V.

So now we can insert the electronics components between the two plates. Again it’s a bit tight but still we can fit everything in. First goes the battery which I secured it using a tape, and on top of it goes the Arduino together with the PCB we made. Next we can connect the NRF24L01 transceiver or just the Bluetooth module depending on what type of communication we will use. I also inserted a LED for indicating when the battery needs to recharge, or in case the voltage drops below 11V volts. Lastly we need to connect all servos to the servo pins. While doing this make sure you write down to what pin number you connected each servo. After we connect all servos we can simply put the curved cover on the top plate and we are actually done with this project.

Arduino Hexapod Code

What’s left to do in this video is to take a look how the Arduino program works. As the code is a bit longer, for better understanding, I will post the source code of the program in sections with description for each section. And at the end of this article I will post the complete source code.

So for controlling the servos we will use the basic Servo library and for the Bluetooth communication we need to include the SoftwareSerial library as well. First we need to define all servo objects as well as some variables needed for the program below.

#include <Servo.h>
#include <SoftwareSerial.h>

#define trigPin 7
#define echoPin 6
#define ledB 10

SoftwareSerial Bluetooth(12, 9); // Arduino(RX, TX) - HC-05 Bluetooth (TX, RX)

// Create servo object
Servo s24;
Servo s23;
Servo s22;

In the setup section we need to initialize the Bluetooth communication, the pin modes for the ultrasonic sensor, the LEd and also define the pins to which the servos are connected.

void setup() {
  Serial.begin(38400);
  Bluetooth.begin(38400); // Default baud rate of the Bluetooth module
  Bluetooth.setTimeout(1);
  delay(20);
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  pinMode(ledB, OUTPUT);
  // Head
  s15.attach(36, 600, 2400);
  s14.attach(35, 600, 2400);
  s13.attach(34, 600, 2400); //grip
  // Tail
  s5.attach(26, 600, 2400); // Tail
  // Leg 4
  s10.attach(31, 600, 2400);
  s11.attach(32, 600, 2400);
  s12.attach(33, 600, 2400); //rot
  // Leg 5
  s7.attach(28, 600, 2400);
  s8.attach(29, 600, 2400);
  s9.attach(30, 600, 2400); //rot
  // Leg 6
  s1.attach(22, 600, 2400);
  s2.attach(23, 600, 2400);
  s3.attach(24, 600, 2400); //rot
  // Leg 1
  s18.attach(39, 600, 2400);
  s17.attach(38, 600, 2400);
  s16.attach(37, 600, 2400); //rot
  // Leg 2
  s21.attach(42, 600, 2400);
  s20.attach(41, 600, 2400);
  s19.attach(40, 600, 2400); //rot
  // Leg 3
  s24.attach(45, 600, 2400);
  s23.attach(44, 600, 2400);
  s22.attach(43, 600, 2400); //rot

Then using the write() functions we move the servos to their initial position. Here’s actually where we can calibrate our servos. Consider that you won’t be able to set each servo at the exact position when assembling the robot, but here we can do adjustments and find out our initial values and from there we can program the robot movement.

// == Move to initial position
  // Head
  s15.write(72);
  s14.write(50);
  s13.write(90); // Grip
  
  s5.write(65); // Tail
  
  // Leg 4
  s10.write(65);
  s11.write(35);
  s12.write(40);
  // Leg 5
  s7.write(80);
  s8.write(50);
  s9.write(25);
  // Leg 6
  s1.write(90);
  s2.write(45);
  s3.write(60);

  // Leg 1
  s18.write(60);
  s17.write(90);
  s16.write(100);
  // Leg 2
  s21.write(50);
  s20.write(85);
  s19.write(75);
  // Leg 3
  s24.write(50);
  s23.write(80);
  s22.write(80);

When it comes to programing a hexapod movement there are several ways that to be done, like using a forward or inverse kinematics. These methods include some serious mathematics where the position of each joint is calculated based on the inputs for the desired final position of the body. However, I decided to make it a bit less complicated because anyway the servos that I am using are not good enough for such a task. That’s because my servos are the cheap version of the MG996R servo. They don’t have the right torque and they don’t always position at the exact desired position.

So let’s take a look how I made the hexapod walk. I made a separate custom function for moving each leg. A leg cycle includes two phase, called swing and stance. In the swing phase the leg move from an initial position to a final position through air, while in the stance phase the leg move from the final position back to the initial position with the leg end effector touching the ground. In this way the body of the hexapod will move forward.

Arduino Hexapod Walking Swing and Stance phase

So I manually program the positions of each servos to achieve these movements using the main loop and some counters.

void moveLeg1() {
  // Swign phase - move leg though air - from initial to final position
  // Rise the leg
  if (i1L1 <= 10) {
    s18.write(60 - i1L1 * 2);
    s17.write(90 - i1L1 * 3);
    i1L1++;
  }
  // Rotate the leg
  if (i2L1 <= 30) {
    s16.write(100 - i2L1);
    i2L1++;

  }
  // Move back to touch the ground
  if (i2L1 > 20 & i3L1 <= 10) {
    s18.write(40 + i3L1 * 2);
    s17.write(60 + i3L1 * 3);
    i3L1++;
  }
  // Stance phase - move leg while touching the ground
  // Rotate back to initial position
  if (i2L1 >= 30) {
    s16.write(70 + i4L1);
    i4L1++;
    l1status = HIGH;
  }
  // Reset the counters for repeating the process
  if (i4L1 >= 30) {
    i1L1 = 0;
    i2L1 = 0;
    i3L1 = 0;
    i4L1 = 0;
    i5L1 = 0;
  }
  // Each iteration or step is executed in the main loop section where there is also a delay time for controlling the speed of movement
}

So first the two outer servos rise the leg and the third servo connected to the body starts to rotate in a particular direction. When third servo is 10 steps before it stops rotating, we start move the outer two servos to the back at the same position to touch the ground. This completes the swing phase, or the leg moved from its initial to the final position. Then we rotate back the third servo from the final to the initial position, and that completes the stance phase. After the leg executes one cycle, the counters are reset and the leg will repeat the cycle over and over again. Each iteration or step is executed in the main loop section where there is also a delay time which controls speed of servos. I made functions like this for all other legs, as well as some additional functions for moving the legs in opposite direction for achieving reverse, left and right movements. In similar way using counters for keeping track of the steps I programmed the rest of the functions, like moving the head, moving the tail, the mandibles and so on.

So for example if we want to move the robot forward, we need to call the six moveLeg() custom functions which will constantly repeat in the main loop.

// Move forward
  if (m == 2) {
    moveLeg1();
    moveLeg3();
    moveLeg5();
    if (l1status == HIGH) {
      moveLeg2();
      moveLeg4();
      moveLeg6();
    }
  }

You can notice that the 3 legs are offset, so when the leg number 1 3 and 5 are in swing phase the other three legs, 2, 4 and 6 are in stance phase. In case we want to move left we call the appropriate moveLeft() functions.

These commands are actually coming from the Bluetooth module or the custom-built Android application from our smartphone.

// Check for incoming data
  if (Bluetooth.available() > 0) {
    dataIn = Bluetooth.read();  // Read the data

Arduino Ant Robot Android App

Let’s take a look at the app now and see what kind of data it is actually sending to the Arduino. I made the app using the MIT App Inventor online application and here’s how it works.

So the graphics of the app are actually images that I made and placed as buttons. At the bottom we have a slider for controlling the speed of the robot and at the top we have the buttons for connecting with the Bluetooth module.

Arduino Ant Robot Hexapod Android App MIT App Inventor

Let’s take a look at the program or the blocks behind the app.

Android app blocks - how it works

So for example if we click the Forward button, the blocks in the “if” statement will be executed. That means we will send the number 2 to the Arduino and that will execute the move forward set of functions. At the same time we can notice that we change the image of the button to the other highlighted version of the same image. If we press again the same button, now the blocks in the “else” statement will be executed and that will send the number 0 to the Arduino which will reset all counters and move the robot its initial positions. Also we set back the initial image of that button. So I used the same principle for all other buttons.

Let’s just take a look at two more functions at the Arduino program and that’s the battery voltage monitor and ultrasonic sensor.

// Monitor the battery voltage
    int sensorValue = analogRead(A3);
    float voltage = sensorValue * (5.00 / 1023.00) * 2.9; // Convert the reading values from 5v to suitable 12V i
    Serial.println(voltage);
    // If voltage is below 11V turn on the LED
    if (voltage < 11) {
      digitalWrite(ledB, HIGH);
    }
    else {
      digitalWrite(ledB, LOW);
    }

So if the battery voltage is lower than 11 volts we will turn on the LED and if the ultrasonic sensor detects an object closer than 40 cm, the robot will prepare for an attack.

// Get the distance from the ultrasonic sensor
    if (getDistance() > 40) {
      att = 0;
    }
    if (getDistance() <= 40) {
      att = 1;
      dataIn = 99;
    }

In case the there is no longer object in front it will dismiss the attack and if the object is still present and closer to the head the robot will attack.

// If there is an object in front of the sensor prepare for attack
  if (att == 1) {
    prepareAttack();
    if (aStatus == HIGH) {
      while (a == 0) {
        delay(2000);
        a = 1;
      }
      if (getDistance() > 30) {
        att = 2;
        a = 0;
        aStatus = LOW;
        initialPosHead();
      }
      if (getDistance() < 30) {
        att = 3;
        a = 0;
        aStatus = LOW;
        initialPosHead();
      }
    }
  }
  // If there is no longer object in front, dismiss the attack
  if (att == 2) {
    dismissAttack();
    if (aStatus == HIGH) {
      dataIn = 0;
      att = 0;
    }
  }
  // If there is closer to the sensor attack
  if (att == 3) {
    attack();
    if (attStatus == HIGH) {
      while (aa == 0) {
        delay(2000);
        aa = 1;
      } attStatus = LOW;
    }
    if (aStatus == HIGH) {
      while (a == 0) {
        delay(2000);
        a = 1;
      }
      dataIn = 0;
      att = 0;
      initialPosHead();
    }
  }

So that’s pretty much everything for this video.

Here you can download the complete code for this Arduino Hexapod project:

Note that if you decide to build this project, you need to be prepared to face some challenges. The biggest problem for me was the poor performance of the servos that I used. I hope you enjoyed this video and learned something new. Feel free to ask any question in the comments section below and check my Arduino Projects Collection.

The post Arduino Ant Hexapod Robot appeared first on HowToMechatronics.



from HowToMechatronics https://ift.tt/2zSpf04

No comments:

Post a Comment