Catapult Robot Tutorial

Description:

A catapult is a ballistic device used to launch a projectile a great distance without the aid of gunpowder or other propellants –particularly various types of ancient and medieval siege engines… We will make a catapult with LEGO building blocks. Equipped withservos and gears, the car has LEGO tower used to carry projectiles.

As the servo rotates to a proper angle then push the long arm backward a projectile will be launched.

Install APP

⚠️Special note: If you have downloaded and installed the APP, this step is skipped; If not, please refer to the installation instructions of link: Project 18:Read WiFi APP Button String .

How to build up a catapult robot

Step 1

Required components

3


Install

3_1


Complete

3_2


Step 2

Required components

3_3


Install

3_4


Complete

3_5


Step 3

Required components

3_6


Install

3_7


Complete

3_8


Step 4

Required components

3_9


Install

3_10


Complete

3_11


Step 5

Required components

3_12


Install

3_13


Complete

3_14


Step 6

Required components

3_15


Install

3_16


Complete

3_17


Step 7

Required components

3_18


Install

3_19


Complete

3_20


Step 8

Required components

3_21


Install

3_22


Complete

3_23


Step 9

Required components

4


Install

4_1


Complete

4_2


Step 10

Required components

4_3


Install

4_4


Complete

4_5


Step 11

Required components

4_6


Install

4_7


Complete

4_8


Step 12

Required components

4_9


Install

4_10


Complete

4_11


Step 13

Required components

4_12


Install

4_13


Complete

4_14


Step 14

Required components

4_15


Install

4_16


Complete

4_17


Step 15

Required components

4_18


Install

4_19


Complete

4_20


Step 16

Required components

4_21


Install

4_22


Complete

4_23


Step 17

Required components

4_24


Set the angle of the servo to 0 degree

Wire servo up Servo

PCB Board

Brown

G

Red

5V

Orange

S2(GPIO23)

Method 1:Arduino code

Upload the code of the servo to the main board of the Beetlebot car, as shown below

#include <Arduino.h>
#include <ESP32Servo.h>

Servo myservo;  // create servo object to control a servo

int servoPin = 23; // Servo motor pin

void setup() {
  myservo.setPeriodHertz(50);           // standard 50 hz servo
  myservo.attach(servoPin, 500, 2500);  // attaches the servo on servoPin to the servo object

  myservo.write(0);  // the angle of the steering gear is 0°.
  delay(1000);
  myservo.write(90);  // the angle of the steering gear is 90°.
  delay(1000);
  myservo.write(0);  // the angle of the steering gear is 0°.
  delay(1000);
}
void loop() {

}

How to get it ?

In folder …\Codes\Catapult_Robot_Code\270° Servo Angle initialization , open file 270_Servo_Angle_initialization.ino , or copy and paste the above test code into the Arduino IDE.

Img

Method 2:KidsBlock(Scratch) code

You can also initialize the angle of the servo through the following code

How to get it?

Open file 270°_Servo_Angle_initialization.sb3 with KidsBlock IDE, in folder …\Codes\Catapult_Robot_Code\270° Servo Angle initialization.

Img


Install

4_25


Complete

4_26


Step 18

Required components

Img


Install

4_28


Complete

4_29


Wire up

Interface the servo

1ce70f3a2988e646798cadfc6fc8995

Test Code for Arduino

⚠️ATTENTION: Before uploading code, please replace the WiFi name(REPLACE_WITH_YOUR_SSID) in the code and the passwords(REPLACE_WITH_YOUR_PASSWORD) into yours.

#include <WiFi.h>
#include <ESPmDNS.h>
#include <WiFiClient.h>

#include <ESP32Servo.h>
//motor
#define left_ctrl  33  //define direction control pins of the left motor as gpio33
#define left_pwm  26   //define PWM control pins of the left motor as gpio26.
#define right_ctrl  32 //define direction control pins of the right motor as gpio32.
#define right_pwm  25  //define PWM control pins of the right motor as gpio25

/*REPLACE WITH YOUR NETWORK CREDENTIALS(Put your SSID & Password)*/
const char* ssid = "REPLACE_WITH_YOUR_SSID"; //Enter SSID here
const char* password = "REPLACE_WITH_YOUR_PASSWORD"; //Enter Password here
// TCP server at port 80 will respond to HTTP requests
WiFiServer server(80);

//servo
const int servoPin = 23;//set the pin of the servo to gpio23.
Servo myservo;  // create servo object to control a servo

