Library used to control ST Nucleo Evaluation board IHM04A1, based on L6206 motor control driver.

Dependencies:   ST_INTERFACES

Dependents:   HelloWorld_IHM04A1 RoboCane_Motore arm_dcmotor_can arm_linear_can_2 ... more

Fork of X_NUCLEO_IHM04A1 by ST Expansion SW Team

Motor Control Library

Library to handle the X-NUCLEO-IHM04A1 Motor Control Expansion Board based on the L6206 component.

It allows a complete management of the L6206 by providing a complete APIs.

The key features of the library are :

  • Configuration of the L6206 (bridges inputs and enabling signals, bridges paralleling)
  • FLAG interrupts handling (overcurrent and thermal alarms reporting)
  • Handling of up to two bidirectional Brush DC motors or up to 4 unidirectional Brush DC motors depending of the bridges paralleling configuration
  • Nucleo and expansion board configuration (GPIOs, PWMs, IRQs…)

To use the L6206 driver library, the user first has to call its initialization method which:

  • Setups the required GPIOs to handle the bridges enable pins, the FLAG interrupt which reports overcurrent detection or thermal protection.
  • Loads the driver parameters with initial values configured by the user or with their predefined values from “l6206_target_config.h”, in order to program the PWMs frequency of the bridges inputs, the bridges paralleling configuration, the number and the type (bidirectional or unidirectional) of brush DC motors.

Once the initialization is done, the user can modify the driver parameters by calling specific functions to change the bridges paralleling configuration, types and numbers of motors or the PWMs frequency.

The user can also write callback functions and attach them to:

  • The flag interrupt handler depending on the actions he wants to perform when an overcurrent or a thermal alarm is reported.
  • The Error handler which is called by the library when it reports an error.

Then, the user can drive the different brush DC motors by requesting to run in a specified direction and by changing the maximal speed. When a motor is requested to run, the corresponding bridge is automatically enabled.

A motion command can be stopped at any moment:

  • Either by a hard stop which immediately stops the motor.
  • Or by a hardHiz command which immediately stops the motor and disables the bridge which is used by the motor.

The library also provides functions to disable or enable the bridges independently from the run or stop commands.

Arduino Connector Compatibility Warning

Using the X-NUCLEO-IHM04A1 expansion board with the NUCLEO-F429ZI requires adopting the following patch:

  • to connect with a wire the PB_4 Nucleo pin to the IN1A expansion board pin;
  • to connect with a wire the PC_7 Nucleo pin to the IN2A expansion board pin;
  • to connect with a wire the PA_15 Nucleo pin to the IN1B expansion board pin;
  • to connect with a wire the PB_3 Nucleo pin to the IN2B expansion board pin.


Board configuration for HelloWorld_IHM04A1 example

/media/uploads/brdirais/drive4brushdcmotor_ihm04a1.png

Files at this revision

API Documentation at this revision

Comitter:
Davidroid
Date:
Thu Aug 03 16:32:32 2017 +0000
Parent:
5:51ad3c4b904c
Commit message:
Aligning to ARM mbed coding style.

Changed in this revision

Components/L6206/L6206.cpp Show annotated file Show diff for this revision Revisions of this file
Components/L6206/L6206.h Show annotated file Show diff for this revision Revisions of this file
diff -r 51ad3c4b904c -r d16ad1d58ea1 Components/L6206/L6206.cpp
--- a/Components/L6206/L6206.cpp	Fri Mar 24 13:08:23 2017 +0000
+++ b/Components/L6206/L6206.cpp	Thu Aug 03 16:32:32 2017 +0000
@@ -230,9 +230,12 @@
   {
     L6206_ErrorHandler(L6206_ERROR_1);
   }
-  else if (devicePrm.motionState[motorId] != INACTIVE)
+  else
   {
-    speed = devicePrm.speed[motorId];
+  	if (devicePrm.motionState[motorId] != INACTIVE)
+    {
+      speed = devicePrm.speed[motorId];
+    }
   }
   
   return (speed);
