summaryrefslogtreecommitdiff
path: root/trunk/darwin/firmware/PIC/devices/stepmotor/stepmotor2.c
blob: 580824d20aa3336872814bb0d0592bb3bead1203 (plain)
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
/*
 *
 * RepRap, The Replicating Rapid Prototyper Project
 *
 * http://reprap.org/
 *
 * RepRap is copyright (C) 2005-6 University of Bath, the RepRap
 * researchers (see the project's People webpage), and other contributors.
 *
 * RepRap is free; you can redistribute it and/or modify it under the
 * terms of the GNU Library General Public Licence as published by the
 * Free Software Foundation; either version 2 of the Licence, or (at your
 * option) any later version.
 *
 * RepRap is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
 * Licence for more details.
 *
 * For this purpose the words "software" and "library" in the GNU Library
 * General Public Licence are taken to mean any and all computer programs
 * computer files data results documents and other copyright information
 * available from the RepRap project.
 *
 * You should have received a copy of the GNU Library General Public
 * Licence along with RepRap (in reports, it will be one of the
 * appendices, for example); if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA, or see
 *
 * http://www.gnu.org/
 *
 */

#include "stepmotor.h"
#include "serial.h"

// Simple stepper controller.
//
// This is for a bipolar stepper - either 4 coils with pairs wired
// in series, or two coils.
//
// A total of 8 steps are achieved for finer movement by also making
// use of "half-steps".
//

// I/O ports - old design:

//   B0 - Min optointerrupter (optional)
//   B1 - UART Receive
//   B2 - UART Transmit
//   B3 - PWM output for motor current control
//   B4-B7 - Stepper motor connections
//   A2 - Sync A input/output (optional)
//   A3 - Sync B input/output (optional)
//   A5 - Max optointerrupter (optional)

// I/O ports - Universal PCB (new design):

// RA0 is L298 4
// RA1 is Sync
// RA2 is L298 3                                                            
// RA3 is Sync
// RA4 is the indicator LED
// RA5 is Max sensor
// RA6 is not used for the stepper
// RA7 is not used for the stepper  
                              
// RB0 is Min sensor
// RB1 is Rx
// RB2 is Tx 
// RB3 is PWM output to L298                                                                                       
// RB4 is L298 2
// RB5 is L298 1
// RB6 is not used
// RB7 is not used


#ifdef HALFSTEP
#define stepCount 8
#else
#define stepCount 4
#endif

typedef union {
  int ival;
  byte bytes[2];
} addressableInt;

volatile static byte coilPosition = 0;

#define CMD_VERSION   0
#define CMD_FORWARD   1
#define CMD_REVERSE   2
#define CMD_SETPOS    3
#define CMD_GETPOS    4
#define CMD_SEEK      5
#define CMD_FREE      6
#define CMD_NOTIFY    7
#define CMD_SYNC      8
#define CMD_CALIBRATE 9
#define CMD_GETRANGE  10
#define CMD_DDA       11
#define CMD_FORWARD1  12
#define CMD_BACKWARD1 13
#define CMD_SETPOWER  14
#define CMD_GETSENSOR 15
#define CMD_HOMERESET 16
#define CMD_CHECKHOSTVERSION  254
#define CMD_GETMODULETYPE 255

enum functions {
  func_idle,
  func_forward,
  func_reverse,
  func_syncwait,   // Waiting for sync prior to seeking
  func_seek,
  func_findmin,    // Calibration, finding minimum
  func_findmax,    // Calibration, finding maximum
  func_ddamaster,
  func_homereset   // Move to min position and reset
};
volatile static byte function = func_idle;

volatile static byte speed = 0;

volatile static byte seekNotify = 255;

// Uncomment this when we have more memory...
//volatile static byte stepPower = 32;

volatile static addressableInt currentPosition, seekPosition, maxPosition;

volatile static addressableInt dda_deltay;
volatile static int dda_error;
static int dda_deltax;

