Library to control 4 phase stepper motors using ULN2003. Default values for motor 28BYJ-48.

Dependents:   Lift_arm_control

Files at this revision

API Documentation at this revision

Comitter:
fbcosentino
Date:
Tue Mar 05 13:18:24 2019 +0000
Commit message:
Initial commit

Changed in this revision

ULN2003.cpp Show annotated file Show diff for this revision Revisions of this file
ULN2003.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ULN2003.cpp	Tue Mar 05 13:18:24 2019 +0000
@@ -0,0 +1,117 @@
+#include "ULN2003.h"
+
+ULN2003::ULN2003(PinName pinOut1, PinName pinOut2, PinName pinOut3, PinName pinOut4, int full_turn_steps): 
+        _out1(pinOut1), _out2(pinOut2), _out3(pinOut3), _out4(pinOut4) {
+    _pos = 0;
+    _full_turn_steps = full_turn_steps;
+    relax();
+}
+
+void ULN2003::_set_output() {
+    switch (_pos) {
+        case 0: {
+            _out1=1;
+            _out2=0;
+            _out3=0;
+            _out4=0;
+            break;
+        }
+        case 1: {
+            _out1=1;
+            _out2=1;
+            _out3=0;
+            _out4=0;
+            break;
+        }
+        case 2: {
+            _out1=0;
+            _out2=1;
+            _out3=0;
+            _out4=0;
+            break;
+        }
+        case 3: {
+            _out1=0;
+            _out2=1;
+            _out3=1;
+            _out4=0;
+            break;
+        }
+        case 4: {
+            _out1=0;
+            _out2=0;
+            _out3=1;
+            _out4=0;
+            break;
+        }
+        case 5: {
+            _out1=0;
+            _out2=0;
+            _out3=1;
+            _out4=1;
+            break;
+        }
+        case 6: {
+            _out1=0;
+            _out2=0;
+            _out3=0;
+            _out4=1;
+            break;
+        }
+        case 7: {
+            _out1=1;
+            _out2=0;
+            _out3=0;
+            _out4=1;
+            break;
+        }
+    }
+}
+
+void ULN2003::stepForward() {
+    _pos++;
+    if (_pos > 7) _pos = 0;
+    _set_output();
+}
+
+void ULN2003::stepReverse() {
+    _pos--;
+    if (_pos < 0) _pos = 7;
+    _set_output();
+}
+
+void ULN2003::moveForward(int steps, float speed) {
+    int delay_us_value = (1.0/speed)*1000000; // speed is in Hz = steps/sec
+    
+    for (int i=0; i<steps; i++) {
+        stepForward();
+        wait_us(delay_us_value);
+    }
+    relax();
+}
+
+void ULN2003::moveReverse(int steps, float speed) {
+    int delay_us_value = (1.0/speed)*1000000; // speed is in Hz = steps/sec
+    
+    for (int i=0; i<steps; i++) {
+        stepForward();
+        wait_us(delay_us_value);
+    }
+    relax();
+}
+
+void ULN2003::turnForward(float turns, float speed) {
+    moveForward(_full_turn_steps*turns, _full_turn_steps*speed);
+}
+
+void ULN2003::turnReverse(float turns, float speed) {
+    moveForward(_full_turn_steps*turns, _full_turn_steps*speed);
+}
+
+
+void ULN2003::relax() {
+    _out1 = 0;
+    _out2 = 0;
+    _out3 = 0;
+    _out4 = 0;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ULN2003.h	Tue Mar 05 13:18:24 2019 +0000
@@ -0,0 +1,98 @@
+/**
+ *  @file ULN2003.h
+ *  Library to control 4 phase stepper motors using ULN2003
+ *
+ *  @author Fernando Cosentino
+*/
+
+#ifndef __ULN2003
+#define __ULN2003
+
+#include "mbed.h"
+
+/**
+ *  ULN2003 Class
+ * 
+ *  @author Fernando Cosentino
+ */
+class ULN2003 {
+public:
+    /**
+     *  Creates an instance.
+     *
+     *  @param pinOut1 Pin connected to IN1 in ULN2003 board
+     *  @param pinOut2 Pin connected to IN2 in ULN2003 board
+     *  @param pinOut3 Pin connected to IN3 in ULN2003 board
+     *  @param pinOut4 Pin connected to IN4 in ULN2003 board
+     *  @param full_turn_steps Amount of steps for a complete turn (4096 for 28BYJ-48)
+     */
+    ULN2003(PinName pinOut1, PinName pinOut2, PinName pinOut3, PinName pinOut4, int full_turn_steps = 4096);
+    
+    /**
+     *  Moves the motor one step clockwise, and leaves the motor powered.
+     */
+    void stepForward();
+
+    /**
+     *  Moves the motor one step counter-clockwise, and leaves the motor powered.
+     */
+    void stepReverse();
+    
+    /**
+     *  Moves the motor a number of steps clockwise, in the given speed. Leaves
+     *  the motor in relaxed state (no power in any coil).
+     *
+     *  @param steps Number of steps to move
+     *  @param speed Speed in steps per second
+     *  @note 28BYJ-48 can't handle speeds higher than 833 steps per second
+     */
+    void moveForward(int steps, float speed = 500);
+
+    /**
+     *  Moves the motor a number of steps counter-clockwise, in the given speed. 
+     *  Leaves the motor in relaxed state (no power in any coil).
+     *
+     *  @param steps Number of steps to move
+     *  @param speed Speed in steps per second
+     *  @note 28BYJ-48 can't handle speeds higher than 833 steps per second
+     */
+    void moveReverse(int steps, float speed = 500);
+    
+    /**
+     *  Moves the motor a number of turns clockwise, in the given speed. 
+     *  Leaves the motor in relaxed state (no power in any coil).
+     *
+     *  @param turns Number of complete turns to move (can be fractional)
+     *  @param speed Speed in complete turns per second
+     *  @note 28BYJ-48 can't handle speeds higher than 0.2 turns per second
+     */
+    void turnForward(float turns, float speed = 0.2); 
+
+    /**
+     *  Moves the motor a number of turns counter-clockwise, in the given speed. 
+     *  Leaves the motor in relaxed state (no power in any coil).
+     *
+     *  @param turns Number of complete turns to move (can be fractional)
+     *  @param speed Speed in complete turns per second
+     *  @note 28BYJ-48 can't handle speeds higher than 0.2 turns per second
+     */
+    void turnReverse(float turns, float speed = 0.2); 
+    
+    
+    /**
+     *  Removes power from all coils, so no energy is spent and the motor is
+     *  free to be moved passively.
+     */
+     void relax();
+
+private:    
+    void _set_output();
+    DigitalOut _out1;
+    DigitalOut _out2;
+    DigitalOut _out3;
+    DigitalOut _out4;
+    int _pos;
+    int _full_turn_steps;
+};
+
+#endif