Library to handle the X-NUCLEO-IHM06A1 Motor Control Expansion Board based on the STSPIN220 component.

Dependencies:   ST_INTERFACES

Dependents:   HelloWorld_IHM06A1

Fork of X-NUCLEO-IHM06A1 by ST Expansion SW Team

Motor Control Library

Library to handle the X-NUCLEO-IHM06A1 Motor Control Expansion Board based on the STSPIN220 component.

It features the:

  • Read and write of the device parameters; GPIO, PWM and IRQ configuration; microstepping, direction position, speed, acceleration, deceleration and torque controls
  • Automatic full-step switch management; high impedance or hold stop mode selection; enable and standby management
  • Fault interrupts handling (over current, short-circuit and over temperature)
  • Command locking until the device completes movement

The API allows to easily:

  • perform various positioning, moves and stops
  • get/set or monitor the motor positions
  • set the home position and mark another position
  • get/set the minimum and maximum speed
  • get the current speed
  • get/set the acceleration and deceleration
  • get/set the stop mode (hold, hiz or standby)
  • get/set the torque
  • get/set the torque boost
  • get/set the step mode (up to 1/256)

Platform compatibility

Compatible platforms have been tested with the configurations provided by the HelloWorld_IHM06A1 example.

Files at this revision

API Documentation at this revision

Comitter:
Davidroid
Date:
Fri Jul 28 13:28:00 2017 +0000
Parent:
4:265c30b9112a
Commit message:
Aligning to ARM mbed coding style.

Changed in this revision

Components/STSpin220/STSpin220.cpp Show annotated file Show diff for this revision Revisions of this file
Components/STSpin220/STSpin220.h Show annotated file Show diff for this revision Revisions of this file
diff -r 265c30b9112a -r fd1315beea32 Components/STSpin220/STSpin220.cpp
--- a/Components/STSpin220/STSpin220.cpp	Fri Mar 24 13:54:23 2017 +0000
+++ b/Components/STSpin220/STSpin220.cpp	Fri Jul 28 13:28:00 2017 +0000
@@ -400,11 +400,14 @@
     STSpin220_ExitDeviceFromStandby();
   }
   /* Deactivate motor if needed */
-  else if (devicePrm.motionState != INACTIVE)
-  { 
-    STSpin220_HardHiZ();
+  else
+  {
+    if (devicePrm.motionState != INACTIVE)
+    { 
+      STSpin220_HardHiZ();
+    }
   }
-  
+
   if (targetPosition > devicePrm.currentPosition)
   {
     devicePrm.stepsToTake = targetPosition -\
@@ -475,9 +478,12 @@
     STSpin220_ExitDeviceFromStandby();
   }
   /* Deactivate motor if needed */
-  else if (devicePrm.motionState != INACTIVE)
-  { 
-    STSpin220_HardHiZ();
+  else
+  {
+    if (devicePrm.motionState != INACTIVE)
+    { 
+      STSpin220_HardHiZ();
+    }
   }
   
   if (direction != BACKWARD)
@@ -618,9 +624,12 @@
     STSpin220_ExitDeviceFromStandby();
   }
   /* Deactivate motor if needed */
-  else if (devicePrm.motionState != INACTIVE)
+  else
   {
-    STSpin220_HardHiZ();
+    if (devicePrm.motionState != INACTIVE)
+    {
+      STSpin220_HardHiZ();
+    }
   }
   
   if (stepCount != 0) 
@@ -667,16 +676,19 @@
     STSpin220_ExitDeviceFromStandby();
   }
   /* Deactivate motor if needed */
-  else if (devicePrm.motionState != INACTIVE)
+  else
   {
-    STSpin220_HardHiZ();
+    if (devicePrm.motionState != INACTIVE)
+    {
+      STSpin220_HardHiZ();
+    }
   }
   