enum sync_modes {
  sync_none,     // no sync (default)
  sync_seek,     // synchronised seeking
  sync_inc,      // inc motor on each pulse
  sync_dec       // dec motor on each pulse
};
static byte sync_mode = sync_none;

void init2()
{
  /// @todo Remove some of these when intialisers fixed
  speed = 0;
  function = func_idle;
  coilPosition = 0;
  sync_mode = sync_none;
  seekNotify = 255;

  currentPosition.bytes[0] = 0;
  currentPosition.bytes[1] = 0;
  seekPosition.bytes[0] = 0;
  seekPosition.bytes[1] = 0;
  maxPosition.bytes[0] = 0;
  maxPosition.bytes[1] = 0;
}

#ifdef UNIVERSAL_PCB

#pragma save
#pragma nooverlay

void motor_stop()
{
	PORTB = PORTB & BIN(11001111);
	PORTA = PORTA & BIN(11111010);
_asm  /// @todo Remove when sdcc bug fixed
  BANKSEL _coilPosition
_endasm;
}
#pragma restore

#pragma save
#pragma nooverlay

// This will adjust the motor power on the half steps to
// get the same (lower) torque as on the full steps
// to get the smoothest running.
// See http://www.sxlist.com/techref/io/stepper/linistep/halfstep.htm
// Uncomment the setPower() lines when we have more memory.

void motor_click()
{
  byte cp;
#ifdef HALFSTEP
  cp = coilPosition;
#else
  cp = coilPosition << 1;
#endif
  switch(cp) {
  case 7:
    //setPower((stepPower >> 1) + (stepPower >> 3));
    RB5 = 1;
    RB4 = 0;
    RA2 = 1;
    RA0 = 0;
    break;
  case 6:
    //setPower(stepPower);
    RB5 = 1;
    RB4 = 0;
    RA2 = 0;
    RA0 = 0;
    break;    
  case 5:
    //setPower((stepPower >> 1) + (stepPower >> 3));
    RB5 = 1;
    RB4 = 0;
    RA2 = 0;
    RA0 = 1;
    break;     
  case 4:
    //setPower(stepPower);
    RB5 = 0;
    RB4 = 0;
    RA2 = 0;
    RA0 = 1;
    break;     
  case 3:
    //setPower((stepPower >> 1) + (stepPower >> 3));
    RB5 = 0;
    RB4 = 1;
    RA2 = 0;
    RA0 = 1;
    break;     
  case 2:
    //setPower(stepPower);
    RB5 = 0;
    RB4 = 1;
    RA2 = 0;
    RA0 = 0;
    break;    
  case 1:
    //setPower((stepPower >> 1) + (stepPower >> 3));
    RB5 = 0;
    RB4 = 1;
    RA2 = 1;
    RA0 = 0;
    break;    
  case 0:
    //setPower(stepPower);
    RB5 = 0;
    RB4 = 0;
    RA2 = 1;
    RA0 = 0;
    break;    
  }
_asm  /// @todo Remove when sdcc bug fixed
  BANKSEL _coilPosition
_endasm;
}
#pragma restore

#else

#ifdef HALFSTEP
byte stepValue()
{
  // The low bits are set to ensure pullups are enabled for those ports
  switch(coilPosition) {
  case 0:
    return BIN(00010000) | PULLUPS;
  case 1:
    return BIN(00110000) | PULLUPS;
  case 2:
    return BIN(00100000) | PULLUPS;
  case 3:
    return BIN(01100000) | PULLUPS;
  case 4:
    return BIN(01000000) | PULLUPS;
  case 5:
    return BIN(11000000) | PULLUPS;
  case 6:
    return BIN(10000000) | PULLUPS;
  case 7:
    return BIN(10010000) | PULLUPS;
  }
  return 0;
}
#else
byte stepValue()
{
  // The low bits are set to ensure pullups are enabled for those ports
  switch(coilPosition) {
  case 0:
    return BIN(00010000) | PULLUPS;
  case 1:
    return BIN(00100000) | PULLUPS;
  case 2:
    return BIN(01000000) | PULLUPS;
  case 3:
    return BIN(10000000) | PULLUPS;
  }
  return 0;
}
#endif

