Skip to content

Commit

Permalink
Major rework to bring the new Adafruit motorshield in.
Browse files Browse the repository at this point in the history
  • Loading branch information
euphy committed Apr 17, 2014
1 parent 3c99941 commit 1aa4a20
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 256 deletions.
9 changes: 4 additions & 5 deletions comms.ino
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ String comms_readCommand()
}
else
{
Serial.print(F("I got "));
Serial.print(F("Rcd: "));
Serial.println(inString);
Serial.print(F("Checksum not matched!:"));
Serial.print(F("Bad Sum:"));
Serial.println(calcCrc);
commandConfirmed = false;
}
Expand Down Expand Up @@ -137,7 +137,7 @@ void comms_parseAndExecuteCommand(String &in)
}
else
{
Serial.print(F("Command ("));
Serial.print(F("Comm ("));
Serial.print(in);
Serial.println(F(") not parsed."));
}
Expand Down Expand Up @@ -238,9 +238,8 @@ void comms_requestResend()
}
void comms_unrecognisedCommand(String &com)
{
Serial.print(F("Sorry, "));
Serial.print(com);
Serial.println(F(" isn't a command I recognise."));
Serial.println(F(" not recognised."));
}


34 changes: 30 additions & 4 deletions configuration.ino
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,40 @@ Comment out the blocks of code you don't need.
// =================================================================
// 1. Adafruit motorshield

#include <AFMotor.h>
const int stepType = INTERLEAVE;
// Using Adafruit Motorshield V2? Comment out this one line underneath.
//#include <AFMotor.h>

// Using Adafruit Motorshield V1? Comment out the three INCLUDE lines below.
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

#ifdef ADAFRUIT_MOTORSHIELD_V1
const int stepType = INTERLEAVE;
AF_Stepper afMotorA(motorStepsPerRev, 1);
AF_Stepper afMotorB(motorStepsPerRev, 2);

void forwarda() { afMotorA.onestep(FORWARD, stepType); }
void backwarda() { afMotorA.onestep(BACKWARD, stepType); }
AccelStepper motorA(forwarda, backwarda);

void forwardb() { afMotorB.onestep(FORWARD, stepType); }
void backwardb() { afMotorB.onestep(BACKWARD, stepType); }
#endif

#ifdef ADAFRUIT_MOTORSHIELD_V2
const int stepType = INTERLEAVE;

Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_StepperMotor *afMotorA = AFMS.getStepper(motorStepsPerRev, 1);
Adafruit_StepperMotor *afMotorB = AFMS.getStepper(motorStepsPerRev, 2);

void forwarda() { afMotorA->onestep(FORWARD, stepType); }
void backwarda() { afMotorA->onestep(BACKWARD, stepType); }
void forwardb() { afMotorB->onestep(FORWARD, stepType); }
void backwardb() { afMotorB->onestep(BACKWARD, stepType); }
#endif


AccelStepper motorA(forwarda, backwarda);
AccelStepper motorB(forwardb, backwardb);

void configuration_motorSetup()
Expand All @@ -45,7 +67,11 @@ void configuration_setup()
defaultMmPerRev = 95;
defaultStepsPerRev = 400;
defaultStepMultiplier = 1;
#ifdef ADAFRUIT_MOTORSHIELD_V2
AFMS.begin(); // create with the default frequency 1.6KHz
#endif
delay(500);

}
// end of Adafruit motorshield definition
// =================================================================
Expand Down
61 changes: 1 addition & 60 deletions eeprom.ino
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,6 @@ void eeprom_loadPenLiftRange()
Serial.println(upPosition);
}

void eeprom_loadMachineName()
{
String name = "";
for (int i = 0; i < 8; i++)
{
char b = EEPROM.read(EEPROM_MACHINE_NAME+i);
name = name + b;
}

if (name[0] == 0)
name = DEFAULT_MACHINE_NAME;
maxLength = 0;
machineName = name;
Serial.print(F("Loaded machine name:"));
Serial.println(machineName);
}

void eeprom_loadStepMultiplier()
{
EEPROM_readAnything(EEPROM_MACHINE_STEP_MULTIPLIER, stepMultiplier);
Expand Down Expand Up @@ -145,7 +128,6 @@ void eeprom_loadMachineSpecFromEeprom()
eeprom_loadMachineSize();
eeprom_loadSpoolSpec();
eeprom_loadStepMultiplier();
eeprom_loadMachineName();
eeprom_loadPenLiftRange();
eeprom_loadSpeed();

Expand Down Expand Up @@ -177,45 +159,4 @@ void eeprom_loadMachineSpecFromEeprom()

maxLength = 0;
}
//
////This function will write a 2 byte integer to the eeprom at the specified address and address + 1
//void eeprom_EEPROMWriteInt(int p_address, int p_value)
//{
// Serial.print(F("Writing Int "));
// Serial.print(p_value);
// Serial.print(F(" to address "));
// Serial.println(p_address);
//
// byte lowByte = ((p_value >> 0) & 0xFF);
// byte highByte = ((p_value >> 8) & 0xFF);
// EEPROM.write(p_address, lowByte);
// EEPROM.write(p_address + 1, highByte);
//}
//
////This function will read a 2 byte integer from the eeprom at the specified address and address + 1
//unsigned int eeprom_EEPROMReadInt(int p_address)
//{
// byte lowByte = EEPROM.read(p_address);
// byte highByte = EEPROM.read(p_address + 1);
// return ((lowByte << 0) & 0xFF) + ((highByte << 8) & 0xFF00);
//}
//
//
//// from http://playground.arduino.cc/Code/EEPROMWriteAnything
//template <class T> int eeprom_writeAnything(int ee, const T& value)
//{
// const byte* p = (const byte*)(const void*)&value;
// int i;
// for (i = 0; i < sizeof(value); i++)
// EEPROM.write(ee++, *p++);
// return i;
//}
//
//template <class T> int eeprom_readAnything(int ee, T& value)
//{
// byte* p = (byte*)(void*)&value;
// int i;
// for (i = 0; i < sizeof(value); i++)
// *p++ = EEPROM.read(ee++);
// return i;
//}

