Compare commits

..

4 commits

4 changed files with 113 additions and 29 deletions

View file

@ -121,27 +121,21 @@ bool PixelBot::Connect()
void PixelBot::Listen() void PixelBot::Listen()
{ {
while(true) { while(true) {
if(!Tick()) { Serial.println("[PixelBot/Server] Entering main loop.");
Serial.println("[PixelBot/Server] Exiting main loop.");
break; // Make sure that there's something to read
} if(tcpClient.available() == 0) {
delay(10); // sleep for 10ms
continue;
}
String nextLine = tcpClient.readStringUntil('\n');
Serial.print(" > "); Serial.println(nextLine);
} }
} }
bool PixelBot::Tick()
{
// Make sure that there's something to read
if(tcpClient.available() == 0) {
delay(10);
return true;
}
String nextLine = tcpClient.readStringUntil('\n');
Serial.print(" > "); Serial.println(nextLine);
return true;
}
bool PixelBot::processCommand(String command) bool PixelBot::processCommand(String command)
{ {

View file

@ -1,5 +1,7 @@
#include "PixelBotController.h" #include "PixelBotController.h"
#include <Arduino.h>
PixelBotController::PixelBotController() PixelBotController::PixelBotController()
{ {
@ -10,3 +12,54 @@ PixelBotController::~PixelBotController()
} }
unsigned long PixelBotController::GetTicksTravelled()
{
return ticksTravelled;
}
void PixelBotController::MoveDistance(int direction, int ticks)
{
for(int i = 0; i < ticks; i++) {
MoveTick(direction);
delay(tickDelay);
}
}
void PixelBotController::MoveTick(int direction)
{
for(int i = 0; i < 8; i++)
{
switch(direction) {
case STOP:
return;
case FORWARD:
setOutputDir(i,7-i);
break;
case BACK:
setOutputDir(7-i,i);
break;
case LEFT:
setOutputDir(7-i,7-i);
break;
case RIGHT:
setOutputDir(i,i);
break;
}
delayMicroseconds(motorSpeed);
}
ticksTravelled++;
}
void PixelBotController::sendTickPart(int leftCode, int rightCode)
{
digitalWrite(lmotorPin1, bitRead(motorLookup[leftCode], 0));
digitalWrite(lmotorPin2, bitRead(motorLookup[leftCode], 1));
digitalWrite(lmotorPin3, bitRead(motorLookup[leftCode], 2));
digitalWrite(lmotorPin4, bitRead(motorLookup[leftCode], 3));
digitalWrite(rmotorPin1, bitRead(motorLookup[rightCode], 0));
digitalWrite(rmotorPin2, bitRead(motorLookup[rightCode], 1));
digitalWrite(rmotorPin3, bitRead(motorLookup[rightCode], 2));
digitalWrite(rmotorPin4, bitRead(motorLookup[rightCode], 3));
}

View file

@ -1,6 +1,5 @@
#pragma once #pragma once
/// <summary> /// <summary>
/// Connects to a PixelHub server, receives commands, and acts upon them. /// Connects to a PixelHub server, receives commands, and acts upon them.
/// </summmary> /// </summmary>
@ -10,13 +9,34 @@ public:
PixelBotController(); PixelBotController();
~PixelBotController(); ~PixelBotController();
static const int STOP = 0;
static const int FORWARDS = 1;
static const int BACKWARDS = 2;
static const int LEFT = 3;
static const int RIGHT = 4;
void MoveDistance(int direction, int ticks);
void MoveTick(int direction);
unsigned long GetTicksTravelled();
private: private:
int motorLookup[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001}; const int rmotorPin1 = 16;
const int rmotorPin2 = 5;
const int rmotorPin3 = 4;
const int rmotorPin4 = 0;
int motorSpeed = 1200; //variable to set stepper speed const int lmotorPin1 = 2;
int count = 0; // count of steps made const int lmotorPin2 = 14;
int countsperrev = 512; // number of steps per full revolution const int lmotorPin3 = 12;
const int lmotorPin4 = 13;
int motorLookup[8] = { 0b01000, 0b01100, 0b00100, 0b00110, 0b00010, 0b00011, 0b00001, 0b01001 };
int motorSpeed = 1200; // The speed at which the stepper motor accepts bits
unsigned long ticksTravelled = 0; // Number of steps travelled (including turns)
int ticksPerRevolution = 512; // Number of steps per full revolution of the wheels
int tickDelay = 1; // The delay in between ticks
void sendTickPart(int leftCode, int rightCode);
}; };

View file

@ -2,9 +2,26 @@
A combination of a C# server and an arduino program that allows the control of a Hull Pixelbot. A combination of a C# server and an arduino program that allows the control of a Hull Pixelbot.
## PixelHub Cilent protocol
The PixelHub client protocol is a (mostly) text-based protocol that the server uses to give the robot instructions. One instruction or message is passed per line (and line endings are '\n'). The message flow works something like this:
(todo: diagram here :D)
1. The server sends a command ot the robot
2. The robot sends back to say whether it will/can execute the command
3. The robot executes the command
4. The robot messages back to say it's finished executing a given command.
## Split-brain robots
If you have a split brain robot, the PixelHub client, while it doesn't support it yet, _can_ be adjusted to add support. Currently, all you need to do is swap out the `PixelHubController` .cpp file with one that can talk to your other microcontroller that actually controls the wheels.
## Credits ## Credits
- Debugging: - Debugging:
- Andrew C, Karen van Eck, Mike C - Andrew C, Karen van Eck, Mike C
- Soldering:
- Brian N
## Useful Links ## Useful Links
@ -14,7 +31,7 @@ A combination of a C# server and an arduino program that allows the control of a
### For Multicast networkinterface blog post ### For Multicast networkinterface blog post
- [NetworkInterface class](https://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterface(v=vs.110).aspx) - [NetworkInterface class](https://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterface(v=vs.110).aspx)
- [Sockoverflow answer](http://stackoverflow.com/questions/2192548/specifying-what-network-interface-an-udp-multicast-should-go-to-in-net) - [Stackoverflow answer](http://stackoverflow.com/questions/2192548/specifying-what-network-interface-an-udp-multicast-should-go-to-in-net)
- [IPv4InterfaceProperties](https://msdn.microsoft.com/en-us/library/system.net.networkinformation.ipv4interfaceproperties(v=vs.110).aspx) - [IPv4InterfaceProperties](https://msdn.microsoft.com/en-us/library/system.net.networkinformation.ipv4interfaceproperties(v=vs.110).aspx)
### Bits and Bobs ### Bits and Bobs