#pragma save
#pragma nooverlay
void motor_stop()
{
	PORTB = PULLUPS;
_asm  /// @todo Remove when sdcc bug fixed
  BANKSEL _coilPosition
_endasm;
}
#pragma restore

#pragma save
#pragma nooverlay
void motor_click()
{
    PORTB = stepValue();
_asm  /// @todo Remove when sdcc bug fixed
  BANKSEL _coilPosition
_endasm;
}
#pragma restore

#endif



#pragma save
#pragma nooverlay

void forward1()
{
  flashLED();
  if (MAXSENSOR) {
    // We hit the end so go idle
    motor_stop();
    function = func_idle;
  } else {
    currentPosition.ival++;
    coilPosition = (coilPosition + 1) & (stepCount - 1);
    motor_click();
  }
_asm  /// @todo Remove when sdcc bug fixed
  BANKSEL _coilPosition
_endasm;
}
#pragma restore

#pragma save
#pragma nooverlay
void reverse1()
{
  flashLED();
  if (MINSENSOR) {
    // We hit the end so go idle
    motor_stop();
    function = func_idle;
  } else {
    currentPosition.ival--;
    coilPosition = (coilPosition + stepCount - 1) & (stepCount - 1);
    motor_click();
  }
_asm  /// @todo Remove when sdcc bug fixed
  BANKSEL _coilPosition;
_endasm;
}
#pragma restore

#pragma save
#pragma nooverlay
void setTimer(byte newspeed)
{
  speed = newspeed;
  if (speed) {
    TMR1ON = 0;  //TMR1H, TMR1L should only be set, when TMR1ON is off
    TMR1H = speed;
    TMR1L = 0;
    TMR1ON = 1;
  } else {
    TMR1ON = 0;
  }
_asm  /// @todo Remove when sdcc bug fixed
  BANKSEL _coilPosition
_endasm;
}
#pragma restore

#pragma save
#pragma nooverlay
void strobe_sync() {
  byte delay;
  
  SYNCA = 0; // Pull low
  SYNCA_TRIS = 0; // Set to output during stobe

  // Spin for a few cycles
  for(delay = 0; delay <= 254; delay++)
    ;

  SYNCA_TRIS = 1; // Back to input so we don't drive the sync line
_asm  /// @todo Remove when sdcc bug fixed
  BANKSEL _coilPosition
_endasm;
}
#pragma restore


#pragma save
#pragma nooverlay
// Perform a single DDA step
static void dda_step()
{
  if (currentPosition.ival == seekPosition.ival) {
    function = func_idle;
  } else if (currentPosition.ival < seekPosition.ival) {
    forward1();
  } else {
    reverse1();
  }
  if (function == func_idle && seekNotify != 255) {
    if (sendMessageISR(seekNotify)) { 
      sendDataByteISR(CMD_DDA);
      sendDataByteISR(currentPosition.bytes[0]);
      sendDataByteISR(currentPosition.bytes[1]);
      endMessageISR();
    } else {
      //sending is not possible, call this function again!
      function = func_ddamaster;
    }
  } else if (function != func_idle) {
    dda_error += dda_deltay.ival;
    if ((dda_error + dda_error) > dda_deltax) {
      // Y needs to be stepped, so signal
      strobe_sync();
      dda_error -= dda_deltax;
    }
  }
}
#pragma restore

