Browse Source

Implement PixelBotController for single-brain robots. Hooray! Now we just need to test it....

master
Starbeamrainbowlabs 4 years ago
parent
commit
a5e91d2605
3 changed files with 95 additions and 28 deletions
  1. +12
    -18
      PixelHub-Client/PixelBot.cpp
  2. +54
    -1
      PixelHub-Client/PixelBotController.cpp
  3. +29
    -9
      PixelHub-Client/PixelBotController.h

+ 12
- 18
PixelHub-Client/PixelBot.cpp View File

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


+ 54
- 1
PixelHub-Client/PixelBotController.cpp View File

@ -1,8 +1,10 @@
#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));
}

+ 29
- 9
PixelHub-Client/PixelBotController.h View File

@ -1,6 +1,5 @@
#pragma once
/// <summary>
/// Connects to a PixelHub server, receives commands, and acts upon them.
/// </summmary>
@ -9,14 +8,35 @@ class PixelBotController
public:
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:
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
int count = 0; // count of steps made
int countsperrev = 512; // number of steps per full revolution
};
const int lmotorPin1 = 2;
const int lmotorPin2 = 14;
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);
};