Created May 27, 2018

Fan te conoce bien

Un fan que conoce tus deseos

15
Fan te conoce bien

Things used in this project

Hardware components

STM32 Nucleo-144 Boards
STMicroelectronics STM32 Nucleo-144 Boards
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

MBED

Story

Read more

Code

código.c

C/C++
#include <stdio.h>
#include "mbed.h"
#include "RateLimiter.h"
#include "SPN7Driver.h"
// Pin definitions for X-NUCLEO-IHM070M1 with STM32 Nucleo boards
#include "x_nucleo_ihm07m1_targets.h"

// Instance of the motor driver
SPN7Driver M(
             P_IN1, P_IN2, P_IN3, // Logic input pins
             P_EN1, P_EN2, P_EN3, // Enable channel pins
             P_HALL1, P_HALL2, P_HALL3, // Hall sensors pins
             P_FAULT // Fault LED
             );
             
// Pin to check temperature on the X-NUCLEO-IHM07M1 board
AnalogIn temperature(P_TEMP);

void checkTemperature()
{
    if (temperature > 0.55f){
        printf("Overheating... Turning off now\n\r");
        M.setDutyCycle(0);
        M.coast(); 
    }
}

int main() {
float ca,avg;
int i;
 
while(1)
{
avg=0;
for(i=0;i<10;i++)
{
a=LM35.read();
avg=avg+(a/10);
wait(.02);
}

c=(avg*3.685503686*100);
wait(1);
    
    // Duty cycle value to be used for speed control
    // Positive for clockwise; negative for anti-clockwise
    float dc = 0.0f;
    
    Ticker ticker;
    ticker.attach(checkTemperature, 1); // Periodic overheating check
    
    while(true) { 
    
        if((c >25) && (dc < 0.9f)) {
            dc += 0.1f;
            M.setDutyCycle(dc);
        }
        if((c <20) && (dc > -0.9f)) {
            dc -= 0.1f;
            M.setDutyCycle(dc);
        }

        printf("Duty Cycle: %1.2f, Sector: %d\n\r",dc, M.getSector());

    }   
}

SPN7Driver.h

C Header File
/* mbed Microcontroller Library
* Copyright (c) 2006-2016 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
  ******************************************************************************
  * @file    SPN7Driver.h 
  * @author  STMicroelectronics
  * @brief   Header file for SPN7Driver based on BLDCmotorDriver class
  ******************************************************************************
  * @copy
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2016 STMicroelectronics</center></h2>
  */ 

// This example is based on the BLDCmotorDriver motor control library
// by the TVZ Mechatronics Team, University of Applied Sciences Zagreb,
// Professional Study in Mechatronics:
// https://developer.mbed.org/teams/TVZ-Mechatronics-Team/code/BLDCmotorDriver/

#ifndef __SPN7DRIVER_H__
#define __SPN7DRIVER_H__
  
#include "mbed.h"
#include "BLDCmotorDriver.h"

class SPN7Driver : public BLDCmotorDriver {
    public:
        SPN7Driver(PinName pIN1, PinName pIN2, PinName pIN3, 
                   PinName pEN1, PinName pEN2, PinName pEN3,
                   PinName pH1, PinName pH2, PinName pH3,
                   PinName pFault);
        void setDutyCycle(float dutyCycle);
                    
    protected:
        void commutation();
};
        
#endif // __SPN7DRIVER_H__

SPN7Driver.cpp

C/C++
/* mbed Microcontroller Library
* Copyright (c) 2006-2016 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
  ******************************************************************************
  * @file    SPN7Driver.cpp 
  * @author  STMicroelectronics
  * @brief   Implementation of SPN7Driver class
  ******************************************************************************
  * @copy
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2016 STMicroelectronics</center></h2>
  */ 

// This example is based on the BLDCmotorDriver motor control library
// by the TVZ Mechatronics Team, University of Applied Sciences Zagreb,
// Professional Study in Mechatronics:
// https://developer.mbed.org/teams/TVZ-Mechatronics-Team/code/BLDCmotorDriver/

#include "mbed.h"
#include "SPN7Driver.h"

typedef enum {
    ST_BLDC_LOW = 0,
    ST_BLDC_HIGH,
    ST_BLDC_OFF
} st_bldc_status_t;

/**************************************************************************/
/**
    @brief  Constructor
     * @param pIN1     Logic input pin IN1 of L6230 chip
     * @param pIN2     Logic input pin IN2 of L6230 chip
     * @param pIN3     Logic input pin IN3 of L6230 chip
     * @param pEN1     Enable channel pin EN1 of L6230 chip
     * @param pEN2     Enable channel pin EN2 of L6230 chip
     * @param pEN3     Enable channel pin EN3 of L6230 chip
     * @param pH1      Hall sensor pin for phase #1 (A) of X-NUCLEO-IHM07M1
     * @param pH2      Hall sensor pin for phase #2 (B) of X-NUCLEO-IHM07M1
     * @param pH3      Hall sensor pin for phase #3 (Z) of X-NUCLEO-IHM07M1
     * @param pFault   Fault LED pin of X-NUCLEO-IHM07M1
*/
/**************************************************************************/
SPN7Driver::SPN7Driver(PinName pIN1, PinName pIN2, PinName pIN3,
                       PinName pEN1, PinName pEN2, PinName pEN3,
                       PinName pH1,  PinName pH2,  PinName pH3,
                       PinName pFault) :
                        BLDCmotorDriver(pIN1, pIN2, pIN3,
                                        pEN1, pEN2, pEN3,
                                        pH1,  pH2,  pH3,
                                        pFault)
{
    // The BLDCmotorDriver class was implemented for half-bridge drivers
    // so the pin names may be misleading when referring to the L6230 chip.
    // Get pointers to each input pin and call them IN[x] (logic input)
    // to be consistent with the terminology used in the L6230 documentation.
    PwmOut* IN[3] = {&GH_A, &GH_B, &GH_C};

    // Set the switching period of the INx logic input pins (PWM driven)
    for (int i = 0; i < 3; i++) {
        IN[i]->period(switchingPeriod);
    }    
    
    // Set the step commutation function (triggered by the Hall sensors)
    H1.rise(this, &SPN7Driver::commutation);
    H2.rise(this, &SPN7Driver::commutation);
    H3.rise(this, &SPN7Driver::commutation);
    H1.fall(this, &SPN7Driver::commutation);
    H2.fall(this, &SPN7Driver::commutation);
    H3.fall(this, &SPN7Driver::commutation);    
}

/**************************************************************************/
/**
    @brief  Set duty cycle for motor control
     * @param dc       duty cycle value (>0 clockwise; <0 anti-clockwise)
*/
/**************************************************************************/
void SPN7Driver::setDutyCycle(float dc) {
    if (dc >= -1 && dc <= 1) {
        ticker.attach(this, &SPN7Driver::commutation, sampleTime);
        tempDutyCycle = dc;
    } else {
        coast();
    }
}

// six-step phase commutation
void SPN7Driver::commutation()
{    
    // The BLDCmotorDriver class was implemented for half-bridge drivers
    // so the pin names may be misleading when referring to the L6230 chip.
    // Get pointers to each input pin and call them IN[x] (logic input)
    // to be consistent with the terminology used in the L6230 documentation.
    PwmOut* IN[3] = {&GH_A, &GH_B, &GH_C};
    // Get pointers to each enable pin and call them ENx (enable channel)
    // to be consistent with the terminology used in the L6230 documentation.
    DigitalOut* EN[3] = {&GL_A, &GL_B, &GL_C};

    st_bldc_status_t tab[6][3] = {
                            {ST_BLDC_OFF, ST_BLDC_LOW, ST_BLDC_HIGH},
                            {ST_BLDC_HIGH, ST_BLDC_LOW, ST_BLDC_OFF},
                            {ST_BLDC_HIGH, ST_BLDC_OFF, ST_BLDC_LOW},
                            {ST_BLDC_OFF, ST_BLDC_HIGH, ST_BLDC_LOW},
                            {ST_BLDC_LOW, ST_BLDC_HIGH, ST_BLDC_OFF},
                            {ST_BLDC_LOW, ST_BLDC_OFF, ST_BLDC_HIGH},
                            };

    dutyCycle = rl.out(tempDutyCycle);
    int sector = getSector();

    if (dutyCycle == 0) {
        // Stop the motor
        coast();
        return;
    }
    
    // Move to next sector (i.e. commute phase)
    if (dutyCycle > 0 ) {
         // Clockwise spinning
         
         // The rows of the phase status table are offset by one when spinning
         // clockwise so we need to increment the current sector by two steps
         sector = (sector + 2) % 6;
    } else {
        // Anti-clockwise spinning
        sector--;
        if(sector < 0) sector = 5;
    }
    
    // Get the absolute value of the duty cycle for the PWM
    float d = (dutyCycle > 0) ? dutyCycle : -dutyCycle;
    
    // Update the logic inputs and the enable pins
    for (int i = 0; i < 3; i++) {
         *EN[i] = (tab[sector][i] == ST_BLDC_OFF) ? 0 : 1;
         *IN[i] = (tab[sector][i] == ST_BLDC_HIGH) ? d : 0;
    }    
}

Credits

Comments