@@ -274,7 +277,7 @@
  **********************************************************/
 uint16_t L6206::L6206_GetMaxSpeed(uint8_t motorId)
 {
-	uint16_t speed = 0;
+  uint16_t speed = 0;
   if (motorId > l6206ArrayNbMaxMotorsByConfig[devicePrm.config])
   {
     L6206_ErrorHandler(L6206_ERROR_1);
@@ -309,7 +312,7 @@
  **********************************************************/
 void L6206::L6206_HardHiz(uint8_t motorId)
 {
-	if (motorId > l6206ArrayNbMaxMotorsByConfig[devicePrm.config])
+  if (motorId > l6206ArrayNbMaxMotorsByConfig[devicePrm.config])
   {
     L6206_ErrorHandler(L6206_ERROR_1);
   }
@@ -388,24 +391,27 @@
 	{
 		L6206_ErrorHandler(L6206_ERROR_1);
 	}
-	else if (devicePrm.motionState[motorId] != INACTIVE)
+	else
 	{
-		uint8_t bridgeInput;
+		if (devicePrm.motionState[motorId] != INACTIVE)
+		{
+			uint8_t bridgeInput;
     
-		/* Get bridge input of the corresponding motor */
-		bridgeInput = L6206_GetBridgeInputUsedByMotorId(motorId);
+			/* Get bridge input of the corresponding motor */
+			bridgeInput = L6206_GetBridgeInputUsedByMotorId(motorId);
     
-		/* Disable corresponding PWM */
-		L6206_Board_PwmStop(bridgeInput);
+			/* Disable corresponding PWM */
+			L6206_Board_PwmStop(bridgeInput);
 
-		/* for bidirectionnal motor, disable second PWM*/
-		if (L6206_IsBidirectionnalMotor(motorId))
-		{
-			bridgeInput = L6206_GetSecondBridgeInputUsedByMotorId(motorId);
-			L6206_Board_PwmStop(bridgeInput);
+			/* for bidirectionnal motor, disable second PWM*/
+			if (L6206_IsBidirectionnalMotor(motorId))
+			{
+				bridgeInput = L6206_GetSecondBridgeInputUsedByMotorId(motorId);
+				L6206_Board_PwmStop(bridgeInput);
+			}
+			/* Set inactive state */
+			devicePrm.motionState[motorId] = INACTIVE;
 		}
-		/* Set inactive state */
-		devicePrm.motionState[motorId] = INACTIVE;
 	}
 }
 
@@ -430,49 +436,52 @@
  **********************************************************/
 void L6206::L6206_Run(uint8_t motorId, motorDir_t direction)
 {
-	if (motorId > l6206ArrayNbMaxMotorsByConfig[devicePrm.config])
+  if (motorId > l6206ArrayNbMaxMotorsByConfig[devicePrm.config])
   {
     L6206_ErrorHandler(L6206_ERROR_1);
   }  
-  else if ((devicePrm.motionState[motorId] == INACTIVE) ||
-           (devicePrm.direction[motorId] != direction))
+  else
   {
-    uint8_t bridgeId;
-    uint8_t bridgeInput;
-    
-    /* Eventually deactivate motor */
-    if (devicePrm.motionState[motorId] != INACTIVE)
+    if ((devicePrm.motionState[motorId] == INACTIVE) ||
+        (devicePrm.direction[motorId] != direction))
     {
-      L6206_HardStop(motorId);
-    }
+      uint8_t bridgeId;
+      uint8_t bridgeInput;
+    
+      /* Eventually deactivate motor */
+      if (devicePrm.motionState[motorId] != INACTIVE)
+      {
+        L6206_HardStop(motorId);
+      }
    
-    /* Store new direction */
-    devicePrm.direction[motorId] = direction;
+      /* Store new direction */
+      devicePrm.direction[motorId] = direction;
     
-    /* Switch to steady state */
-    devicePrm.motionState[motorId] = STEADY;
+      /* Switch to steady state */
+      devicePrm.motionState[motorId] = STEADY;
     
-   /* Get bridge Id of the corresponding motor */
-    bridgeId = L6206_GetBridgeIdUsedByMotorId(motorId);
+     /* Get bridge Id of the corresponding motor */
+      bridgeId = L6206_GetBridgeIdUsedByMotorId(motorId);
         
-    /* Get bridge input of the corresponding motor */
-    bridgeInput = L6206_GetBridgeInputUsedByMotorId(motorId);
+      /* Get bridge input of the corresponding motor */
+      bridgeInput = L6206_GetBridgeInputUsedByMotorId(motorId);
     
-    /* Enable bridge */
-    L6206_EnableBridge(bridgeId);
+      /* Enable bridge */
+      L6206_EnableBridge(bridgeId);
     
-    /* Set PWM */
-    if (L6206_IsBidirectionnalMotor(motorId))
-    {
-      /* for bidirectionnal motor */
-      L6206_Board_PwmSetFreq(bridgeInput, devicePrm.pwmFreq[bridgeInput],(100 - devicePrm.speed[motorId]));
-      bridgeInput = L6206_GetSecondBridgeInputUsedByMotorId(motorId);
-      L6206_Board_PwmSetFreq(bridgeInput, devicePrm.pwmFreq[bridgeInput],100);
-    }
-    else
-    {
-      /* for unidirectionnal motor */
-      L6206_Board_PwmSetFreq(bridgeInput, devicePrm.pwmFreq[bridgeInput],devicePrm.speed[motorId]);
+      /* Set PWM */
+      if (L6206_IsBidirectionnalMotor(motorId))
+      {
+        /* for bidirectionnal motor */
+        L6206_Board_PwmSetFreq(bridgeInput, devicePrm.pwmFreq[bridgeInput],(100 - devicePrm.speed[motorId]));
+        bridgeInput = L6206_GetSecondBridgeInputUsedByMotorId(motorId);
+        L6206_Board_PwmSetFreq(bridgeInput, devicePrm.pwmFreq[bridgeInput],100);
+      }
+      else
+      {
+        /* for unidirectionnal motor */
+        L6206_Board_PwmSetFreq(bridgeInput, devicePrm.pwmFreq[bridgeInput],devicePrm.speed[motorId]);
+      }
     }
   }
 }
@@ -484,7 +493,7 @@
  **********************************************************/
 void L6206::L6206_SetDualFullBridgeConfig(uint8_t newConfig)
 {
-	devicePrm.config = (dualFullBridgeConfig_t)newConfig;
+  devicePrm.config = (dualFullBridgeConfig_t)newConfig;
 
   /* Start to reset all else if several inits are used successively */
   /* they will fail */
@@ -552,7 +561,7 @@
  **********************************************************/
 bool L6206::L6206_SetMaxSpeed(uint8_t motorId, uint16_t newMaxSpeed)
 {
-	bool cmdExecuted = FALSE;
+  bool cmdExecuted = FALSE;
 
   if (motorId > l6206ArrayNbMaxMotorsByConfig[devicePrm.config])
   {
@@ -593,7 +602,7 @@
  **********************************************************/
 void L6206::L6206_SetBridgeInputPwmFreq(uint8_t bridgeId, uint32_t newFreq)
 {
-	uint8_t loop;
+  uint8_t loop;
   
   if (newFreq > L6206_MAX_PWM_FREQ)
   {
@@ -616,8 +625,8 @@
       {
         if (bridgeInput !=  L6206_GetSecondBridgeInputUsedByMotorId(motorId))
         {
-        /* Set PWM frequency for bidirectionnal motor of the first bridge*/
-        L6206_Board_PwmSetFreq(bridgeInput, devicePrm.pwmFreq[bridgeInput],(100 - devicePrm.speed[motorId]));
+          /* Set PWM frequency for bidirectionnal motor of the first bridge*/
+          L6206_Board_PwmSetFreq(bridgeInput, devicePrm.pwmFreq[bridgeInput],(100 - devicePrm.speed[motorId]));
         }
         else
         {
@@ -659,7 +668,7 @@
  **********************************************************/
 void L6206::L6206_ErrorHandler(uint16_t error)
 {
-	if (errorHandlerCallback != 0)
+  if (errorHandlerCallback != 0)
   {
     (void) errorHandlerCallback(error);
   }
@@ -678,7 +687,7 @@
  **********************************************************/
 void L6206::L6206_FlagInterruptHandler(void)
 {
-	bool status;
+  bool status;
   
   status = L6206_GetBridgeStatus(BRIDGE_A);
   if (status != devicePrm.bridgeEnabled[BRIDGE_A])
@@ -777,14 +786,17 @@
       {
         motorId = 2;
       }
-      else if (bridgeInput == INPUT_1B) 
-      {
-        motorId = 1;
-      }
       else
       {
-        motorId = 0;
-      }      
+      	if (bridgeInput == INPUT_1B) 
+        {
+          motorId = 1;
+        }
+        else
+        {
+          motorId = 0;
+        }
+      }
       break;        
     case PARALLELING_NONE___2_UNDIR_MOTOR_BRIDGE_A__1_BIDIR_MOTOR_BRIDGE_B:
     case PARALLELING_IN1B_IN2B__2_UNDIR_MOTOR_BRIDGE_A__1_UNDIR_MOTOR_BRIDGE_B:      
@@ -792,32 +804,41 @@
       {
         motorId = 2;
       }
-      else if (bridgeInput == INPUT_2A) 
-      {
-        motorId = 1;
-      }
       else
       {
-        motorId = 0;
-      }      
+      	if (bridgeInput == INPUT_2A) 
+        {
+          motorId = 1;
+        }
+        else
+        {
+          motorId = 0;
+        }
+      }
       break;           
     case PARALLELING_NONE___2_UNDIR_MOTOR_BRIDGE_A__2_UNDIR_MOTOR_BRIDGE_B:
       if (bridgeInput == INPUT_2B) 
       {
         motorId = 3;
       }
-      else if (bridgeInput == INPUT_1B) 
-      {
-        motorId = 2;
-      }
-      else if (bridgeInput == INPUT_2A) 
-      {
-        motorId = 1;
-      }      
       else
       {
-        motorId = 0;
-      }      
+      	if (bridgeInput == INPUT_1B) 
+        {
+          motorId = 2;
+        }
+        else
+        {
+          if (bridgeInput == INPUT_2A) 
+          {
+            motorId = 1;
+          }
+          else
+          {
+            motorId = 0;
+          }
+        }
+      }
       break;           
     case PARALLELING_IN1A_IN1B__IN2A_IN2B__1_UNDIR_MOTOR_BRIDGE_1A__1_UNDIR_MOTOR_BRIDGE_2A:
       if ((bridgeInput == INPUT_2A) || (bridgeInput == INPUT_2B))
@@ -886,14 +907,16 @@
            bridgeInput = INPUT_2A;
          }
       }
-      else if  (motorId == 1)
-      { 
-        
-        bridgeInput = INPUT_1B;
-      }
       else
       {
-        bridgeInput = INPUT_2B;        
+      	if  (motorId == 1)
+        {
+          bridgeInput = INPUT_1B;
+        }
+        else
+        {
+          bridgeInput = INPUT_2B;        
+        }
       }
       break;        
     case PARALLELING_NONE___2_UNDIR_MOTOR_BRIDGE_A__1_BIDIR_MOTOR_BRIDGE_B:
@@ -901,20 +924,23 @@
       { 
         bridgeInput = INPUT_1A;
       }
-      else if (motorId == 1)
-      {   
-        bridgeInput = INPUT_2A;
-      }
-      else 
-      { 
-         if (devicePrm.direction[2] == FORWARD)
-         {
-           bridgeInput = INPUT_1B;
-         }
-         else
-         {
-           bridgeInput = INPUT_2B;
-         }        
+      else
+      {
+      	if (motorId == 1)
+        {
+          bridgeInput = INPUT_2A;
+        }
+        else 
+        {
+          if (devicePrm.direction[2] == FORWARD)
+          {
+            bridgeInput = INPUT_1B;
+           }
+           else
+           {
+             bridgeInput = INPUT_2B;
+           }
+        }
       }
       break;   
     case PARALLELING_NONE___2_UNDIR_MOTOR_BRIDGE_A__2_UNDIR_MOTOR_BRIDGE_B:
@@ -922,18 +948,24 @@
       { 
         bridgeInput = INPUT_1A;
       }
-      else if (motorId == 1)
-      {   
-        bridgeInput = INPUT_2A;
-      }
-      else if  (motorId == 2)
-      { 
-        bridgeInput = INPUT_1B;
-      }
       else
       {
-        bridgeInput = INPUT_2B;
-      }        
+          if (motorId == 1)
+	      {   
+	        bridgeInput = INPUT_2A;
+	      }
+	      else
+	      {
+			  if  (motorId == 2)
+		      { 
+		        bridgeInput = INPUT_1B;
+		      }
+		      else
+		      {
+		        bridgeInput = INPUT_2B;
+		      }        
+	      }
+      }
       break;     
     case PARALLELING_IN1A_IN2A__1_UNDIR_MOTOR_BRIDGE_A__1_BIDIR_MOTOR_BRIDGE_B:
       if  (motorId == 0)
@@ -942,7 +974,7 @@
       }
       else 
       {  
-         if (devicePrm.direction[1] == FORWARD)
+        if (devicePrm.direction[1] == FORWARD)
         {   
           bridgeInput = INPUT_1B;
         }
@@ -957,15 +989,18 @@
       { 
         bridgeInput = INPUT_1A;
       }
-      else if  (motorId == 1)
-      {  
-        bridgeInput = INPUT_1B;
+      else
+      {
+	      if  (motorId == 1)
+	      {  
+	        bridgeInput = INPUT_1B;
+	      }
+	      else
+	      {   
+	        bridgeInput = INPUT_2B;
+	      }
       }
-      else
-      {   
-        bridgeInput = INPUT_2B;
-      }
-      break;         
+      break;
     case PARALLELING_IN1B_IN2B__1_BIDIR_MOTOR_BRIDGE_A__1_UNDIR_MOTOR_BRIDGE_B:
       if  (motorId == 0)
       { 
@@ -988,15 +1023,18 @@
       { 
         bridgeInput = INPUT_1A;        
       }
-      else if  (motorId == 1)
-      { 
-        bridgeInput = INPUT_2A;        
-      }        
       else
       {
-        bridgeInput = INPUT_1B;        
+	      if  (motorId == 1)
+	      { 
+	        bridgeInput = INPUT_2A;        
+	      }        
+	      else
+	      {
+	        bridgeInput = INPUT_1B;        
+	      }
       }
-      break;      
+      break;
     case PARALLELING_IN1A_IN2A__IN1B_IN2B__1_UNDIR_MOTOR_BRIDGE_A__1_UNDIR_MOTOR_BRIDGE_B:
       if  (motorId == 0)
       { 
@@ -1009,13 +1047,13 @@
       break;      
     case PARALLELING_IN1A_IN2A__IN1B_IN2B__1_BIDIR_MOTOR:
       if (devicePrm.direction[0] == FORWARD)
-       {
-         bridgeInput = INPUT_1A;
-       }
-       else
-       {
-         bridgeInput = INPUT_1B;
-       }
+      {
+        bridgeInput = INPUT_1A;
+      }
+      else
+      {
+        bridgeInput = INPUT_1B;
+      }
       break;       
     case PARALLELING_IN1A_IN1B__IN2A_IN2B__1_UNDIR_MOTOR_BRIDGE_1A__1_UNDIR_MOTOR_BRIDGE_2A:
       if (motorId == 0)
@@ -1052,7 +1090,7 @@
  **********************************************************/
 uint8_t L6206::L6206_GetSecondBridgeInputUsedByMotorId(uint8_t motorId)
 {
-	uint8_t bridgeInput = 0xFF;
+  uint8_t bridgeInput = 0xFF;
   
   switch (devicePrm.config)
   {
@@ -1110,7 +1148,7 @@
     case PARALLELING_IN1A_IN2A__1_UNDIR_MOTOR_BRIDGE_A__1_BIDIR_MOTOR_BRIDGE_B:
       if  (motorId == 1)
       {  
-         if (devicePrm.direction[1] == FORWARD)
+        if (devicePrm.direction[1] == FORWARD)
         {   
           bridgeInput = INPUT_2B;
         }
@@ -1174,7 +1212,7 @@
  **********************************************************/
 bool L6206::L6206_IsBidirectionnalMotor(uint8_t motorId)
 {
-	bool isBiDir = FALSE;
+  bool isBiDir = FALSE;
 
   switch (devicePrm.config)
   {
@@ -1219,7 +1257,7 @@
  **********************************************************/
 void L6206::L6206_SetDeviceParamsToPredefinedValues(void)
 {
-	uint32_t i;
+  uint32_t i;
 
   memset(&devicePrm, 0, sizeof(devicePrm));
 
@@ -1255,27 +1293,4 @@
 	memcpy(&devicePrm, initDevicePrm, sizeof(devicePrm));
 }
 
- /**
-  * @}
-  */
-  
-  
-  
-  
-   /**
-  * @}
-  */
-  
-   /**
-  * @}
-  */
-  
-   /**
-  * @}
-  */
-
-
 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
-
-
-
diff -r 51ad3c4b904c -r d16ad1d58ea1 Components/L6206/L6206.h
--- a/Components/L6206/L6206.h	Fri Mar 24 13:08:23 2017 +0000
+++ b/Components/L6206/L6206.h	Thu Aug 03 16:32:32 2017 +0000
@@ -454,9 +454,13 @@
     virtual void disable_flag_irq(uint8_t bridgeId)
     {
     	if (bridgeId == BRIDGE_A)
+    	{
     		flag_A_irq.fall(NULL);
+		}
     	else
+    	{
     		flag_B_irq.fall(NULL);
+		}
     }
 
 
@@ -582,7 +586,8 @@
 	{
 		L6206_OutVal( bridgeId, 1);
 
-  		if (addDelay != 0) {
+  		if (addDelay != 0)
+  		{
 			wait_ms(BSP_MOTOR_CONTROL_BOARD_BRIDGE_TURN_ON_DELAY);
 		}
 
@@ -646,7 +651,10 @@
         float duty_cycle;
         int period_us = (int)(period * 1000000);
 
-        if (duty > 100)   duty = 100;
+        if (duty > 100)
+        {
+        	duty = 100;
+    	}
         duty_cycle = (float)duty / 100.0f;
 
         switch (bridgeInput)