#pragma save
#pragma nooverlay
// if speed is set to 0, then TMR1ON should also be set to 0, therefore
//   set function to func_idle and then next round both will be set to 0
void timerTick()
{
  switch(function) {
  case func_idle:
    TMR1ON = 0;
    speed = 0;
    LEDon();
    break;
  case func_forward:
    forward1();
    break;
  case func_reverse:
    reverse1();
    break;
  case func_seek:
    if (currentPosition.ival < seekPosition.ival) {
      forward1();
    } else if (currentPosition.ival > seekPosition.ival) {
      reverse1();
    } else {
      // Reached
      LEDon();
      // Uncomment next line to remove torque on arrival
      //motor_stop();
      function=func_idle;
    }
    if (function == func_idle && seekNotify != 255) {
      if (sendMessageISR(seekNotify)) {
        sendDataByteISR(CMD_SEEK);
        sendDataByteISR(currentPosition.bytes[0]);
        sendDataByteISR(currentPosition.bytes[1]);
        endMessageISR();
      } else {
        //sending is not possible, call this function again!
        function=func_seek;
      }
    }
    break;
  case func_findmin:
    if (MINSENSOR) {
      currentPosition.bytes[0] = 0;
      currentPosition.bytes[1] = 0;
      function = func_findmax;
    } else {
      reverse1();
    }
    break;
  case func_findmax:
    if (MAXSENSOR) {
      maxPosition.bytes[0] = currentPosition.bytes[0];
      maxPosition.bytes[1] = currentPosition.bytes[1];
      function = func_idle;
    } else {
      forward1();
    }
    if (function == func_idle && seekNotify != 255) {
      if (sendMessageISR(seekNotify)) {
        sendDataByteISR(CMD_CALIBRATE);
        sendDataByteISR(currentPosition.bytes[0]);
        sendDataByteISR(currentPosition.bytes[1]);
        endMessageISR();
      } else {
        //sending is not possible, call this function again!
        function = func_findmax;
      }
    }
    break;
  case func_syncwait:
    // Do nothing, we're still waiting
    break;
  case func_ddamaster:
    dda_step();
    break;
  case func_homereset:
    if (MINSENSOR) {
      currentPosition.bytes[0] = 0;
      currentPosition.bytes[1] = 0;
      function = func_idle;
    } else {
      reverse1();
    }
    if (function == func_idle && seekNotify != 255) {
      if (sendMessageISR(seekNotify)) {
        sendDataByteISR(CMD_HOMERESET);
        endMessageISR();
      } else {
        //sending is not possible, call this function again!
        function = func_homereset;
      }
    }      
    break;
  }
  setTimer(speed);
_asm  /// @todo Remove when sdcc bug fixed
  BANKSEL _coilPosition
_endasm;
}
#pragma restore

/// Called when the sync line is strobed (pulled briefly low)
void syncStrobe() {
  switch(sync_mode) {
  case sync_none:
    break;
  case sync_seek:
    if (function = func_syncwait) {
      sync_mode = sync_none;
      function = func_seek;
    }
    break;
  case sync_inc:
    forward1();
    break;
  case sync_dec:
    reverse1();
    break;
  }
}

void setPower(byte p)
{
    // This is a value from 0 to 63 (6 bits)
    // The low two bits are stored in CCP1CON, and the remaining
    // four bits in CCPR1L
    CCPR1L = p >> 2;
    PR2 = 16;  // The maximum range
    // Store low 2 bits and turn on PWM
    CCP1CON = BIN(1100) | ((p & BIN(11)) << 4);
}

