Robotics Kit 2 - Software - Controller

Start 

Controller in action

Controller Framework 

We'll be using the UDP messaging system from the Weather Station Series to send the joystick positions to the robot. The robot will then translate those into a direction and power for the motors. For a more in depth explanation head over to this section.

Adding the WiFi Module 

Take a free WiFi module and cable. Roll up a piece of tape to create double sided tape. Place one side of the tape on the bottom plate of the controller. Take the WiFi module and place it on top of the tape. Take a cable and connect one end to the WiFi module socket and the other to D8.

Upload

Upload the following code.

Download file Copy to clipboard
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
  ________    _           __    __
  /_  __/ /_  (_)___ ___  / /_  / /__
  / / / __ \/ / __ `__ \/ __ \/ / _ \
  / / / / / / / / / / / / /_/ / /  __/
  /_/ /_/ /_/_/_/ /_/ /_/_.___/_/\___/
    ____        __          __  _              __ __ _ __     ___
   / __ \____  / /_  ____  / /_(_)_________   / //_/(_) /_   |__ \
  / /_/ / __ \/ __ \/ __ \/ __/ / ___/ ___/  / ,<  / / __/   __/ /
  / _, _/ /_/ / /_/ / /_/ / /_/ / /__(__  )  / /| |/ / /_    / __/
  /_/ |_|\____/_.___/\____/\__/_/\___/____/  /_/ |_/_/\__/   /____/
*/

//All the libraries used
#include <WiFiEsp.h>
#include <WiFiEspUdp.h>
#include "SoftwareSerial.h"

//Create Serial communication Object
SoftwareSerial Serial1(8, 9); // RX, TX

WiFiEspUDP Udp;

IPAddress apIP = IPAddress(192, 168, 111, 111);
IPAddress stationIP = IPAddress(192, 168, 111, 112);
IPAddress broadcastIP = IPAddress(0, 0, 0, 0);

//Wifi settings
char ssid[] = "My WiFi Robot";    // the name of your access point
char pass[] = "password";        // the password for your access point
int status = WL_IDLE_STATUS;     // the Wifi radio's status

//Ports to be used
unsigned int listeningPort = 10003;  // local port to listen on
unsigned int sendingPort = 10002;  // local port to sent on

//Message Variables
#define arrayLength 6
char data[arrayLength + 1];
String dataAsString;

// Vaiables
int leftData;
int rightData;

void setup()
{
  // Start Serial Communication with computer and WiFi module
  Serial.begin(9600);
  // initialize serial for ESP module
  Serial1.begin(9600);
  // initialize ESP module
  WiFi.init(&Serial1);
  WiFi.config(stationIP);

  // Connect to robot
  status = WiFi.begin(ssid, pass);

  // Set up UDP listener on port
  Udp.begin(listeningPort);

  // Print out which port
  Serial.print(F("Listening on port "));
  Serial.println(listeningPort);
}

void loop() {
    //Read the Joystick values
  leftData = analogRead(A0);
  rightData = analogRead(A2);

    //Concatanate all the vlaues into one string
  dataAsString = String(leftData) + String(rightData);

    //Convert String to char array
  dataAsString.toCharArray(data, arrayLength + 1);

  //Print out all values
  Serial.println(data);

  //Begin UDP sending
  Udp.beginPacket(apIP, sendingPort);
  //Send the data
  Udp.write(data);
  //Send ending packets
  Udp.endPacket();

  //Battery saving delay
  delay(10);
}

Observe

Make sure RS1 is powered and open up the Serial Monitor. You should see the Controller attempting to connect to the network created by RS1. After connected it will start sending the joystick position. Move the left joystick to control the left wheel and the right joystick to control the right wheel.

Modify

Change the robot's motor speed to go faster or slower and take a look at the following section for upgrades!