53 changes: 14 additions & 39 deletions exec.ino
Original file line number Diff line number Diff line change
Expand Up @@ -23,40 +23,44 @@ boolean exec_executeBasicCommand(String &com)
boolean executed = true;
if (com.startsWith(CMD_CHANGELENGTH))
exec_changeLength();
#ifdef VECTOR_LINES
else if (com.startsWith(CMD_CHANGELENGTHDIRECT))
exec_changeLengthDirect();
#endif
else if (com.startsWith(CMD_CHANGEPENWIDTH))
exec_changePenWidth();
else if (com.startsWith(CMD_SETMOTORSPEED))
exec_setMotorSpeed();
else if (com.startsWith(CMD_SETMOTORACCEL))
exec_setMotorAcceleration();
#ifdef PIXEL_DRAWING
else if (com.startsWith(CMD_DRAWPIXEL))
pixel_drawSquarePixel();
else if (com.startsWith(CMD_DRAWSCRIBBLEPIXEL))
pixel_drawScribblePixel();
else if (com.startsWith(CMD_CHANGEDRAWINGDIRECTION))
exec_changeDrawingDirection();
else if (com.startsWith(CMD_SETPOSITION))
exec_setPosition();
pixel_changeDrawingDirection();
else if (com.startsWith(CMD_TESTPENWIDTHSQUARE))
pixel_testPenWidth();
#endif
else if (com.startsWith(CMD_SETPOSITION))
exec_setPosition();
#ifdef PENLIFT
else if (com.startsWith(CMD_PENDOWN))
penlift_penDown();
else if (com.startsWith(CMD_PENUP))
penlift_penUp();
else if (com.startsWith(CMD_SETPENLIFTRANGE))
exec_setPenLiftRange();
#endif
else if (com.startsWith(CMD_SETMACHINESIZE))
exec_setMachineSizeFromCommand();
else if (com.startsWith(CMD_SETMACHINENAME))
exec_setMachineNameFromCommand();
else if (com.startsWith(CMD_SETMACHINEMMPERREV))
exec_setMachineMmPerRevFromCommand();
else if (com.startsWith(CMD_SETMACHINESTEPSPERREV))
exec_setMachineStepsPerRevFromCommand();
else if (com.startsWith(CMD_SETMACHINESTEPMULTIPLIER))
exec_setMachineStepMultiplierFromCommand();
else if (com.startsWith(CMD_SETPENLIFTRANGE))
exec_setPenLiftRange();
else if (com.startsWith(CMD_GETMACHINEDETAILS))
exec_reportMachineSpec();
else if (com.startsWith(CMD_RESETEEPROM))
Expand All @@ -66,25 +70,9 @@ boolean exec_executeBasicCommand(String &com)

return executed;
}

void exec_changeDrawingDirection()
{
globalDrawDirectionMode = asInt(inParam1);
globalDrawDirection = asInt(inParam2);
// Serial.print(F("Changed draw direction mode to be "));
// Serial.print(globalDrawDirectionMode);
// Serial.print(F(" and direction is "));
// Serial.println(globalDrawDirection);
}


void exec_reportMachineSpec()
{
eeprom_dumpEeprom();
Serial.print(F("PGNAME,"));
Serial.print(machineName);
Serial.println(CMD_END);

Serial.print(F("PGSIZE,"));
Serial.print(machineWidth);
Serial.print(COMMA);
Expand Down Expand Up @@ -142,20 +130,6 @@ void exec_setMachineSizeFromCommand()
eeprom_loadMachineSize();
}


void exec_setMachineNameFromCommand()
{
String name = inParam1;
if (name != DEFAULT_MACHINE_NAME)
{
for (int i = 0; i < 8; i++)
{
EEPROM.write(EEPROM_MACHINE_NAME+i, name[i]);
}
}
eeprom_loadMachineSpecFromEeprom();
}

void exec_setMachineMmPerRevFromCommand()
{
float mmPerRev = asFloat(inParam1);
Expand Down Expand Up @@ -278,6 +252,7 @@ void exec_changeLength()
changeLength(lenA, lenB);
}

#ifdef VECTOR_LINES
void exec_changeLengthDirect()
{
float endA = multiplier(asFloat(inParam1));
Expand Down Expand Up @@ -396,7 +371,7 @@ void exec_drawBetweenPoints(float p1a, float p1b, float p2a, float p2b, int maxS
{
Serial.println("Line is not on the page. Skipping it.");
}
outputAvailableMemory();
// outputAvailableMemory();
}

// Work out and return a new speed.
Expand Down Expand Up @@ -451,4 +426,4 @@ float desiredSpeed(long distanceTo, float currentSpeed, float acceleration)
//Serial.println(requiredSpeed);
return requiredSpeed;
}

#endif
4 changes: 2 additions & 2 deletions penlift.ino
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ is already up, there's a good chance it won't be up enough.
The same goes for the
*/

#ifdef PENLIFT
void penlift_movePen(int start, int end, int delay_ms)
{
penHeight.attach(PEN_HEIGHT_SERVO_PIN);
Expand Down Expand Up @@ -88,4 +88,4 @@ void penlift_penDown()
}
isPenUp = false;
}

#endif
Loading

0 comments on commit 1aa4a20

Please sign in to comment.