void processCommand()
{
  switch(buffer[0]) {
  case CMD_VERSION:
    sendReply();
    sendDataByte(CMD_VERSION);  // Response type 0
    sendDataByte(MAJOR_VERSION_NUMBER);
    sendDataByte(MINOR_VERSION_NUMBER);
    endMessage();
    break;

  case CMD_CHECKHOSTVERSION:
    sendReply();
    sendDataByte(CMD_CHECKHOSTVERSION);    
    if(buffer[1] > OLDHOST_MAJOR_VERSION_NUMBER)
       sendDataByte(0xff);    
    else if (buffer[1] == OLDHOST_MAJOR_VERSION_NUMBER)
    {
       if (buffer[2] >= OLDHOST_MINOR_VERSION_NUMBER)
          sendDataByte(0xff);
       else
          sendDataByte(0);    
    } else
       sendDataByte(0);
    sendDataByte(OLDHOST_MAJOR_VERSION_NUMBER);
    sendDataByte(OLDHOST_MINOR_VERSION_NUMBER);
    endMessage();
    break;    

  case CMD_GETMODULETYPE:
    sendReply();
    sendDataByte(CMD_GETMODULETYPE);
    sendDataByte(LINEAR_AXIS_TYPE);
    endMessage();
    break;

  case CMD_FORWARD:
    // Forward speed
    function = func_forward;
    setTimer(buffer[1]);
    break;

  case CMD_REVERSE:
    // Reverse speed
    function = func_reverse;
    setTimer(buffer[1]);    
    break;

  case CMD_SETPOS:
    // Set (reset) position counter
    currentPosition.bytes[0] = buffer[1];
    currentPosition.bytes[1] = buffer[2];
    break;

  case CMD_GETPOS:
    // Get position counter
    sendReply();
    sendDataByte(CMD_GETPOS);
    sendDataByte(currentPosition.bytes[0]);
    sendDataByte(currentPosition.bytes[1]);
    endMessage();
    break;

  case CMD_SEEK:
    // Goto position
    seekPosition.bytes[0] = buffer[2];
    seekPosition.bytes[1] = buffer[3];

    if (sync_mode == sync_seek)
      function = func_syncwait;
    else
      function = func_seek;
    setTimer(buffer[1]);
    break;

  case CMD_FREE:
    // Free motor (release torque)
    motor_stop();
    function = func_idle;
    break;

  case CMD_NOTIFY:
    // Set seek completion (and calibration) notification
    seekNotify = buffer[1];
    break;

  case CMD_SYNC:
    // Set sync mode
    sync_mode = buffer[1];
    break;

  case CMD_CALIBRATE:
    // Request calibration (search at given speed)
    function = func_findmin;
    setTimer(buffer[1]);
    break;

  case CMD_GETRANGE:
    // Request range
    sendReply();
    sendDataByte(CMD_GETRANGE);
    sendDataByte(maxPosition.bytes[0]);
    sendDataByte(maxPosition.bytes[1]);
    endMessage();
    break;

  case CMD_DDA:
    // Master a DDA
    // Assumes head is already positioned correctly at x0 and extrusion
    // is starting

    seekPosition.bytes[0] = buffer[2];
    seekPosition.bytes[1] = buffer[3];
    dda_deltay.bytes[0] = buffer[4];
    dda_deltay.bytes[1] = buffer[5];
    dda_error = 0;

    dda_deltax = seekPosition.ival - currentPosition.ival;
    if (dda_deltax < 0) dda_deltax = -dda_deltax;

    function = func_ddamaster;
    setTimer(buffer[1]);
    break;

  case CMD_FORWARD1:
    forward1();
    break;

  case CMD_BACKWARD1:
    reverse1();
    break;

  case CMD_SETPOWER:
    // Uncomment these when we have more memory
    //stepPower = buffer[1];
    //setPower(stepPower);
    setPower(buffer[1]);
    break;

  case CMD_GETSENSOR:
    sendReply();
    sendDataByte(CMD_GETSENSOR);
    sendDataByte(PORTA);
    sendDataByte(PORTB);
    endMessage();
    break;

  case CMD_HOMERESET:
    // Seek to home position and reset (search at given speed)
    function = func_homereset;
    setTimer(buffer[1]);
    break;

  }
_asm  /// @todo Remove when sdcc bug fixed
  BANKSEL _coilPosition
_endasm;
}