Implement PixelBotController for single-brain robots. Hooray! Now we just need to test it....
This commit is contained in:
parent
66195d2af3
commit
a5e91d2605
3 changed files with 95 additions and 28 deletions
|
@ -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)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
|
@ -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));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -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);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Reference in a new issue