-    /* Direction setup */
-    STSpin220_SetDirection(direction);
-    devicePrm.commandExecuted = RUN_CMD;
-    /* Motor activation */
-    STSpin220_StartMovement(); 
+  /* Direction setup */
+  STSpin220_SetDirection(direction);
+  devicePrm.commandExecuted = RUN_CMD;
+  /* Motor activation */
+  STSpin220_StartMovement(); 
 }
 
 /******************************************************//**
@@ -735,10 +747,13 @@
     devicePrm.direction = dir;
     STSpin220_Board_SetDirectionGpio(dir);
   }
-  else if ((devicePrm.commandExecuted&RUN_CMD)!=0)
+  else
   {
-    devicePrm.commandExecuted=(deviceCommand_t)
-      (STSPIN220_DIR_CHANGE_BIT_MASK|devicePrm.commandExecuted);
+    if ((devicePrm.commandExecuted&RUN_CMD)!=0)
+    {
+      devicePrm.commandExecuted=(deviceCommand_t)
+        (STSPIN220_DIR_CHANGE_BIT_MASK|devicePrm.commandExecuted);
+    }
   }
 }
 
@@ -988,26 +1003,32 @@
         }
       }
     }
-    else if (((newSpeed <= devicePrm.torqueBoostSpeedThreshold) &&\
-              (devicePrm.stepModeLatched != (motorStepMode_t)STEP_MODE_FULL))||\
-             (((devicePrm.commandExecuted & STSPIN220_MOVE_BIT_MASK) == MOVE_CMD)&&\
-               ((devicePrm.stepsToTake-devicePrm.relativePos)<=\
-                (1<<devicePrm.stepModeLatched))))
+    else
     {
+      if (((newSpeed <= devicePrm.torqueBoostSpeedThreshold) &&\
+          (devicePrm.stepModeLatched != (motorStepMode_t)STEP_MODE_FULL))||\
+           (((devicePrm.commandExecuted & STSPIN220_MOVE_BIT_MASK) == MOVE_CMD)&&\
+            ((devicePrm.stepsToTake-devicePrm.relativePos)<=\
+             (1<<devicePrm.stepModeLatched))))
+      {
+        STSpin220_Board_UnsetFullStep();
+        devicePrm.stepMode = devicePrm.stepModeLatched;
+        devicePrm.accu <<= devicePrm.stepModeLatched;
+        newSpeed <<= devicePrm.stepModeLatched;
+      }
+    }
+  }
+  else
+  {
+    if (devicePrm.stepMode != devicePrm.stepModeLatched)
+    {
+      //torqueBoostEnable has just been disabled
       STSpin220_Board_UnsetFullStep();
       devicePrm.stepMode = devicePrm.stepModeLatched;
       devicePrm.accu <<= devicePrm.stepModeLatched;
       newSpeed <<= devicePrm.stepModeLatched;
     }
   }
-  else if (devicePrm.stepMode != devicePrm.stepModeLatched)
-  {
-    //torqueBoostEnable has just been disabled
-    STSpin220_Board_UnsetFullStep();
-    devicePrm.stepMode = devicePrm.stepModeLatched;
-    devicePrm.accu <<= devicePrm.stepModeLatched;
-    newSpeed <<= devicePrm.stepModeLatched;
-  }
   
   if (newSpeed < STSPIN220_MIN_STCK_FREQ)
   {
@@ -1020,7 +1041,6 @@
   
   devicePrm.speed = newSpeed;
   STSpin220_Board_TimStckSetFreq(newSpeed);
-
 }
 
 /******************************************************//**
@@ -1127,10 +1147,22 @@
 {
   devicePrm.motionState = STANDBY;
 
-  if (STSpin220_SetAcceleration(pInitDevicePrm->acceleration)==false) STSpin220_ErrorHandler(STSPIN220_ERROR_SET_ACCELERATION); 
-  if (STSpin220_SetDeceleration(pInitDevicePrm->deceleration)==false) STSpin220_ErrorHandler(STSPIN220_ERROR_SET_DECELERATION);
-  if (STSpin220_SetMaxSpeed(pInitDevicePrm->maxSpeed)==false) STSpin220_ErrorHandler(STSPIN220_ERROR_SET_MAX_SPEED);
-  if (STSpin220_SetMinSpeed(pInitDevicePrm->minSpeed)==false) STSpin220_ErrorHandler(STSPIN220_ERROR_SET_MIN_SPEED);
+  if (STSpin220_SetAcceleration(pInitDevicePrm->acceleration)==false)
+  {
+      STSpin220_ErrorHandler(STSPIN220_ERROR_SET_ACCELERATION);
+  }
+  if (STSpin220_SetDeceleration(pInitDevicePrm->deceleration)==false)
+  {
+      STSpin220_ErrorHandler(STSPIN220_ERROR_SET_DECELERATION);
+  }
+  if (STSpin220_SetMaxSpeed(pInitDevicePrm->maxSpeed)==false)
+  {
+      STSpin220_ErrorHandler(STSPIN220_ERROR_SET_MAX_SPEED);
+  }
+  if (STSpin220_SetMinSpeed(pInitDevicePrm->minSpeed)==false)
+  {
+      STSpin220_ErrorHandler(STSPIN220_ERROR_SET_MIN_SPEED);
+  }
 
   STSpin220_VrefPwmSetFreq(pInitDevicePrm->vrefPwmFreq);
   STSpin220_SetTorque(ACC_TORQUE,pInitDevicePrm->accelTorque);
@@ -1157,10 +1189,22 @@
 {
   devicePrm.motionState = STANDBY;
 
-  if (STSpin220_SetAcceleration(STSPIN220_CONF_PARAM_ACC)==false) STSpin220_ErrorHandler(STSPIN220_ERROR_SET_ACCELERATION);
-  if (STSpin220_SetDeceleration(STSPIN220_CONF_PARAM_DEC)==false) STSpin220_ErrorHandler(STSPIN220_ERROR_SET_DECELERATION);
-  if (STSpin220_SetMaxSpeed(STSPIN220_CONF_PARAM_RUNNING_SPEED)==false) STSpin220_ErrorHandler(STSPIN220_ERROR_SET_MAX_SPEED);
-  if (STSpin220_SetMinSpeed(STSPIN220_CONF_PARAM_MIN_SPEED)==false) STSpin220_ErrorHandler(STSPIN220_ERROR_SET_MIN_SPEED);
+  if (STSpin220_SetAcceleration(STSPIN220_CONF_PARAM_ACC)==false)
+  {
+      STSpin220_ErrorHandler(STSPIN220_ERROR_SET_ACCELERATION);
+  }
+  if (STSpin220_SetDeceleration(STSPIN220_CONF_PARAM_DEC)==false)
+  {
+      STSpin220_ErrorHandler(STSPIN220_ERROR_SET_DECELERATION);
+  }
+  if (STSpin220_SetMaxSpeed(STSPIN220_CONF_PARAM_RUNNING_SPEED)==false)
+  {
+      STSpin220_ErrorHandler(STSPIN220_ERROR_SET_MAX_SPEED);
+  }
+  if (STSpin220_SetMinSpeed(STSPIN220_CONF_PARAM_MIN_SPEED)==false)
+  {
+      STSpin220_ErrorHandler(STSPIN220_ERROR_SET_MIN_SPEED);
+  }
 
   STSpin220_VrefPwmSetFreq(STSPIN220_CONF_PARAM_REF_PWM_FREQUENCY);  
   STSpin220_SetTorque(ACC_TORQUE,STSPIN220_CONF_PARAM_ACC_TORQUE);
@@ -1358,39 +1402,45 @@
             /* Apply decelerating torque */
             STSpin220_ApplyTorque(DEC_TORQUE);
           }
