Complete ROS 4WD Robot Guide — From Ubuntu to Xbox Controller

Complete ROS 4WD Robot Guide
From Ubuntu Installation to Xbox Controller Teleoperation

This is a complete, end-to-end robotics guide. Follow this step by step and you will go from a blank Raspberry Pi to a fully drivable ROS robot controlled via keyboard and Xbox wireless controller.


ROADMAP — WHAT YOU WILL BUILD

  1. Install Ubuntu 20.04 on Raspberry Pi
  2. Install and verify ROS Noetic
  3. Wire and test motors (no ROS)
  4. Create a ROS motor driver node
  5. Drive robot using keyboard
  6. Drive robot wirelessly using Xbox controller

Final Result: A smooth, lag-free, ROS-controlled robot.


DAY 1 — Installing Ubuntu & ROS Noetic

1. Hardware Required

  • Raspberry Pi 3 / 4
  • 32GB microSD card
  • Stable 5V power supply

2. Flash Ubuntu

  • Install Raspberry Pi Imager
  • Select Ubuntu 20.04 LTS
  • Flash SD card and boot Pi

3. Enable SSH

sudo systemctl enable ssh
sudo systemctl start ssh
sudo systemctl status ssh

4. Update System

sudo apt update
sudo apt upgrade -y

5. Install ROS Noetic

sudo apt install software-properties-common
sudo add-apt-repository universe
sudo apt update
sudo apt install ros-noetic-desktop-full -y

6. ROS Environment Setup

echo "source /opt/ros/noetic/setup.bash" >> ~/.bashrc
source ~/.bashrc
Verify:
roscore

DAY 2 — Motor Wiring & Standalone Testing

Why This Step Matters

ROS cannot fix wiring mistakes. We validate motors before ROS.

GPIO Mapping (Example)

  • ENA → GPIO18
  • IN1 → GPIO17
  • IN2 → GPIO27
  • ENB → GPIO13
  • IN3 → GPIO22
  • IN4 → GPIO23

Motor Test Script


import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
ENA, IN1, IN2 = 18, 17, 27
ENB, IN3, IN4 = 13, 22, 23

GPIO.setup([ENA, IN1, IN2, ENB, IN3, IN4], GPIO.OUT)

GPIO.output(IN1, GPIO.HIGH)
GPIO.output(IN2, GPIO.LOW)
GPIO.output(IN3, GPIO.HIGH)
GPIO.output(IN4, GPIO.LOW)

time.sleep(2)
GPIO.cleanup()
Run:
python3 motor_test.py
Expected: Robot moves forward.

DAY 3 — ROS Motor Driver Node

Concept

ROS sends velocity commands on /cmd_vel. Our node converts them into GPIO signals.

Motor Driver Node


#!/usr/bin/env python3
import rospy
from geometry_msgs.msg import Twist
import RPi.GPIO as GPIO

def callback(msg):
    linear = msg.linear.x
    angular = msg.angular.z
    # add motion logic here

rospy.init_node("motor_driver")
rospy.Subscriber("/cmd_vel", Twist, callback)
rospy.spin()

Test Using ROS


rostopic pub /cmd_vel geometry_msgs/Twist "linear:
  x: 0.3
angular:
  z: 0.0"

DAY 4 — Keyboard Control

Install Teleop Package

sudo apt install ros-noetic-teleop-twist-keyboard

Run System

roscore
rosrun my_4wd_robot motor_driver_node.py
rosrun teleop_twist_keyboard teleop_twist_keyboard.py

Controls

  • W → Forward
  • S → Backward
  • A → Left
  • D → Right
  • X → Stop

DAY 5 — Xbox Wireless Controller (Bluetooth)

1. Pair Controller

bluetoothctl
power on
agent on
default-agent
scan on
Put controller into pairing mode.
pair XX:XX:XX:XX:XX:XX
trust XX:XX:XX:XX:XX:XX
connect XX:XX:XX:XX:XX:XX
quit

2. Verify Controller

ls /dev/input/js0
jstest /dev/input/js0

3. Fix Headless pygame Issue


import os
os.environ["SDL_VIDEODRIVER"] = "dummy"

4. Run Xbox Teleop

rosrun xbox_teleop xbox_teleop_node.py
Expected:
[INFO] XBOX TELEOP ACTIVE

FINAL RESULT

  • Ubuntu + ROS stable
  • Motors correctly wired
  • ROS motor driver working
  • Keyboard control verified
  • Xbox wireless teleoperation smooth

Your robot now drives like butter.


NEXT UPGRADES

  • Deadman safety button
  • Speed modes
  • Launch files
  • Auto-start on boot

This guide documents a real debugging journey — not a copy-paste tutorial.

Comments