Megazilla Simulation

by onlyland in Circuits > Robots

29 Views, 1 Favorites, 0 Comments

Megazilla Simulation

스크린샷 2024-12-14 133845.png

I watched SpaceX's Megazilla video. The video was about a rocket being launched, completing its flight, and then being retrieved by Megazilla's chopsticks. I decided to try implementing this technology. Since I couldn't make a rocket, I thought of hanging a paper cup on a drone to act as a rocket, and making Megazilla out of Legos. I thought of controlling Megazilla by watching a video on my laptop, imitating the idea of ​​multiple engineers controlling the model Megazilla in SpaceX's control center while looking at a screen.

Supplies

20250111_160757.jpg
20250113_150934.jpg

- Lego EV3 set (materials that middle school students can easily make a robot with)

- Toy drone (role as a rocket)

- Paper cup ((role as a body of rocket)

- MicroSD card (to boot EV3 with ev3dev2)

- Wifi dongle

Sketch

plan.png

Implementing SpaceX's Megazilla

- Create a window to control Megazilla using python tkinter, openCV, and paho-mqtt.

- Create Megazilla with EV3 set.

- Hang a paper cup on a drone to act as a rocket.

- When the drone rocket approaches Megazilla, activate Megazilla to catch the rocket.

Making Megazilla

20241130_140118.jpg
20241214_134355.jpg
screenshot.png
20250113_153301.jpg
20250111_143500 (1).jpg

- Make a simple Megazilla with the Lego EV3 set.

- Download the ev3dev boot image to run EV3 with Python and flash the microSD card.

- Install mosquitto, paho-mqtt to run mqtt on EV3 and python-ev3dev2 to run the robot with Python.

ev3dev Home


Code to run on LEGO EV3

#!/usr/bin/env python3
import paho.mqtt.client as mqtt
from ev3dev2.motor import LargeMotor, OUTPUT_B, SpeedPercent,MediumMotor
import json
# MQTT 브로커 설정
broker_address = "broker.emqx.io" #"192.168.0.4" #"broker.emqx.io" # 브로커 IP 주소
topic = "megazilla"

# EV3 장치 초기화l
motor = MediumMotor(OUTPUT_B)

# 메시지를 받을 때 호출되는 함수
def on_message(client, userdata, message):
try:
msg = message.payload.decode()
json_point = json.loads(msg)

print("Received message: {}".format(msg))

if "speed" in json_point :
print("Activating motor")
motor.on_for_degrees(SpeedPercent(json_point["speed"]), (json_point["amount"])) # speed만큼 속도 정하기(음수값을 넣으면 방향 바뀜뀜), amount만큼 각도값 주기기
except Exception as e:
print("Error handling message: {}".format(e))
# MQTT 클라이언트 설정
client = mqtt.Client("EV3_Subscriber_2")
client.on_message = on_message

try:
# 브로커 연결
print("Connecting to broker: {}".format(broker_address))
client.connect(broker_address, 1883, 60)
except Exception as e:
print("Error connecting to broker: {}".format(e))
exit(1)

# 주제 구독
try:
client.subscribe(topic)
print("Subscribed to topic: {}".format(topic))
except Exception as e:
print("Error subscribing to topic: {}".format(e))
exit(1)

# 메시지 수신 대기
try:
client.loop_forever()
except KeyboardInterrupt:
print("Disconnected from broker.")
except Exception as e:
print("Error in MQTT loop: {}".format(e))

Making a Drone Rocket

20250111_151027.jpg
20250111_143832 (1).jpg

At first, I tried to make a drone rocket body with 3D printing (photo attached). However, the rocket could not fly because of its weight. I cut the 3D model with a saw to reduce the weight, but the rocket was still too heavy to fly. So I decided to make the rocket body in the shape of two paper cups stacked on top of each other.

Downloads

Programing

3.png

I created a window panel to control Megazilla.

I sent a message to mqtt to control the speed, direction, and rotation angle so that I could catch Megazilla while watching the screen transmitted from Iriun Webcam in Windows.

from tkinter import *
import requests
import paho.mqtt.client as mqtt
import threading
from bs4 import BeautifulSoup
import cv2
import json
tk = Tk()

subscribe_topic = "laptop"

publish_topic = "megazilla"

broker_adress = "broker.emqx.io"

tk.geometry("1920x1080")

client = mqtt.Client()
client.connect(broker_adress, 1883, 60)

mqtt_thread = threading.Thread(target = client.loop_start)
mqtt_thread.daemon = True
mqtt_thread.start()

cap = None

webcam_label = Label(tk, width = 1000, height = 700)
webcam_label.pack()

def mqtt_on_connect(client, userdata, flags, rc):
print("연결되었습니다." + str(rc))
client.subscribe(subscribe_topic)

def publish_mqtt_message():
Message_1 = int(input_1.get())
Message_2 = int(input_2.get())
Message_3 = int(input_3.get())
real_Message = {"speed" : Message_1, "direct" : Message_2, "amount" : Message_3}
json_data = json.dumps(real_Message)
client.publish(publish_topic, json_data)
record.insert(END, json_data)
record.see(END)

def subscribe_mqtt_message(client, userdata, msg):
Message_4 = msg.payload.decode()
print(f"수신:{Message_4}.")
update_gui_text(f"수신메시지: {Message_4}")

def update_gui_text(Message_4):
record2.insert(END, f"{Message_4}\n")
record2.see(END)

#카메라 피드 표시 함수
def show_camera():
global cap, bg

if cap in None:
cap = cv2.VideaCapture(0)#카메라 연결

ret, frame = cap.read()
if ret:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # BGR(OpenCV 형식)을 RGB로 변환
img = Image.fromarray(frame) #이미지를 PIL 형식으로 변환
imgtk = ImageTk.PhotoImage(image = img) #Tkinter에서 사용할 수 있는 형식으로 변환
bg.create_image(0, 0, anchor = "nw", image = imgtk) # Canvas에 이미지 추가
bg.image = imgtk #이미지가 사라지지 않도록 참조 저장

bg.after(10, show_camera) # 반복 호출

def open_camera():
show_camera() # 카메라 시작

def update_webcam():
ret, frame = cap.read()
if ret:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame = cv2.resize(frame,(780,438))
img_bytes = cv2.imencode('.ppm',frame)[1].tobytes()
img = PhotoImage(data = img_bytes)

webcam_label.config(image = img)
webcam_label.image = img

webcam_label.after(20, update_webcam)

cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("연결되지 않았습니다")

update_webcam()

client = mqtt.Client()
client.on_connect = mqtt_on_connect
client.on_message = subscribe_mqtt_message
client.connect(broker_adress, 1883, 60)

mqtt_thread = threading.Thread(target = client.loop_start)
mqtt_thread.daemon = True
mqtt_thread.start()

btn_1 = Button(tk)
btn_1.configure(text = "Publish")
btn_1.config(width = 100, height = 2)
btn_1.place(x = 650, y = 800)
btn_1.config(command = publish_mqtt_message)

record = Text(tk)
record.config(width = 50, height = 20)
record.place(x = 20, y = 650)

label_4 = Label(tk)
label_4.configure(text = "Subscribe", font = ("맑은 고딕", 15))
label_4.place(x = 20, y = 170)

label_1 = Label(tk)
label_1.configure(text = "Speed", font = ("맑은 고딕", 15))
label_1.place(x = 550, y = 640)

input_1 = Entry(tk)
input_1.place(x = 650, y = 650)

label_2 = Label(tk)
label_2.configure(text = "Direction", font = ("맑은 고딕", 15))
label_2.place(x = 850, y = 640 )

input_2 = Entry(tk)
input_2.place(x = 950, y = 650)

label_3 = Label(tk)
label_3.configure(text = "Amount", font = ("맑은 고딕", 15))
label_3.place(x = 1150, y = 640 )

input_3 = Entry(tk)
input_3.place(x = 1250, y = 650)

record2 = Text(tk)
record2.config(width = 50, height = 20)
record2.place(x = 20, y = 200)

label_5 = Label(tk)
label_5.configure(text = "OutGoing", font = ("맑은 고딕", 15))
label_5.place(x = 20, y = 615)


tk.mainloop()

cap.release()
cv2.destroyAllWindows()


Megazilla Stimulation

메가질라따라하기

After completing the above process, I simulated the robot I made catching a safely returned rocket like SpaceX's Megazilla. It wasn't perfect, but I was able to successfully catch the drone rocket. This project was very fun for me.