-          else if ((speed>=(devicePrm.maxSpeed>>stepModeShift))||\
-                   ((devicePrm.commandExecuted==MOVE_CMD)&&(relPos >= endAccPos)))
-          {
-            devicePrm.motionState = STEADY;
-            /* Apply running torque */
-            STSpin220_ApplyTorque(RUN_TORQUE);
-          }
           else
           {
-            bool speedUpdated = false;
-            /* Go on accelerating */
-            if (speed==0) speed =1;
-            devicePrm.accu += acc / speed;
-            while (devicePrm.accu>=(0X10000L))
+            if ((speed>=(devicePrm.maxSpeed>>stepModeShift))||\
+                ((devicePrm.commandExecuted==MOVE_CMD)&&(relPos >= endAccPos)))
+            {
+              devicePrm.motionState = STEADY;
+              /* Apply running torque */
+              STSpin220_ApplyTorque(RUN_TORQUE);
+            }
+            else
             {
-              devicePrm.accu -= (0X10000L);
-              speed +=1;
-              speedUpdated = true;
-            }
+              bool speedUpdated = false;
+              /* Go on accelerating */
+              if (speed==0)
+              {
+                speed =1;
+              }
+              devicePrm.accu += acc / speed;
+              while (devicePrm.accu>=(0X10000L))
+              {
+                devicePrm.accu -= (0X10000L);
+                speed +=1;
+                speedUpdated = true;
+              }
           
-            if (speedUpdated)
-            {
-              if (speed>(devicePrm.maxSpeed>>stepModeShift))
+              if (speedUpdated)
               {
-                speed = devicePrm.maxSpeed>>stepModeShift;
-              }    
-              devicePrm.speed = speed;
-            }
+                if (speed>(devicePrm.maxSpeed>>stepModeShift))
+                {
+                  speed = devicePrm.maxSpeed>>stepModeShift;
+                }    
+                devicePrm.speed = speed;
+              }
             
