Stepper motor control class library

Dependents:   StepperMotor_HelloWorld

Components pages

Components pages are available for bipolar and unipolar motor libraries

A bipolar stepper motor driving pulse generator

A unipolar stepper motor driving pulse generator

Files at this revision

API Documentation at this revision

Comitter:
okano
Date:
Fri Nov 26 15:32:25 2010 +0000
Parent:
0:4beb37ae37ce
Child:
2:f14b5e7276b0
Commit message:
rev.0.51 : (a) fixed - set angle by negative big number problem fixed. (b) function set_pps() can take float number. (c) comment improved to have default number information. (d) and small improvements..

Changed in this revision

StepperMotor.cpp Show annotated file Show diff for this revision Revisions of this file
StepperMotor.h Show annotated file Show diff for this revision Revisions of this file
--- a/StepperMotor.cpp	Thu Nov 25 11:11:51 2010 +0000
+++ b/StepperMotor.cpp	Fri Nov 26 15:32:25 2010 +0000
@@ -1,3 +1,15 @@
+/** Stepper Motor control library
+ *  
+ *  @class   StepperMotor
+ *  @author  Tedd OKANO
+ *  @version 0.51(27-Nov-2010)
+ *  
+ *  Copyright: 2010 Tedd OKANO, Tsukimidai Communications Syndicate - Crawl Design
+ *  The library that controls stepper motor via motor driver chip: TA7774
+ *  The TA7774 is a driver for a bipolar stepper motor. 
+ *  With this library, mbed will generate 2 phase pulses to operate the motor. 
+ */
+ 
 #include "mbed.h"
 #include "StepperMotor.h"
 
@@ -25,8 +37,8 @@
     t.attach( this, &StepperMotor::motor_maintain, 1.0 / (float)pps );
 }
 
-int StepperMotor::set_pps( int v ) {
-    int     p;
+float StepperMotor::set_pps( float v ) {
+    float   p;
 
     p   = pps;
     pps = v;
@@ -35,14 +47,14 @@
     return ( p );
 }
 
-void StepperMotor::set_max_pps( int v ) {
+void StepperMotor::set_max_pps( float v ) {
     max_pps     = v;
 }
 
 int StepperMotor::find_home_position( PositionDetectPorarity edge ) {
     RotMode     prev_rot;
     SyncMode    prev_sync;
-    int         prev_pps;
+    float       prev_pps;
     int         prev_det_pin;
     int         direction;
 
@@ -131,8 +143,9 @@
 }
 
 void StepperMotor::set_target_pos( int p ) {
-    target_pos  = (p + max_pos) % max_pos;
-
+    p           = p % max_pos;
+    target_pos  = (p < 0) ? (p + max_pos) : p;
+     
     if (sync_mode == SYNCHRONOUS)
         while ( distance() )
             wait( 0 );
--- a/StepperMotor.h	Thu Nov 25 11:11:51 2010 +0000
+++ b/StepperMotor.h	Fri Nov 26 15:32:25 2010 +0000
@@ -1,5 +1,9 @@
 /** Stepper Motor control library
  *  
+ *  @class   StepperMotor
+ *  @author  Tedd OKANO
+ *  @version 0.51(27-Nov-2010)
+ *  
  *  Copyright: 2010 Tedd OKANO, Tsukimidai Communications Syndicate - Crawl Design
  *  The library that controls stepper motor via motor driver chip: TA7774
  *  The TA7774 is a driver for a bipolar stepper motor. 
@@ -11,7 +15,7 @@
 
 #include "mbed.h"
 
-#define MAX_PPS   50   //  pulse per second
+#define MAX_PPS   100   //  pulse per second
 
 /** Stepper Motor control class
  *
@@ -23,6 +27,7 @@
  *  StepperMotor m( p21, p22, p23, p24 );
  *
  *  int main() {
+ *      m.set_steps_per_rotate( 480 );
  *      m.set_sync_mode( StepperMotor::SYNCHRONOUS );
  *      m.set_power_ctrl( true );
  *
@@ -88,15 +93,15 @@
 
     /** Set the pulse width (i.e. motor turning speed)
      *
-     *  @param v pulse per second : default is 100. lower number makes the turn slower
+     *  @param v pulse per second : lower number makes the turn slower (default = 100)
      */
-    int set_pps( int v );
+    float set_pps( float v );
 
     /** Set maximum PPS (= minimum pulse width) which will be used in finding home position
      *
-     *  @param v maximum pulse per second : default is 100. lower number makes the turn slower
+     *  @param v maximum pulse per second : lower number makes the turn slower (default = 100)
      */
-    void set_max_pps( int v );
+    void set_max_pps( float v );
 
     /** Find home position: rotate the motor until the detection edge comes.
      *
@@ -125,7 +130,7 @@
      */     
     int go_position( int v );
 
-    /** Turn the motor to defined position (by angle (0.0..360 degree) from home position)
+    /** Turn the motor to defined position (by angle (degree)) from home position)
      *
      *  Make motor move to absolute position
      *
@@ -151,7 +156,7 @@
      *      ...
      *  @endcode
      *
-     *  @param m motor rotate mode : SHORTEST, NO_WRAPAROUND, CLOCKWISE_ONLY or COUNTER_CLOCKWISE_ONLY
+     *  @param m motor rotate mode : SHORTEST (default), NO_WRAPAROUND, CLOCKWISE_ONLY or COUNTER_CLOCKWISE_ONLY
      */
     void set_rot_mode( RotMode m );
 
@@ -165,7 +170,7 @@
      *      ...
      *  @endcode
      *
-     *  @param m motor rotate mode : ASYNCHRONOUS or SYNCHRONOUS
+     *  @param m motor rotate mode : ASYNCHRONOUS (default) or SYNCHRONOUS
      */
     void set_sync_mode( SyncMode m );
 
@@ -179,7 +184,7 @@
 
     /** Pause/Resume the motor action
      *
-     *  @param sw use "true" for pause, "false" for resume
+     *  @param sw use "true" for pause, "false" (default) for resume
      */
     void set_pause( int sw );
 
@@ -187,7 +192,7 @@
      *
      *  If the auto power control is enabled, the motor power will be turned-off when it stays same place
      *  
-     *  @param sw use "true" for pause, "false" for resume
+     *  @param sw use "true" for pause, "false" (default) for resume
      */
     void set_power_ctrl( int sw );
 
@@ -214,8 +219,8 @@
     int         current_pos;
     int         pos_offset;
     int         target_pos;
-    int         pps;
-    int         max_pps;
+    float       pps;
+    float       max_pps;
     int         init_done;
     int         pause;
     int         power_ctrl;