Bläddra i källkod

Merge pull request #2470 from thinkyhead/patch_servo_move

Patch servos code for move
Scott Lahteine 10 år sedan
förälder
incheckning
9d1d590f43
4 ändrade filer med 40 tillägg och 33 borttagningar
  1. 2
    0
      Marlin/Conditionals.h
  2. 0
    2
      Marlin/Marlin_main.cpp
  3. 34
    27
      Marlin/servo.cpp
  4. 4
    4
      Marlin/servo.h

+ 2
- 0
Marlin/Conditionals.h Visa fil

@@ -279,6 +279,8 @@
279 279
     #define MAX_PROBE_Y (min(Y_MAX_POS, Y_MAX_POS + Y_PROBE_OFFSET_FROM_EXTRUDER))
280 280
   #endif
281 281
 
282
+  #define SERVO_LEVELING (defined(ENABLE_AUTO_BED_LEVELING) && defined(DEACTIVATE_SERVOS_AFTER_MOVE))
283
+
282 284
    /**
283 285
     * Sled Options
284 286
     */ 

+ 0
- 2
Marlin/Marlin_main.cpp Visa fil

@@ -36,8 +36,6 @@
36 36
   #endif
37 37
 #endif // ENABLE_AUTO_BED_LEVELING
38 38
 
39
-#define SERVO_LEVELING (defined(ENABLE_AUTO_BED_LEVELING) && defined(DEACTIVATE_SERVOS_AFTER_MOVE))
40
-
41 39
 #ifdef MESH_BED_LEVELING
42 40
   #include "mesh_bed_leveling.h"
43 41
 #endif

+ 34
- 27
Marlin/servo.cpp Visa fil

@@ -35,7 +35,7 @@
35 35
 
36 36
  write()     - Sets the servo angle in degrees.  (invalid angle that is valid as pulse in microseconds is treated as microseconds)
37 37
  writeMicroseconds() - Sets the servo pulse width in microseconds
38
- move(pin, angel) - Sequence of attach(pin), write(angel).
38
+ move(pin, angle) - Sequence of attach(pin), write(angle).
39 39
                     With DEACTIVATE_SERVOS_AFTER_MOVE it waits SERVO_DEACTIVATION_DELAY and detaches.
40 40
  read()      - Gets the last written servo pulse width as an angle between 0 and 180.
41 41
  readMicroseconds()   - Gets the last written servo pulse width in microseconds. (was read_us() in first release)
@@ -59,7 +59,7 @@
59 59
 
60 60
 //#define NBR_TIMERS        (MAX_SERVOS / SERVOS_PER_TIMER)
61 61
 
62
-static servo_t servos[MAX_SERVOS];                          // static array of servo structures
62
+static ServoInfo_t servo_info[MAX_SERVOS];                  // static array of servo info structures
63 63
 static volatile int8_t Channel[_Nbr_16timers ];             // counter for the servo being pulsed for each timer (or -1 if refresh interval)
64 64
 
65 65
 uint8_t ServoCount = 0;                                     // the total number of attached servos
@@ -69,7 +69,7 @@ uint8_t ServoCount = 0;                                     // the total number
69 69
 #define SERVO_INDEX_TO_TIMER(_servo_nbr) ((timer16_Sequence_t)(_servo_nbr / SERVOS_PER_TIMER)) // returns the timer controlling this servo
70 70
 #define SERVO_INDEX_TO_CHANNEL(_servo_nbr) (_servo_nbr % SERVOS_PER_TIMER)       // returns the index of the servo on this timer
71 71
 #define SERVO_INDEX(_timer,_channel)  ((_timer*SERVOS_PER_TIMER) + _channel)     // macro to access servo index by timer and channel
72
-#define SERVO(_timer,_channel)  (servos[SERVO_INDEX(_timer,_channel)])            // macro to access servo class by timer and channel
72
+#define SERVO(_timer,_channel)  (servo_info[SERVO_INDEX(_timer,_channel)])       // macro to access servo class by timer and channel
73 73
 
74 74
 #define SERVO_MIN() (MIN_PULSE_WIDTH - this->min * 4)  // minimum value in uS for this servo
75 75
 #define SERVO_MAX() (MAX_PULSE_WIDTH - this->max * 4)  // maximum value in uS for this servo
