Smasher Bot

 

20130812_203319Smasher bot is a 150lb robotic platform for development. I plan on making it semi-autonomous able to do directional navigation and object detection and avoidance. As I make code and update the robot I will be posting images here.

This is the code for the Arduino with internet shield.


/*
Auther : Odis Harkins
Site : http://odisharkins.com
Date : 08/16/2013
Comment : Have fun!
*/
#include <SPI.h> // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
#include <Servo.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 177);
unsigned int localPort = 8888; // local port to listen on

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
// create servo object to control a servo
Servo xservo;
Servo yservo;
void setup() {
// start the Ethernet and UDP:
Ethernet.begin(mac,ip);
Udp.begin(localPort);

xservo.attach(9); // attaches the servo on pin 9 to the servo object
yservo.attach(6); // attaches the servo on pin 6 to the servo object
Serial.begin(9600);
}

void loop() {
// if there’s data available, read a packet
int packetSize = Udp.parsePacket();
if(packetSize)
{
// read the packet into packetBufffer
Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
//Serial.println(packetBuffer);
String myString(packetBuffer);
//get value of : seperated string and position.
String xval = getValue(myString, ‘:’, 0);
String yval = getValue(myString, ‘:’, 1);

Serial.println(“Y:” + yval);
Serial.print(“X:” + xval);

//Convert String to int
int xvalue = stringToNumber(xval);
int yvalue = stringToNumber(yval);
//map the values to the servo max output 0-180 90 is full stop.
//Servo ouptuts
xservo.write(map(xvalue, 320, 0, 0, 180));
yservo.write(map(yvalue, 0, 320, 0, 180));

}
//delay(10); // Add in for serial testing.
}
//This Chunk of code takes a string and seperates it bassed on a given charator and returns
//The item between the seperating charator
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = {
0, -1 };
int maxIndex = data.length()-1;
for(int i=0; i<=maxIndex && found<=index; i++){
if(data.charAt(i)==separator || i==maxIndex){
found++;
strIndex[0] = strIndex[1]+1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found>index ? data.substring(strIndex[0], strIndex[1]) : “”;
}
//This converts a string of numbers to an Int
int stringToNumber(String thisString) {
int i, value, length;
length = thisString.length();
char character[(length+1)];
for(i=0; i<length; i++) {
character[i] = thisString.charAt(i);
}
character[i]=0;
value = atoi(character);
return value;
}

The Code for Processing


/*
Auther : Odis Harkins
Site : http://harkinstech.com
Date : 08/16/2013
Comment : Have fun!
*/
import hypermedia.net.*;

UDP udp; // define the UDP object

void setup() {
size(320, 320);
udp = new UDP( this, 6000 ); // create a new datagram connection on port 6000
}
void draw()
{
size(320, 320);
background(204);
line(mouseX, 130, mouseX, 190);
line(130, mouseY, 190, mouseY);
text(“Y: “+ mouseY, 10, 20);
text(“X: “+ mouseX, 10, 40);
// If the mouse is clicked and held down send the values! if not send a stop command.
if (mousePressed == true) {
sendstring(mouseX,mouseY);
}
else
{
sendstring(160,160);
}

}
//This sends the values through the UDP port.
void sendstring(int mouseXval, int mouseYval) {
String ip = “192.168.1.177”; // the remote IP address
int port = 8888; // the destination port
udp.send(mouseXval+ “:” +mouseYval, ip, port ); // the message to send
}

 

Leave a comment