forked from reprappro/RepRapFirmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RepRapFirmware.cpp
1669 lines (1427 loc) · 40.8 KB
/
RepRapFirmware.cpp
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************************************
RepRapFirmware - Main Program
This firmware is intended to be a fully object-oriented highly modular control program for
RepRap self-replicating 3D printers.
It owes a lot to Marlin and to the original RepRap FiveD_GCode.
General design principles:
* Control by RepRap G Codes. These are taken to be machine independent, though some may be unsupported.
* Full use of C++ OO techniques,
* Make classes hide their data,
* Make everything except the Platform class (see below) as stateless as possible,
* No use of conditional compilation except for #include guards - if you need that, you should be
forking the repository to make a new branch - let the repository take the strain,
* Concentration of all machine-dependent definitions and code in Platform.h and Platform.cpp,
* No specials for (X,Y) or (Z) - all movement is 3-dimensional,
* Except in Platform.h, use real units (mm, seconds etc) throughout the rest of the code wherever possible,
* Try to be efficient in memory use, but this is not critical,
* Labour hard to be efficient in time use, and this is critical,
* Don't abhor floats - they work fast enough if you're clever,
* Don't avoid arrays and structs/classes,
* Don't avoid pointers,
* Use operator and function overloading where appropriate.
Naming conventions:
* #defines are all CAPITALS_WITH_OPTIONAL_UNDERSCORES_BETWEEN_WORDS
* No underscores in other names - MakeReadableWithCapitalisation
* Class names and functions start with a CapitalLetter
* Variables start with a lowerCaseLetter
* Use veryLongDescriptiveNames
Structure:
There are eight main classes:
* RepRap
* GCodes
* Heat
* Move
* Platform
* Network
* Webserver, and
* PrintMonitor
RepRap:
This is just a container class for the single instances of all the others, and otherwise does very little.
GCodes:
This class is fed GCodes, either from the web interface, or from GCode files, or from a serial interface,
Interprets them, and requests actions from the RepRap machine via the other classes.
Heat:
This class implements all heating and temperature control in the RepRap machine.
Move:
This class controls all movement of the RepRap machine, both along its axes, and in its extruder drives.
Platform:
This is the only class that knows anything about the physical setup of the RepRap machine and its
controlling electronics. It implements the interface between all the other classes and the RepRap machine.
All the other classes are completely machine-independent (though they may declare arrays dimensioned
to values #defined in Platform.h).
Network:
This class implements a basic TCP interface for the Webserver classes using lwip.
Webserver:
This class talks to the network (via Platform) and implements a simple webserver to give an interactive
interface to the RepRap machine. It uses the Knockout and Jquery Javascript libraries to achieve this.
In addition, FTP and Telnet servers are provided for easier SD card file management and G-Code handling.
PrintMonitor:
This class provides methods to obtain statistics (height, filament usage etc.) from generated G-Code
files and to calculate estimated print end-times for a live print.
When the software is running there is one single instance of each main class, and all the memory allocation is
done on initialization. new/malloc should not be used in the general running code, and delete is never
used. Each class has an Init() function that resets it to its boot-up state; the constructors merely handle
that memory allocation on startup. Calling RepRap.Init() calls all the other Init()s in the right sequence.
There are other ancillary classes that are declared in the .h files for the master classes that use them. For
example, Move has a DDA class that implements a Bresenham/digital differential analyser.
Timing:
There is a single interrupt chain entered via Platform.Interrupt(). This controls movement step timing, and
this chain of code should be the only place that volatile declarations and structure/variable-locking are
required. All the rest of the code is called sequentially and repeatedly as follows:
All the main classes have a Spin() function. These are called in a loop by the RepRap.Spin() function and implement
simple timesharing. No class does, or ever should, wait inside one of its functions for anything to happen or call
any sort of delay() function. The general rule is:
Can I do a thing?
Yes - do it
No - set a flag/timer to remind me to do it next-time-I'm-called/at-a-future-time and return.
The restriction this strategy places on almost all the code in the firmware (that it must execute quickly and
never cause waits or delays) is balanced by the fact that none of that code needs to worry about synchronization,
locking, or other areas of code accessing items upon which it is working. As mentioned, only the interrupt
chain needs to concern itself with such problems. Unlike movement, heating (including PID controllers) does
not need the fast precision of timing that interrupts alone can offer. Indeed, most heating code only needs
to execute a couple of times a second.
Most data is transferred bytewise, with classes' Spin() functions typically containing code like this:
Is a byte available for me?
Yes
read it and add it to my buffer
Is my buffer complete?
Yes
Act on the contents of my buffer
No
Return
No
Return
Note that it is simple to raise the "priority" of any class's activities relative to the others by calling its
Spin() function more than once from RepRap.Spin().
-----------------------------------------------------------------------------------------------------
Version 0.1
18 November 2012
Adrian Bowyer
RepRap Professional Ltd
http://reprappro.com
Licence: GPL
****************************************************************************************************/
#include "RepRapFirmware.h"
// We just need one instance of RepRap; everything else is contained within it and hidden
RepRap reprap;
//*************************************************************************************************
// RepRap member functions.
// Do nothing more in the constructor; put what you want in RepRap:Init()
RepRap::RepRap() : active(false), debug(0), stopped(false), spinningModule(noModule), ticksInSpinState(0),
resetting(false), gcodeReply(gcodeReplyBuffer, GCODE_REPLY_LENGTH)
{
platform = new Platform();
network = new Network(platform);
webserver = new Webserver(platform, network);
gCodes = new GCodes(platform, webserver);
move = new Move(platform, gCodes);
heat = new Heat(platform, gCodes);
printMonitor = new PrintMonitor(platform, gCodes);
toolList = NULL;
chamberHeater = -1;
}
void RepRap::Init()
{
debug = false;
// zpl thinks it's a bad idea to count the bed as an active heater...
activeExtruders = activeHeaters = 0;
SetPassword(DEFAULT_PASSWORD);
SetName(DEFAULT_NAME);
beepFrequency = beepDuration = 0;
message[0] = 0;
gcodeReply[0] = 0;
replySeq = webSeq = auxSeq = 0;
processingConfig = true;
// All of the following init functions must execute reasonably quickly before the watchdog times us out
platform->Init();
gCodes->Init();
webserver->Init();
move->Init();
heat->Init();
printMonitor->Init();
currentTool = NULL;
coldExtrude = false;
active = true; // must do this before we start the network, else the watchdog may time out
platform->Message(HOST_MESSAGE, "%s Version %s dated %s\n", NAME, VERSION, DATE);
FileStore *startup = platform->GetFileStore(platform->GetSysDir(), platform->GetConfigFile(), false);
platform->AppendMessage(HOST_MESSAGE, "\n\nExecuting ");
if(startup != NULL)
{
startup->Close();
platform->AppendMessage(HOST_MESSAGE, "%s...\n\n", platform->GetConfigFile());
scratchString.printf("M98 P%s\n", platform->GetConfigFile());
}
else
{
platform->AppendMessage(HOST_MESSAGE, "%s (no configuration file found)...\n\n", platform->GetDefaultFile());
scratchString.printf("M98 P%s\n", platform->GetDefaultFile());
}
// We inject an M98 into the serial input stream to run the start-up macro
platform->GetLine()->InjectString(scratchString.Pointer());
bool runningTheFile = false;
while (true)
{
Spin();
if (gCodes->DoingFileMacro())
{
runningTheFile = true;
}
else if (runningTheFile)
{
break;
}
}
processingConfig = false;
if(network->IsEnabled())
{
platform->AppendMessage(HOST_MESSAGE, "\nStarting network...\n");
network->Init(); // Need to do this here, as the configuration GCodes may set IP address etc.
}
else
{
platform->AppendMessage(HOST_MESSAGE, "\nNetwork disabled.\n");
}
platform->AppendMessage(HOST_MESSAGE, "\n%s is up and running.\n", NAME);
fastLoop = FLT_MAX;
slowLoop = 0.0;
lastTime = platform->Time();
}
void RepRap::Exit()
{
active = false;
heat->Exit();
move->Exit();
gCodes->Exit();
webserver->Exit();
platform->Message(BOTH_MESSAGE, "RepRap class exited.\n");
platform->Exit();
}
void RepRap::Spin()
{
if(!active)
return;
spinningModule = modulePlatform;
ticksInSpinState = 0;
platform->Spin();
spinningModule = moduleNetwork;
ticksInSpinState = 0;
network->Spin();
spinningModule = moduleWebserver;
ticksInSpinState = 0;
webserver->Spin();
spinningModule = moduleGcodes;
ticksInSpinState = 0;
gCodes->Spin();
spinningModule = moduleMove;
ticksInSpinState = 0;
move->Spin();
spinningModule = moduleHeat;
ticksInSpinState = 0;
heat->Spin();
spinningModule = modulePrintMonitor;
ticksInSpinState = 0;
printMonitor->Spin();
spinningModule = noModule;
ticksInSpinState = 0;
// Check if we need to display a cold extrusion warning
bool displayWarning = false;
for(Tool *t = toolList; t != NULL; t = t->Next())
{
displayWarning |= t->DisplayColdExtrudeWarning();
}
if (displayWarning)
{
platform->Message(BOTH_MESSAGE, "Warning: Tools can only be driven if their heater temperatures are high!\n");
}
// Keep track of the loop time
float t = platform->Time();
float dt = t - lastTime;
if(dt < fastLoop)
{
fastLoop = dt;
}
if(dt > slowLoop)
{
slowLoop = dt;
}
lastTime = t;
}
void RepRap::Timing()
{
platform->AppendMessage(BOTH_MESSAGE, "Slowest main loop (seconds): %f; fastest: %f\n", slowLoop, fastLoop);
fastLoop = FLT_MAX;
slowLoop = 0.0;
}
void RepRap::Diagnostics()
{
platform->Diagnostics(); // this includes a call to our Timing() function
move->Diagnostics();
heat->Diagnostics();
gCodes->Diagnostics();
network->Diagnostics();
webserver->Diagnostics();
}
// Turn off the heaters, disable the motors, and
// deactivate the Heat and Move classes. Leave everything else
// working.
void RepRap::EmergencyStop()
{
stopped = true;
platform->SetAtxPower(false); // turn off the ATX power if we can
//platform->DisableInterrupts();
Tool* tool = toolList;
while(tool)
{
tool->Standby();
tool = tool->Next();
}
heat->Exit();
for(int8_t heater = 0; heater < HEATERS; heater++)
{
platform->SetHeater(heater, 0.0);
}
// We do this twice, to avoid an interrupt switching
// a drive back on. move->Exit() should prevent
// interrupts doing this.
for(int8_t i = 0; i < 2; i++)
{
move->Exit();
for(int8_t drive = 0; drive < DRIVES; drive++)
{
platform->SetMotorCurrent(drive, 0.0);
platform->DisableDrive(drive);
}
}
}
void RepRap::SetDebug(Module m, bool enable)
{
if (enable)
{
debug |= (1 << m);
}
else
{
debug &= ~(1 << m);
}
PrintDebug();
}
void RepRap::SetDebug(bool enable)
{
debug = (enable) ? 0xFFFF : 0;
}
void RepRap::PrintDebug()
{
if (debug != 0)
{
platform->Message(BOTH_MESSAGE, "Debugging enabled for modules:");
for(uint8_t i=0; i<16;i++)
{
if (debug & (1 << i))
{
platform->AppendMessage(BOTH_MESSAGE, " %s", moduleName[i]);
}
}
platform->AppendMessage(BOTH_MESSAGE, "\n");
}
else
{
platform->Message(BOTH_MESSAGE, "Debugging disabled\n");
}
}
void RepRap::AddTool(Tool* tool)
{
if(toolList == NULL)
{
toolList = tool;
}
else
{
toolList->AddTool(tool);
}
tool->UpdateExtruderAndHeaterCount(activeExtruders, activeHeaters);
}
void RepRap::DeleteTool(Tool* tool)
{
// Must have a valid tool...
if (tool == NULL)
{
return;
}
// Deselect it if necessary
if (GetCurrentTool() == tool)
{
SelectTool(-1);
}
// Switch off any associated heater
for(size_t i=0; i<tool->HeaterCount(); i++)
{
reprap.GetHeat()->SwitchOff(tool->Heater(i));
}
// Purge any references to this tool
Tool *parent = NULL;
for(Tool *t = toolList; t != NULL; t = t->Next())
{
if (t->Next() == tool)
{
parent = t;
break;
}
}
if (parent == NULL)
{
toolList = tool->Next();
}
else
{
parent->next = tool->next;
}
// Delete it
delete tool;
// Update the number of active heaters and extruder drives
activeExtruders = activeHeaters = 0;
for(Tool *t = toolList; t != NULL; t = t->Next())
{
t->UpdateExtruderAndHeaterCount(activeExtruders, activeHeaters);
}
}
void RepRap::SelectTool(int toolNumber)
{
Tool* tool = toolList;
while(tool)
{
if (tool->Number() == toolNumber)
{
tool->Activate(currentTool);
currentTool = tool;
return;
}
tool = tool->Next();
}
// Selecting a non-existent tool is valid. It sets them all to standby.
if (currentTool != NULL)
{
StandbyTool(currentTool->Number());
}
currentTool = NULL;
}
void RepRap::PrintTool(int toolNumber, StringRef& reply)
{
for(Tool *tool = toolList; tool != NULL; tool = tool->next)
{
if (tool->Number() == toolNumber)
{
tool->Print(reply);
return;
}
}
reply.copy("Attempt to print details of non-existent tool.\n");
}
void RepRap::StandbyTool(int toolNumber)
{
Tool* tool = toolList;
while(tool)
{
if (tool->Number() == toolNumber)
{
tool->Standby();
if (currentTool == tool)
{
currentTool = NULL;
}
return;
}
tool = tool->Next();
}
platform->Message(BOTH_MESSAGE, "Attempt to standby a non-existent tool: %d.\n", toolNumber);
}
Tool* RepRap::GetTool(int toolNumber)
{
Tool* tool = toolList;
while(tool)
{
if(tool->Number() == toolNumber)
{
return tool;
}
tool = tool->Next();
}
return NULL; // Not an error
}
/*Tool* RepRap::GetToolByDrive(int driveNumber)
{
Tool* tool = toolList;
while (tool)
{
for(uint8_t drive = 0; drive < tool->DriveCount(); drive++)
{
if (tool->Drive(drive) + AXES == driveNumber)
{
return tool;
}
}
tool = tool->Next();
}
return NULL;
}*/
void RepRap::SetToolVariables(int toolNumber, float* standbyTemperatures, float* activeTemperatures)
{
Tool* tool = toolList;
while(tool)
{
if(tool->Number() == toolNumber)
{
tool->SetVariables(standbyTemperatures, activeTemperatures);
return;
}
tool = tool->Next();
}
platform->Message(BOTH_MESSAGE, "Attempt to set variables for a non-existent tool: %d.\n", toolNumber);
}
void RepRap::Tick()
{
if (active && !resetting)
{
platform->Tick();
++ticksInSpinState;
if (ticksInSpinState >= 20000) // if we stall for 20 seconds, save diagnostic data and reset
{
resetting = true;
for(size_t i = 0; i < HEATERS; i++)
{
platform->SetHeater(i, 0.0);
}
for(size_t i = 0; i < DRIVES; i++)
{
platform->DisableDrive(i);
// We can't set motor currents to 0 here because that requires interrupts to be working, and we are in an ISR
}
platform->SoftwareReset(SoftwareResetReason::stuckInSpin);
}
}
}
// Get the JSON status response for the web server (or later for the M105 command).
// Type 1 is the ordinary JSON status response.
// Type 2 is the same except that static parameters are also included.
// Type 3 is the same but instead of static parameters we report print estimation values.
void RepRap::GetStatusResponse(StringRef& response, uint8_t type, bool forWebserver)
{
// Machine status
char ch = GetStatusCharacter();
response.printf("{\"status\":\"%c\",\"coords\":{", ch);
/* Coordinates */
{
float liveCoordinates[DRIVES + 1];
if (currentTool != NULL)
{
const float *offset = currentTool->GetOffset();
for (size_t i = 0; i < AXES; ++i)
{
liveCoordinates[i] += offset[i];
}
}
move->LiveCoordinates(liveCoordinates);
// Homed axes
response.catf("\"axesHomed\":[%d,%d,%d]",
(gCodes->GetAxisIsHomed(0)) ? 1 : 0,
(gCodes->GetAxisIsHomed(1)) ? 1 : 0,
(gCodes->GetAxisIsHomed(2)) ? 1 : 0);
// Actual and theoretical extruder positions since power up, last G92 or last M23
response.catf(",\"extr\":"); // announce actual extruder positions
ch = '[';
for (uint8_t extruder = 0; extruder < GetExtrudersInUse(); extruder++)
{
response.catf("%c%.1f", ch, liveCoordinates[AXES + extruder]);
ch = ',';
}
if (ch == '[')
{
response.cat("[");
}
// XYZ positions
response.cat("],\"xyz\":");
ch = '[';
for (uint8_t axis = 0; axis < AXES; axis++)
{
response.catf("%c%.2f", ch, liveCoordinates[axis]);
ch = ',';
}
}
// Current tool number
int toolNumber = (currentTool == NULL) ? -1 : currentTool->Number();
response.catf("]},\"currentTool\":%d", toolNumber);
/* Output - only reported once */
{
bool sendBeep = (beepDuration != 0 && beepFrequency != 0);
bool sendMessage = (message[0]) && ((gCodes->HaveAux() && !forWebserver) || (!gCodes->HaveAux() && forWebserver));
if (sendBeep || sendMessage)
{
response.cat(",\"output\":{");
// Report beep values
if (sendBeep)
{
response.catf("\"beepDuration\":%d,\"beepFrequency\":%d", beepDuration, beepFrequency);
if (sendMessage)
{
response.cat(",");
}
beepFrequency = beepDuration = 0;
}
// Report message
if (sendMessage)
{
response.cat("\"message\":");
EncodeString(response, message, 2, false);
message[0] = 0;
}
response.cat("}");
}
}
/* Parameters */
{
// ATX power
response.catf(",\"params\":{\"atxPower\":%d", platform->AtxPower() ? 1 : 0);
// Cooling fan value
float fanValue = (gCodes->CoolingInverted() ? 1.0 - platform->GetFanValue() : platform->GetFanValue());
response.catf(",\"fanPercent\":%.2f", fanValue * 100.0);
// Speed and Extrusion factors
response.catf(",\"speedFactor\":%.2f,\"extrFactors\":", move->GetSpeedFactor() * 100.0);
ch = '[';
for (uint8_t extruder = 0; extruder < GetExtrudersInUse(); extruder++)
{
response.catf("%c%.2f", ch, move->GetExtrusionFactor(extruder) * 100.0);
ch = ',';
}
response.cat((ch == '[') ? "[]}" : "]}");
}
// G-code reply sequence for webserver
if (forWebserver)
{
response.catf(",\"seq\":%d", GetReplySeq());
// There currently appears to be no need for this one, so skip it
//response.catf(",\"buff\":%u", webserver->GetGcodeBufferSpace());
}
/* Sensors */
{
response.cat(",\"sensors\":{");
// Probe
int v0 = platform->ZProbe();
int v1, v2;
switch (platform->GetZProbeSecondaryValues(v1, v2))
{
case 1:
response.catf("\"probeValue\":\%d,\"probeSecondary\":[%d]", v0, v1);
break;
case 2:
response.catf("\"probeValue\":\%d,\"probeSecondary\":[%d,%d]", v0, v1, v2);
break;
default:
response.catf("\"probeValue\":%d", v0);
break;
}
// Fan RPM
response.catf(",\"fanRPM\":%d}", (unsigned int)platform->GetFanRPM());
}
/* Temperatures */
{
response.cat(",\"temps\":{");
/* Bed */
#if HOT_BED != -1
{
response.catf("\"bed\":{\"current\":%.1f,\"active\":%.1f,\"state\":%d},",
heat->GetTemperature(HOT_BED), heat->GetActiveTemperature(HOT_BED),
heat->GetStatus(HOT_BED));
}
#endif
/* Chamber */
if (chamberHeater != -1)
{
response.catf("\"chamber\":{\"current\":%.1f,", heat->GetTemperature(chamberHeater));
response.catf("\"active\":%.1f,", heat->GetActiveTemperature(chamberHeater));
response.catf("\"state\":%d},", static_cast<int>(heat->GetStatus(chamberHeater)));
}
/* Heads */
{
response.cat("\"heads\":{\"current\":");
// Current temperatures
ch = '[';
for (size_t heater = E0_HEATER; heater < GetHeatersInUse(); heater++)
{
response.catf("%c%.1f", ch, heat->GetTemperature(heater));
ch = ',';
}
response.cat((ch == '[') ? "[]" : "]");
// Active temperatures
response.catf(",\"active\":");
ch = '[';
for (size_t heater = E0_HEATER; heater < GetHeatersInUse(); heater++)
{
response.catf("%c%.1f", ch, heat->GetActiveTemperature(heater));
ch = ',';
}
response.cat((ch == '[') ? "[]" : "]");
// Standby temperatures
response.catf(",\"standby\":");
ch = '[';
for (size_t heater = E0_HEATER; heater < GetHeatersInUse(); heater++)
{
response.catf("%c%.1f", ch, heat->GetStandbyTemperature(heater));
ch = ',';
}
response.cat((ch == '[') ? "[]" : "]");
// Heater statuses (0=off, 1=standby, 2=active, 3=fault)
response.cat(",\"state\":");
ch = '[';
for (size_t heater = E0_HEATER; heater < GetHeatersInUse(); heater++)
{
response.catf("%c%d", ch, static_cast<int>(heat->GetStatus(heater)));
ch = ',';
}
response.cat((ch == '[') ? "[]" : "]");
}
response.cat("}}");
}
// Time since last reset
response.catf(",\"time\":%.1f", platform->Time());
/* Extended Status Response */
if (type == 2)
{
// Cold Extrude/Retract
response.catf(",\"coldExtrudeTemp\":%1.f", ColdExtrude() ? 0 : HOT_ENOUGH_TO_EXTRUDE);
response.catf(",\"coldRetractTemp\":%1.f", ColdExtrude() ? 0 : HOT_ENOUGH_TO_RETRACT);
// Delta configuration
response.cat(",\"geometry\":\"cartesian\""); // TODO: Implement this with delta being an alternative
// Machine name
response.cat(",\"name\":");
EncodeString(response, myName, 2, false);
/* Probe */
{
ZProbeParameters probeParams;
platform->GetZProbeParameters(probeParams);
// Trigger threshold
response.catf(",\"probe\":{\"threshold\":%d", probeParams.adcValue);
// Trigger height
response.catf(",\"height\":%.2f", probeParams.height);
// Type
response.catf(",\"type\":%d}", platform->GetZProbeType());
}
/* Tool Mapping */
{
response.cat(",\"tools\":[");
for(Tool *tool=toolList; tool != NULL; tool = tool->Next())
{
// Heaters
response.catf("{\"number\":%d,\"heaters\":[", tool->Number());
for(size_t heater=0; heater<tool->HeaterCount(); heater++)
{
response.catf("%d", tool->Heater(heater));
if (heater < tool->HeaterCount() - 1)
{
response.cat(",");
}
}
// Extruder drives
response.cat("],\"drives\":[");
for(size_t drive=0; drive<tool->DriveCount(); drive++)
{
response.catf("%d", tool->Drive(drive));
if (drive < tool->DriveCount() - 1)
{
response.cat(",");
}
}
// Do we have any more tools?
if (tool->Next() != NULL)
{
response.cat("]},");
}
else
{
response.cat("]}");
}
}
response.cat("]");
}
}
else if (type == 3)
{
// Current Layer
response.catf(",\"currentLayer\":%d", printMonitor->GetCurrentLayer());
// Current Layer Time
response.catf(",\"currentLayerTime\":%.1f", printMonitor->GetCurrentLayerTime());
// Raw Extruder Positions
float rawExtruderPos[DRIVES - AXES];
move->GetRawExtruderPositions(rawExtruderPos);
response.cat(",\"extrRaw\":");
ch = '[';
for (size_t extruder = 0; extruder < GetExtrudersInUse(); extruder++) // loop through extruders
{
response.catf("%c%.1f", ch, rawExtruderPos[extruder]);
ch = ',';
}
if (ch == '[')
{
response.cat("]");
}
// Fraction of file printed
response.catf("],\"fractionPrinted\":%.1f", (gCodes->PrintingAFile()) ? (gCodes->FractionOfFilePrinted() * 100.0) : 0.0);
// First Layer Duration
response.catf(",\"firstLayerDuration\":%.1f", printMonitor->GetFirstLayerDuration());
// First Layer Height
response.catf(",\"firstLayerHeight\":%.2f", printMonitor->GetFirstLayerHeight());
// Print Duration
response.catf(",\"printDuration\":%.1f", printMonitor->GetPrintDuration());
// Warm-Up Time
response.catf(",\"warmUpDuration\":%.1f", printMonitor->GetWarmUpDuration());
/* Print Time Estimations */
{
// Based on file progress
response.catf(",\"timesLeft\":{\"file\":%.1f", printMonitor->EstimateTimeLeft(fileBased));
// Based on filament usage
response.catf(",\"filament\":%.1f", printMonitor->EstimateTimeLeft(filamentBased));
// Based on layers
response.catf(",\"layer\":%.1f}", printMonitor->EstimateTimeLeft(layerBased));
}
}
response.cat("}");
}
void RepRap::GetConfigResponse(StringRef& response)
{
// Axis minima
response.copy("{\"axisMins\":");
char ch = '[';
for (size_t axis = 0; axis < AXES; axis++)
{
response.catf("%c%.2f", ch, platform->AxisMinimum(axis));
ch = ',';
}
// Axis maxima
response.cat("],\"axisMaxes\":");
ch = '[';
for (size_t axis = 0; axis < AXES; axis++)
{
response.catf("%c%.2f", ch, platform->AxisMaximum(axis));
ch = ',';
}
// Accelerations
response.cat("],\"accelerations\":");
ch = '[';
for (size_t drive = 0; drive < DRIVES; drive++)
{
response.catf("%c%.2f", ch, platform->Acceleration(drive));
ch = ',';
}
// Firmware details
response.catf("],\"firmwareElectronics\":\"%s\"", ELECTRONICS);
response.catf(",\"firmwareName\":\"%s\"", NAME);
response.catf(",\"firmwareVersion\":\"%s\"", VERSION);
response.catf(",\"firmwareDate\":\"%s\"", DATE);
// Minimum feedrates
response.cat(",\"minFeedrates\":");
ch = '[';
for (size_t drive = 0; drive < DRIVES; drive++)