-            if (devicePrm.updateTorque!=false)
-            {
-              /* Apply accelerating torque */
-              STSpin220_ApplyTorque(ACC_TORQUE);              
+              if (devicePrm.updateTorque!=false)
+              {
+                /* Apply accelerating torque */
+                STSpin220_ApplyTorque(ACC_TORQUE);              
+              }
             }
           }
           break;
@@ -1415,12 +1465,15 @@
           /* Apply decelerating torque */
           STSpin220_ApplyTorque(DEC_TORQUE);
         }
-        else if ((devicePrm.commandExecuted==RUN_CMD)&&(devicePrm.speed<maxSpeed))
+        else
         {
-          devicePrm.motionState = ACCELERATING;
-          devicePrm.accu = 0;
-          /* Apply accelerating torque */
-          STSpin220_ApplyTorque(ACC_TORQUE);
+          if ((devicePrm.commandExecuted==RUN_CMD)&&(devicePrm.speed<maxSpeed))
+          {
+            devicePrm.motionState = ACCELERATING;
+             devicePrm.accu = 0;
+            /* Apply accelerating torque */
+            STSpin220_ApplyTorque(ACC_TORQUE);
+          }
         }
         break;
       }
@@ -1437,8 +1490,14 @@
           if ((devicePrm.commandExecuted&STSPIN220_DIR_CHANGE_BIT_MASK)!=0)
           {
             devicePrm.commandExecuted=(deviceCommand_t)((~STSPIN220_DIR_CHANGE_BIT_MASK)&devicePrm.commandExecuted);
-            if (devicePrm.direction==BACKWARD) devicePrm.direction=FORWARD;
-            else devicePrm.direction=BACKWARD;
+            if (devicePrm.direction==BACKWARD)
+            {
+                devicePrm.direction=FORWARD;
+            }
+            else
+            {
+                devicePrm.direction=BACKWARD;
+            }
             STSpin220_Board_SetDirectionGpio(devicePrm.direction);
             if ((devicePrm.commandExecuted&STSPIN220_SOFT_STOP_BIT_MASK)==0)
             {
@@ -1462,44 +1521,50 @@
             STSpin220_HardHiZ();
           }
         }
