CQ_KIT_Ver1_5

Dependencies:   mbed RateLimiter BLDCmotorDriverCQ_KIT_Ver1_5

X_NUCLEO_IHM07M1/SPN7Driver.cpp

Committer:
avilei
Date:
2016-10-13
Revision:
3:4c71d3475814
Parent:
2:4ae769d0b112
Child:
4:3f63eed73ad1

File content as of revision 3:4c71d3475814:

/* 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>
  */ 

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

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

// FIXME: add doxygen

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);    
}

// 6-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};

//      1--X--0--0--X--1
//      X--1--1--X--0--0
//      0--0--X--1--1--X    
    st_bldc_status_t tab[6][3] = {
                            {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},
                            {ST_BLDC_OFF, ST_BLDC_LOW, ST_BLDC_HIGH},
                            {ST_BLDC_HIGH, ST_BLDC_LOW, ST_BLDC_OFF},
                            };

    dutyCycle = rl.out(tempDutyCycle);
    currentSector = getSector();

    if (dutyCycle == 0) {
        // Stop the motor
        coast();
        return;
    }
    
    // Move to next sector (i.e. commute phase)
    if (dutyCycle > 0 ) {
         // Move forward
         currentSector++;
         if(currentSector > 5) currentSector = 0;
    } else {
        // Move backward
        currentSector--;
        if(currentSector < 0) currentSector = 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
    //printf("Commutation sector %d\n\r", currentSector);
    for (int i = 0; i < 3; i++) {
         *EN[i] = (tab[currentSector][i] == ST_BLDC_OFF) ? 0 : 1;
         *IN[i] = (tab[currentSector][i] == ST_BLDC_HIGH) ? d : 0;
/*
         int ena = (tab[currentSector][i] == ST_BLDC_OFF) ? 0 : 1;
         float inp = (tab[currentSector][i] == ST_BLDC_HIGH) ? d : 0;         
         printf("EN[%d] = %d, IN[%d] = %f\n\r", i, ena, i, inp);
*/
    }    
}