void setup(void)
{
    Serial.begin(115200);
    pinMode(left_ctrl,OUTPUT); //set control pins of the left motor to OUTPUT
    ledcAttach(left_pwm, 1200, 8); //Set the frequency of left_pwm pin to 1200, PWM resolution to 8 that duty cycle is 256.
    pinMode(right_ctrl,OUTPUT);//set direction control pins of the right motor to OUTPUT..
    ledcAttach(right_pwm, 1200, 8); //Set the frequency of right_pwm pin to 1200, PWM resolution to 8 that duty cycle is 256.
  
    myservo.setPeriodHertz(50);           // standard 50 hz servo
    myservo.attach(servoPin, 500, 2500);  // attaches the servo on servoPin to the servo object.
    myservo.write(0);  // the initial angle of the servo is set to 0° .
    delay(300);

    // Connect to WiFi network
    WiFi.begin(ssid, password);
    Serial.println("");

    // Wait for connection
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println("");
    Serial.print("Connected to ");
    Serial.println(ssid);
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());

    // Set up mDNS responder:
    // - first argument is the domain name, in this example
    //   the fully-qualified domain name is "esp32.local"
    // - second argument is the IP address to advertise
    //   we send our IP address on the WiFi network
    if (!MDNS.begin("esp32")) {
        Serial.println("Error setting up MDNS responder!");
        while(1) {
            delay(1000);
        }
    }
    Serial.println("mDNS responder started");

    // Start TCP (HTTP) server
    server.begin();
    Serial.println("TCP server started");

    // Add service to MDNS-SD
    MDNS.addService("http", "tcp", 80);
}

void loop(void)
{
    // Check if a client has connected
    WiFiClient client = server.available();
    if (!client) {
        return;
    }
    //Serial.println("");
    //Serial.println("New client");

    // Wait for data from client to become available
    while(client.connected() && !client.available()){
        delay(1);
    }

    // Read the first line of HTTP request
    String req = client.readStringUntil('\r');
    //Serial.println(req);
    // First line of HTTP request looks like "GET /path HTTP/1.1"
    // Retrieve the "/path" part by finding the spaces
    int addr_start = req.indexOf(' ');
    int addr_end = req.indexOf(' ', addr_start + 1);
    if (addr_start == -1 || addr_end == -1) {
        //Serial.print("Invalid request: ");
        //Serial.println(req);
        return;
    }
    req = req.substring(addr_start + 1, addr_end);
    //Serial.print("Request: ");
    Serial.println(req);
    String s;
    if (req == "/")
    {
        IPAddress ip = WiFi.localIP();
        String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
        s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>Hello from ESP32 at ";
        s += ipStr;
        s += "</html>\r\n\r\n";
        Serial.println("Sending 200");
        client.println(WiFi.localIP());
    }
    else if(req == "/btn/F")
    {
      digitalWrite(left_ctrl,LOW); //set direction control pins of the left motor to LOW.
      ledcWrite(left_pwm, 150); //the left motor outputs PWM 150
      digitalWrite(right_ctrl,LOW); //set control pins of the right motor to LOW.
      ledcWrite(right_pwm, 150); //the right motor outputs PWM 150
    }
    else if(req == "/btn/B")
    {
      digitalWrite(left_ctrl, HIGH);
      ledcWrite(left_pwm, 150);
      digitalWrite(right_ctrl, HIGH);
      ledcWrite(right_pwm, 150);
    }
    else if(req == "/btn/L")
    {
      digitalWrite(left_ctrl,HIGH); 
      ledcWrite(left_pwm, 150); 
      digitalWrite(right_ctrl,LOW); 
      ledcWrite(right_pwm, 150); 
    }
    else if(req == "/btn/R")
    {
      digitalWrite(left_ctrl,LOW); 
      ledcWrite(left_pwm, 150); 
      digitalWrite(right_ctrl,HIGH); 
      ledcWrite(right_pwm, 150); 
    }
    else if(req == "/btn/S")
    {
      digitalWrite(left_ctrl,LOW);
      ledcWrite(left_pwm, 0);
      digitalWrite(right_ctrl,LOW);
      ledcWrite(right_pwm, 0); 
    }
    else if(req == "/btn/p")
    {
      Serial.write('p');
      myservo.write(55);
      delay(200);
    }
    else if(req == "/btn/q")
    {
      Serial.write('q');
      client.println("q");
    }
    else if(req == "/btn/x")
    {
      myservo.write(0);
      delay(200); 
    }
}

Test Code for Kidsblock

⚠️ATTENTION: Before uploading code, please replace the WiFi name(REPLACE_WITH_YOUR_SSID) in the code and the passwords(REPLACE_WITH_YOUR_PASSWORD) into yours.

Img

Build up a few target objects with building blocks(object A, B, C, D, E) and keep them in a certain distance away the catapult and connect Wifi.

Click to make the car to face the object A, hold down the button to drive the catapult to launch a building block.

Then release the button to make the long arm return to the original state. Next, let’s check if the object A is hit by the launched block

You can repeat above steps to hit the object B, C and D