-        else if ((devicePrm.commandExecuted==RUN_CMD)&&
-                 (speed<=(devicePrm.maxSpeed>>stepModeShift)))
-        {
-          devicePrm.motionState = STEADY;
-          /* Apply running torque */
-          STSpin220_ApplyTorque(RUN_TORQUE);
-        }
         else
         {
-          /* Go on decelerating */
-          if (speed>(devicePrm.minSpeed>>stepModeShift))
+          if ((devicePrm.commandExecuted==RUN_CMD)&&
+              (speed<=(devicePrm.maxSpeed>>stepModeShift)))
           {
-            bool speedUpdated = false;
-            if (speed==0) speed =1;
-            devicePrm.accu += dec / speed;
-            while (devicePrm.accu>=(0X10000L))
+            devicePrm.motionState = STEADY;
+            /* Apply running torque */
+            STSpin220_ApplyTorque(RUN_TORQUE);
+          }
+          else
+          {
+            /* Go on decelerating */
+            if (speed>(devicePrm.minSpeed>>stepModeShift))
             {
-              devicePrm.accu -= (0X10000L);
-              if (speed>1)
-              {  
-                speed -=1;
+              bool speedUpdated = false;
+              if (speed==0)
+              {
+                  speed =1;
               }
-              speedUpdated = true;
-            }
+              devicePrm.accu += dec / speed;
+              while (devicePrm.accu>=(0X10000L))
+              {
+                devicePrm.accu -= (0X10000L);
+                if (speed>1)
+                {  
+                  speed -=1;
+                }
+                speedUpdated = true;
+              }
           
-            if (speedUpdated)
-            {
-              if (speed<(devicePrm.minSpeed>>stepModeShift))
+              if (speedUpdated)
               {
-                speed = devicePrm.minSpeed>>stepModeShift;
-              }  
-              devicePrm.speed = speed;
-            }
+                if (speed<(devicePrm.minSpeed>>stepModeShift))
+                {
+                  speed = devicePrm.minSpeed>>stepModeShift;
+                }  
+                devicePrm.speed = speed;
+              }
             
-            if (devicePrm.updateTorque!=false)
-            {
-              /* Apply decelerating torque */
-              STSpin220_ApplyTorque(DEC_TORQUE);
+              if (devicePrm.updateTorque!=false)
+              {
+                /* Apply decelerating torque */
+                STSpin220_ApplyTorque(DEC_TORQUE);
+              }
             }
           }
         }
diff -r 265c30b9112a -r fd1315beea32 Components/STSpin220/STSpin220.h
--- a/Components/STSpin220/STSpin220.h	Fri Mar 24 13:54:23 2017 +0000
+++ b/Components/STSpin220/STSpin220.h	Fri Jul 28 13:28:00 2017 +0000
@@ -103,7 +103,9 @@
     {
         /* Checking stackability. */
         if (numberOfDevices!=0)
+        {
             error("Instantiation of the STSpin220 component failed: it can't be stacked on itself.\r\n");
+        }
 
         /* ACTION 4 ----------------------------------------------------------*
          * Initialize here the component's member variables, one variable per *
@@ -978,8 +980,14 @@
      */
     void STSpin220_Board_SetDirectionGpio(motorDir_t dir)
     {
-        if (dir==BACKWARD) direction_mode4 = 0;
-        else direction_mode4 = 1;
+        if (dir==BACKWARD)
+        {
+            direction_mode4 = 0;
+        }
+        else
+        {
+            direction_mode4 = 1;
+        }
     }
  
     /**
@@ -1040,9 +1048,21 @@
      */
     void STSpin220_Board_TimStckInit(bool check)
     {
-        if (monitor.is_connected()) monitor = 0;
-        if (check==false) stck_mode3 = 0;
-        else if (stck_mode3==1) STSpin220_ErrorHandler(STSPIN220_ERROR_STEP_CLOCK);
+        if (monitor.is_connected())
+        {
+            monitor = 0;
+        }
+        if (check==false)
+        {
+            stck_mode3 = 0;
+        }
+        else
+        {
+            if (stck_mode3==1)
+            {
+                STSpin220_ErrorHandler(STSPIN220_ERROR_STEP_CLOCK);
+            }
+        }
     }
 
     /**
@@ -1063,12 +1083,18 @@
 
     void STSpin220_Board_Monitor_Set(void)
     {
-        if (monitor.is_connected()) monitor = 1;
+        if (monitor.is_connected())
+        {
+            monitor = 1;
+        }
     }
     
     void STSpin220_Board_Monitor_Reset(void)
     {
-        if (monitor.is_connected()) monitor = 0;
+        if (monitor.is_connected())
+        {
+            monitor = 0;
+        }
     }
 
     /**