@@ -232,34 +232,37 @@ static boolean isTimerActive(timer16_Sequence_t timer) {
232 232
 Servo::Servo() {
233 233
   if ( ServoCount < MAX_SERVOS) {
234 234
     this->servoIndex = ServoCount++;                    // assign a servo index to this instance
235
-    servos[this->servoIndex].ticks = usToTicks(DEFAULT_PULSE_WIDTH);   // store default values  - 12 Aug 2009
235
+    servo_info[this->servoIndex].ticks = usToTicks(DEFAULT_PULSE_WIDTH);   // store default values  - 12 Aug 2009
236 236
   }
237 237
   else
238 238
     this->servoIndex = INVALID_SERVO;  // too many servos
239 239
 }
240 240
 
241
-uint8_t Servo::attach(int pin) {
241
+int8_t Servo::attach(int pin) {
242 242
   return this->attach(pin, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
243 243
 }
244 244
 
245
-uint8_t Servo::attach(int pin, int min, int max) {
246
-  if (this->servoIndex < MAX_SERVOS ) {
247
-    if(pin > 0)
248
-      servos[this->servoIndex].Pin.nbr = pin;
249
-    pinMode(servos[this->servoIndex].Pin.nbr, OUTPUT); // set servo pin to output
250
-    // todo min/max check: abs(min - MIN_PULSE_WIDTH) /4 < 128
251
-    this->min = (MIN_PULSE_WIDTH - min) / 4; //resolution of min/max is 4 uS
252
-    this->max = (MAX_PULSE_WIDTH - max) / 4;
253
-    // initialize the timer if it has not already been initialized
254
-    timer16_Sequence_t timer = SERVO_INDEX_TO_TIMER(servoIndex);
255
-    if (!isTimerActive(timer)) initISR(timer);
256
-    servos[this->servoIndex].Pin.isActive = true;  // this must be set after the check for isTimerActive
257
-  }
245
+int8_t Servo::attach(int pin, int min, int max) {
246
+
247
+  if (this->servoIndex >= MAX_SERVOS) return -1;
248
+
249
+  if (pin > 0) servo_info[this->servoIndex].Pin.nbr = pin;
250
+  pinMode(servo_info[this->servoIndex].Pin.nbr, OUTPUT); // set servo pin to output
251
+
252
+  // todo min/max check: abs(min - MIN_PULSE_WIDTH) /4 < 128
253
+  this->min = (MIN_PULSE_WIDTH - min) / 4; //resolution of min/max is 4 uS
254
+  this->max = (MAX_PULSE_WIDTH - max) / 4;
255
+
256
+  // initialize the timer if it has not already been initialized
257
+  timer16_Sequence_t timer = SERVO_INDEX_TO_TIMER(servoIndex);
258
+  if (!isTimerActive(timer)) initISR(timer);
259
+  servo_info[this->servoIndex].Pin.isActive = true;  // this must be set after the check for isTimerActive
260
+
258 261
   return this->servoIndex;
259 262
 }
260 263
 
261 264
 void Servo::detach() {
262
-  servos[this->servoIndex].Pin.isActive = false;
265
+  servo_info[this->servoIndex].Pin.isActive = false;
263 266
   timer16_Sequence_t timer = SERVO_INDEX_TO_TIMER(servoIndex);
264 267
   if (!isTimerActive(timer)) finISR(timer);
265 268
 }
@@ -287,7 +290,7 @@ void Servo::writeMicroseconds(int value) {
287 290
 
288 291
     uint8_t oldSREG = SREG;
289 292
     cli();
290
-    servos[channel].ticks = value;
293
+    servo_info[channel].ticks = value;
291 294
     SREG = oldSREG;
292 295
   }
293 296
 }
@@ -296,17 +299,21 @@ void Servo::writeMicroseconds(int value) {
296 299
 int Servo::read() { return map( this->readMicroseconds()+1, SERVO_MIN(), SERVO_MAX(), 0, 180); }
297 300
 
298 301
 int Servo::readMicroseconds() {
299
-  return (this->servoIndex == INVALID_SERVO) ? 0 : ticksToUs(servos[this->servoIndex].ticks) + TRIM_DURATION;
302
+  return (this->servoIndex == INVALID_SERVO) ? 0 : ticksToUs(servo_info[this->servoIndex].ticks) + TRIM_DURATION;
300 303
 }
301 304
 
302
-bool Servo::attached() { return servos[this->servoIndex].Pin.isActive; }
305
+bool Servo::attached() { return servo_info[this->servoIndex].Pin.isActive; }
303 306
 
304
-uint8_t Servo::move(int pin, int value) {
305
-  uint8_t ret;
306
-  ret = this->attach(pin);
307
-  if (ret) {
307
+int8_t Servo::move(int pin, int value) {
308
+  int8_t ret;
309
+  #if SERVO_LEVELING
310
+    ret = this->attach(pin);
311
+  #else
312
+    ret = this->servoIndex;
313
+  #endif
314
+  if (ret >= 0) {
308 315
     this->write(value);
309
-    #ifdef DEACTIVATE_SERVOS_AFTER_MOVE && (SERVO_DEACTIVATION_DELAY > 0)
316
+    #if SERVO_LEVELING
310 317
       delay(SERVO_DEACTIVATION_DELAY);
311 318
       this->detach();
312 319
     #endif

+ 4
- 4
Marlin/servo.h Visa fil

@@ -112,17 +112,17 @@ typedef struct {
112 112
 typedef struct {
113 113
   ServoPin_t Pin;
114 114
   unsigned int ticks;
115
-} servo_t;
115
+} ServoInfo_t;
116 116
 
117 117
 class Servo {
118 118
   public:
119 119
     Servo();
120
-    uint8_t attach(int pin);           // attach the given pin to the next free channel, sets pinMode, returns channel number or 0 if failure
121
-    uint8_t attach(int pin, int min, int max); // as above but also sets min and max values for writes.
120
+    int8_t attach(int pin);           // attach the given pin to the next free channel, set pinMode, return channel number (-1 on fail)
121
+    int8_t attach(int pin, int min, int max); // as above but also sets min and max values for writes.
122 122
     void detach();
123 123
     void write(int value);             // if value is < 200 it is treated as an angle, otherwise as pulse width in microseconds
124 124
     void writeMicroseconds(int value); // Write pulse width in microseconds
125
-    uint8_t move(int pin, int value);  // attach the given pin to the next free channel, sets pinMode, returns channel number or 0 if failure.
125
+    int8_t move(int pin, int value);  // attach the given pin to the next free channel, set pinMode, return channel number (-1 if attach fails)
126 126
                                        // if value is < 200 it is treated as an angle, otherwise as pulse width in microseconds.
127 127
                                        // if DEACTIVATE_SERVOS_AFTER_MOVE is defined waits SERVO_DEACTIVATION_DELAY, than detaches.
128 128
     int read();                        // returns current pulse width as an angle between 0 and 180 degrees

Laddar…
Avbryt
Spara