-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathpolargraph_server_polarshield.ino
More file actions
457 lines (350 loc) · 12.4 KB
/
polargraph_server_polarshield.ino
File metadata and controls
457 lines (350 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/**
* Polargraph Server for ATMEGA1280+ based arduino boards.
* Written by Sandy Noble
* Released under GNU License version 3.
* http://www.polargraph.co.uk
* https://github.com/euphy/polargraph_server_polarshield
The program has a core part that consists of the following files:
- comms.ino
- configuration.ino
- eeprom.ino
- exec.ino
- penlift.ino
- pixel.ino
- util.ino
and the first portion of this main file, probably called
something like polargraph_server_polarshield.ino.
This version which is for the polarshield has a
bunch of other files too, providing extra functions.
The file called impl_ps perhaps deserves a special mention, and
that file contains alternative implementations of a few functions,
where the changes to make it work on ATMEGA1280+ mean that code
is _different_ to the basic implemenation.
The UTouch library needs a couple of calibration values:
The UTouch library needs a couple of calibration values - these
ones are the ones I use for the ITDB02-2.2 inch screen.
#define CAL_X 0x039281CCUL
#define CAL_Y 0x03A2C1DEUL
#define CAL_S 0x000AF0DBUL
// for the 2.4in screen that is current.
#define CAL_X 0x03C34136UL
#define CAL_Y 0x03C0018AUL
#define CAL_S 0x000EF13FUL
Put them in libraries/UTouch/UTouchCD.h
**/
//http://forum.arduino.cc/index.php?topic=173584.0
#include <SPI.h>
#include <SD.h>
#include <AccelStepper.h>
#include <Servo.h>
#include <EEPROM.h>
#include "EEPROMAnything.h"
/* ===========================================================
CONFIGURATION!!
=========================================================== */
//Uncomment the following line to use a 2.4" panel, August 2014 and later
#define LCD_TYPE TFT01_24_8
//Uncomment the following line to use an older 2.4" panel, prior to August 2014.
//#define LCD_TYPE ITDB24E_8
//Uncomment the following line to use a 2.2" panel
//#define LCD_TYPE ITDB22
/* ===========================================================
Define what kind of driver breakout you're using.
(By commenting out the one's you _haven't_ got.)
=========================================================== */
#ifndef MOTHERBOARD
#define MOTHERBOARD POLARSHIELD
//#define MOTHERBOARD RAMPS14
//#define MOTHERBOARD TFTSHIELD
#endif
#define POLARSHIELD 1
#define RAMPS14 2
#define TFTSHIELD 3
/* ===========================================================
Control whether to look for touch input or update LCD
Comment this out if you DON'T have an LCD connected
=========================================================== */
#define USE_LCD
/* ===========================================================
Some debugging flags
=========================================================== */
//#define DEBUG_SD
//#define DEBUG_STATE
#define DEBUG_COMMS
/* ===========================================================
These variables are common to all polargraph server builds
=========================================================== */
const String FIRMWARE_VERSION_NO = "1.4.3";
#if MOTHERBOARD == RAMPS14
const String MB_NAME = "RAMPS14";
#elif MOTHERBOARD == POLARSHIELD
const String MB_NAME = "POLARSHIELD";
#elif MOTHERBOARD == TFTSHIELD
const String MB_NAME = "TFTSHIELD";
#endif
// EEPROM addresses
const int EEPROM_MACHINE_WIDTH = 0;
const int EEPROM_MACHINE_HEIGHT = 2;
const int EEPROM_MACHINE_NAME = 4;
const int EEPROM_MACHINE_MM_PER_REV = 14; // 4 bytes (float)
const int EEPROM_MACHINE_STEPS_PER_REV = 18;
const int EEPROM_MACHINE_STEP_MULTIPLIER = 20;
const int EEPROM_MACHINE_MOTOR_SPEED = 22; // 4 bytes float
const int EEPROM_MACHINE_MOTOR_ACCEL = 26; // 4 bytes float
const int EEPROM_MACHINE_PEN_WIDTH = 30; // 4 bytes float
const long EEPROM_MACHINE_HOME_A = 34; // 4 bytes
const long EEPROM_MACHINE_HOME_B = 38; // 4 bytes
const int EEPROM_PENLIFT_DOWN = 42; // 2 bytes
const int EEPROM_PENLIFT_UP = 44; // 2 bytes
// Pen raising servo
Servo penHeight;
const int DEFAULT_DOWN_POSITION = 90;
const int DEFAULT_UP_POSITION = 180;
static int upPosition = DEFAULT_UP_POSITION; // defaults
static int downPosition = DEFAULT_DOWN_POSITION;
static int penLiftSpeed = 3; // ms between steps of moving motor
#if MOTHERBOARD == RAMPS14
#define PEN_HEIGHT_SERVO_PIN 4
#else // MOTHERBOARD == POLARSHIELD
#define PEN_HEIGHT_SERVO_PIN 9
#endif
boolean debugComms = false;
boolean isPenUp = false;
int motorStepsPerRev = 200;
float mmPerRev = 95;
int stepMultiplier = 8;
static float translateX = 0.0;
static float translateY = 0.0;
static float scaleX = 1.0;
static float scaleY = 1.0;
static int rotateTransform = 0;
static int machineWidth = 650;
static int machineHeight = 800;
static int sqtest = 0;
static int defaultMachineWidth = 650;
static int defaultMachineHeight = 650;
static float defaultMmPerRev = 95.0;
static int defaultStepsPerRev = 200;
static int defaultStepMultiplier = 8;
static long startLengthStepsA = 8000;
static long startLengthStepsB = 8000;
String machineName = "";
const String DEFAULT_MACHINE_NAME = "PG01 ";
float currentMaxSpeed = 2000.0;
float currentAcceleration = 2000.0;
boolean usingAcceleration = true;
float mmPerStep = 0.0F;
float stepsPerMM = 0.0F;
long pageWidth = machineWidth * stepsPerMM;
long pageHeight = machineHeight * stepsPerMM;
long maxLength = 0;
//static char rowAxis = 'A';
const int INLENGTH = 50;
const char INTERMINATOR = 10;
const char SEMICOLON = ';';
float penWidth = 0.8f; // line width in mm
boolean reportingPosition = true;
boolean acceleration = true;
extern AccelStepper motorA;
extern AccelStepper motorB;
volatile boolean currentlyRunning = true;
static char inCmd[10];
static char inParam1[14];
static char inParam2[14];
static char inParam3[14];
static char inParam4[14];
static byte inNoOfParams;
char lastCommand[INLENGTH+1];
boolean commandConfirmed = false;
int rebroadcastReadyInterval = 5000L;
volatile long lastOperationTime = 0L;
long motorIdleTimeBeforePowerDown = 600000L;
boolean automaticPowerDown = true;
volatile long lastInteractionTime = 0L;
static boolean lastWaveWasTop = true;
static boolean lastMotorBiasWasA = true;
// Drawing direction
const static byte DIR_NE = 1;
const static byte DIR_SE = 2;
const static byte DIR_SW = 3;
const static byte DIR_NW = 4;
const static byte DIR_N = 5;
const static byte DIR_E = 6;
const static byte DIR_S = 7;
const static byte DIR_W = 8;
static int globalDrawDirection = DIR_NW;
const static byte DIR_MODE_AUTO = 1;
const static byte DIR_MODE_PRESET = 2;
const static byte DIR_MODE_RANDOM = 3;
static int globalDrawDirectionMode = DIR_MODE_AUTO;
static int currentRow = 0;
const String READY_STR = "READY_200";
const String RESEND_STR = "RESEND";
const String DRAWING_STR = "DRAWING";
const static String OUT_CMD_CARTESIAN_STR = "CARTESIAN,";
const static String OUT_CMD_SYNC_STR = "SYNC,";
char MSG_E_STR[] = "MSG,E,";
char MSG_I_STR[] = "MSG,I,";
char MSG_D_STR[] = "MSG,D,";
static boolean pixelDebug = true;
static const byte ALONG_A_AXIS = 0;
static const byte ALONG_B_AXIS = 1;
static const byte SQUARE_SHAPE = 0;
static const byte SAW_SHAPE = 1;
const static char COMMA[] = ",";
const static char CMD_END[] = ",END";
const static String CMD_CHANGELENGTH = "C01";
const static String CMD_CHANGEPENWIDTH = "C02";
const static String CMD_CHANGEMOTORSPEED = "C03";
const static String CMD_CHANGEMOTORACCEL = "C04";
const static String CMD_DRAWPIXEL = "C05";
const static String CMD_DRAWSCRIBBLEPIXEL = "C06";
const static String CMD_CHANGEDRAWINGDIRECTION = "C08";
const static String CMD_SETPOSITION = "C09";
const static String CMD_TESTPATTERN = "C10";
const static String CMD_TESTPENWIDTHSQUARE = "C11";
const static String CMD_PENDOWN = "C13";
const static String CMD_PENUP = "C14";
const static String CMD_CHANGELENGTHDIRECT = "C17";
const static String CMD_SETMACHINESIZE = "C24";
const static String CMD_SETMACHINENAME = "C25";
const static String CMD_GETMACHINEDETAILS = "C26";
const static String CMD_RESETEEPROM = "C27";
const static String CMD_SETMACHINEMMPERREV = "C29";
const static String CMD_SETMACHINESTEPSPERREV = "C30";
const static String CMD_SETMOTORSPEED = "C31";
const static String CMD_SETMOTORACCEL = "C32";
const static String CMD_SETMACHINESTEPMULTIPLIER = "C37";
const static String CMD_SETPENLIFTRANGE = "C45";
const static String CMD_PIXELDIAGNOSTIC = "C46";
const static String CMD_SET_DEBUGCOMMS = "C47";
void setup()
{
Serial.begin(57600); // set up Serial library at 57600 bps
Serial.println(F("POLARGRAPH ON!"));
Serial.print(F("v"));
Serial.println(FIRMWARE_VERSION_NO);
Serial.print(F("Hardware: "));
Serial.println(MB_NAME);
Serial.print(F("Servo "));
Serial.println(PEN_HEIGHT_SERVO_PIN);
configuration_motorSetup();
eeprom_loadMachineSpecFromEeprom();
configuration_setup();
motorA.setMaxSpeed(currentMaxSpeed);
motorA.setAcceleration(currentAcceleration);
motorB.setMaxSpeed(currentMaxSpeed);
motorB.setAcceleration(currentAcceleration);
motorA.setCurrentPosition(startLengthStepsA);
motorB.setCurrentPosition(startLengthStepsB);
for (int i = 0; i<INLENGTH; i++) {
lastCommand[i] = 0;
}
pinMode(PEN_HEIGHT_SERVO_PIN, OUTPUT);
delay(500);
penlift_penUp();
sd_autorunSD();
}
void loop()
{
if (comms_waitForNextCommand(lastCommand))
{
#ifdef DEBUG_COMMS
Serial.print(F("Last comm: "));
Serial.print(lastCommand);
Serial.println(F("..."));
#endif
comms_parseAndExecuteCommand(lastCommand);
}
}
/*===========================================================
These variables are for the polarshield / mega
=========================================================== */
#include <UTFT.h>
#include <UTouch.h>
const static String CMD_TESTPENWIDTHSCRIBBLE = "C12";
const static String CMD_DRAWSAWPIXEL = "C15,";
const static String CMD_DRAWCIRCLEPIXEL = "C16";
const static String CMD_SET_ROVE_AREA = "C21";
const static String CMD_DRAWDIRECTIONTEST = "C28";
const static String CMD_MODE_STORE_COMMANDS = "C33";
const static String CMD_MODE_EXEC_FROM_STORE = "C34";
const static String CMD_MODE_LIVE = "C35";
const static String CMD_RANDOM_DRAW = "C36";
const static String CMD_START_TEXT = "C38";
const static String CMD_DRAW_SPRITE = "C39";
const static String CMD_CHANGELENGTH_RELATIVE = "C40";
const static String CMD_SWIRLING = "C41";
const static String CMD_DRAW_RANDOM_SPRITE = "C42";
const static String CMD_DRAW_NORWEGIAN = "C43";
const static String CMD_DRAW_NORWEGIAN_OUTLINE = "C44";
const static String CMD_AUTO_CALIBRATE = "C48";
/* End stop pin definitions */
const int ENDSTOP_X_MAX = 17;
const int ENDSTOP_X_MIN = 16;
const int ENDSTOP_Y_MAX = 15;
const int ENDSTOP_Y_MIN = 14;
long ENDSTOP_X_MIN_POSITION = 130;
long ENDSTOP_Y_MIN_POSITION = 130;
long motorARestPoint = 0;
long motorBRestPoint = 0;
/* Stuff for display */
extern uint8_t SmallFont[];
extern uint8_t BigFont[];
UTFT lcd(LCD_TYPE, 38,39,40,41);
#if MOTHERBOARD == TFTSHIELD
UTouch touch(6, 5, 4, 3, 2); // pinouts for the TFT shield
#else
UTouch touch(11,12,18,19, 2);
#endif
const int INTERRUPT_TOUCH_PIN = 0;
boolean displayTouched = false;
int touchX = 0;
int touchY = 0;
// size and location of rove area
long rove1x = 1000;
long rove1y = 1000;
long roveWidth = 5000;
long roveHeight = 8000;
boolean swirling = false;
String spritePrefix = "";
int textRowSize = 200;
int textCharSize = 180;
boolean useRoveArea = false;
int commandNo = 0;
int errorInjection = 0;
long screenSaveIdleTime = 1200000L;
const static byte SCREEN_STATE_NORMAL = 0;
const static byte SCREEN_STATE_POWER_SAVE = 1;
byte screenState = SCREEN_STATE_NORMAL;
boolean storeCommands = false;
boolean drawFromStore = false;
String commandFilename = "";
// sd card stuff
const int chipSelect = 53;
boolean sdCardInit = false;
// set up variables using the SD utility library functions:
File root;
boolean cardPresent = false;
boolean cardInit = false;
boolean echoingStoredCommands = false;
// the file itself
File pbmFile;
// information we extract about the bitmap file
long pbmWidth, pbmHeight;
float pbmScaling = 1.0;
int pbmDepth, pbmImageoffset;
long pbmFileLength = 0;
float pbmAspectRatio = 1.0;
volatile int speedChangeIncrement = 100;
volatile int accelChangeIncrement = 100;
volatile float penWidthIncrement = 0.05;
volatile int moveIncrement = 400;
boolean currentlyDrawingFromFile = false;
String currentlyDrawingFilename = "";
boolean powerIsOn = false;
boolean isCalibrated = false;
boolean canCalibrate = false;
boolean useAutoStartFromSD = true;
String autoStartFilename = "AUTORUN.TXT";
boolean autoStartFileFound = false;