CQ_KIT_Ver1_5

Dependencies:   mbed RateLimiter BLDCmotorDriverCQ_KIT_Ver1_5

Committer:
avilei
Date:
Fri Oct 14 13:45:35 2016 +0000
Revision:
4:3f63eed73ad1
Parent:
3:4c71d3475814
Child:
5:3290e8857120
Call the right commutation method

Who changed what in which revision?

UserRevisionLine numberNew contents of line
avilei 2:4ae769d0b112 1 /* mbed Microcontroller Library
avilei 2:4ae769d0b112 2 * Copyright (c) 2006-2016 ARM Limited
avilei 2:4ae769d0b112 3 *
avilei 2:4ae769d0b112 4 * Licensed under the Apache License, Version 2.0 (the "License");
avilei 2:4ae769d0b112 5 * you may not use this file except in compliance with the License.
avilei 2:4ae769d0b112 6 * You may obtain a copy of the License at
avilei 2:4ae769d0b112 7 *
avilei 2:4ae769d0b112 8 * http://www.apache.org/licenses/LICENSE-2.0
avilei 2:4ae769d0b112 9 *
avilei 2:4ae769d0b112 10 * Unless required by applicable law or agreed to in writing, software
avilei 2:4ae769d0b112 11 * distributed under the License is distributed on an "AS IS" BASIS,
avilei 2:4ae769d0b112 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
avilei 2:4ae769d0b112 13 * See the License for the specific language governing permissions and
avilei 2:4ae769d0b112 14 * limitations under the License.
avilei 2:4ae769d0b112 15 */
avilei 2:4ae769d0b112 16
avilei 2:4ae769d0b112 17 /**
avilei 2:4ae769d0b112 18 ******************************************************************************
avilei 2:4ae769d0b112 19 * @file SPN7Driver.cpp
avilei 2:4ae769d0b112 20 * @author STMicroelectronics
avilei 2:4ae769d0b112 21 * @brief Implementation of SPN7Driver class
avilei 2:4ae769d0b112 22 ******************************************************************************
avilei 2:4ae769d0b112 23 * @copy
avilei 2:4ae769d0b112 24 *
avilei 2:4ae769d0b112 25 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
avilei 2:4ae769d0b112 26 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
avilei 2:4ae769d0b112 27 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
avilei 2:4ae769d0b112 28 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
avilei 2:4ae769d0b112 29 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
avilei 2:4ae769d0b112 30 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
avilei 2:4ae769d0b112 31 *
avilei 2:4ae769d0b112 32 * <h2><center>&copy; COPYRIGHT 2016 STMicroelectronics</center></h2>
avilei 2:4ae769d0b112 33 */
avilei 2:4ae769d0b112 34
avilei 2:4ae769d0b112 35 #include "mbed.h"
avilei 2:4ae769d0b112 36 #include "SPN7Driver.h"
avilei 2:4ae769d0b112 37
avilei 3:4c71d3475814 38 typedef enum {
avilei 3:4c71d3475814 39 ST_BLDC_LOW = 0,
avilei 3:4c71d3475814 40 ST_BLDC_HIGH,
avilei 3:4c71d3475814 41 ST_BLDC_OFF
avilei 3:4c71d3475814 42 } st_bldc_status_t;
avilei 3:4c71d3475814 43
avilei 2:4ae769d0b112 44 // FIXME: add doxygen
avilei 2:4ae769d0b112 45
avilei 2:4ae769d0b112 46 SPN7Driver::SPN7Driver(PinName pIN1, PinName pIN2, PinName pIN3,
avilei 2:4ae769d0b112 47 PinName pEN1, PinName pEN2, PinName pEN3,
avilei 2:4ae769d0b112 48 PinName pH1, PinName pH2, PinName pH3,
avilei 2:4ae769d0b112 49 PinName pFault) :
avilei 2:4ae769d0b112 50 BLDCmotorDriver(pIN1, pIN2, pIN3,
avilei 2:4ae769d0b112 51 pEN1, pEN2, pEN3,
avilei 2:4ae769d0b112 52 pH1, pH2, pH3,
avilei 2:4ae769d0b112 53 pFault)
avilei 2:4ae769d0b112 54 {
avilei 2:4ae769d0b112 55 // The BLDCmotorDriver class was implemented for half-bridge drivers
avilei 2:4ae769d0b112 56 // so the pin names may be misleading when referring to the L6230 chip.
avilei 3:4c71d3475814 57 // Get pointers to each input pin and call them IN[x] (logic input)
avilei 2:4ae769d0b112 58 // to be consistent with the terminology used in the L6230 documentation.
avilei 3:4c71d3475814 59 PwmOut* IN[3] = {&GH_A, &GH_B, &GH_C};
avilei 3:4c71d3475814 60
avilei 2:4ae769d0b112 61 // Set the switching period of the INx logic input pins (PWM driven)
avilei 3:4c71d3475814 62 for (int i = 0; i < 3; i++) {
avilei 3:4c71d3475814 63 IN[i]->period(switchingPeriod);
avilei 3:4c71d3475814 64 }
avilei 2:4ae769d0b112 65
avilei 2:4ae769d0b112 66 // Set the step commutation function (triggered by the Hall sensors)
avilei 2:4ae769d0b112 67 H1.rise(this, &SPN7Driver::commutation);
avilei 2:4ae769d0b112 68 H2.rise(this, &SPN7Driver::commutation);
avilei 2:4ae769d0b112 69 H3.rise(this, &SPN7Driver::commutation);
avilei 2:4ae769d0b112 70 H1.fall(this, &SPN7Driver::commutation);
avilei 2:4ae769d0b112 71 H2.fall(this, &SPN7Driver::commutation);
avilei 2:4ae769d0b112 72 H3.fall(this, &SPN7Driver::commutation);
avilei 2:4ae769d0b112 73 }
avilei 2:4ae769d0b112 74
avilei 4:3f63eed73ad1 75 void SPN7Driver::setDutyCycle(float dc) {
avilei 4:3f63eed73ad1 76 if (dc >= -1 && dc <= 1) {
avilei 4:3f63eed73ad1 77 ticker.attach(this, &SPN7Driver::commutation, sampleTime);
avilei 4:3f63eed73ad1 78 tempDutyCycle = dc;
avilei 4:3f63eed73ad1 79 } else {
avilei 4:3f63eed73ad1 80 coast();
avilei 4:3f63eed73ad1 81 }
avilei 4:3f63eed73ad1 82 }
avilei 4:3f63eed73ad1 83
avilei 2:4ae769d0b112 84 // 6-step phase commutation
avilei 3:4c71d3475814 85 void SPN7Driver::commutation()
avilei 3:4c71d3475814 86 {
avilei 3:4c71d3475814 87 // The BLDCmotorDriver class was implemented for half-bridge drivers
avilei 3:4c71d3475814 88 // so the pin names may be misleading when referring to the L6230 chip.
avilei 3:4c71d3475814 89 // Get pointers to each input pin and call them IN[x] (logic input)
avilei 3:4c71d3475814 90 // to be consistent with the terminology used in the L6230 documentation.
avilei 3:4c71d3475814 91 PwmOut* IN[3] = {&GH_A, &GH_B, &GH_C};
avilei 3:4c71d3475814 92 // Get pointers to each enable pin and call them ENx (enable channel)
avilei 3:4c71d3475814 93 // to be consistent with the terminology used in the L6230 documentation.
avilei 3:4c71d3475814 94 DigitalOut* EN[3] = {&GL_A, &GL_B, &GL_C};
avilei 3:4c71d3475814 95
avilei 2:4ae769d0b112 96 // 1--X--0--0--X--1
avilei 2:4ae769d0b112 97 // X--1--1--X--0--0
avilei 3:4c71d3475814 98 // 0--0--X--1--1--X
avilei 3:4c71d3475814 99 st_bldc_status_t tab[6][3] = {
avilei 3:4c71d3475814 100 {ST_BLDC_HIGH, ST_BLDC_OFF, ST_BLDC_LOW},
avilei 3:4c71d3475814 101 {ST_BLDC_OFF, ST_BLDC_HIGH, ST_BLDC_LOW},
avilei 3:4c71d3475814 102 {ST_BLDC_LOW, ST_BLDC_HIGH, ST_BLDC_OFF},
avilei 3:4c71d3475814 103 {ST_BLDC_LOW, ST_BLDC_OFF, ST_BLDC_HIGH},
avilei 3:4c71d3475814 104 {ST_BLDC_OFF, ST_BLDC_LOW, ST_BLDC_HIGH},
avilei 3:4c71d3475814 105 {ST_BLDC_HIGH, ST_BLDC_LOW, ST_BLDC_OFF},
avilei 3:4c71d3475814 106 };
avilei 2:4ae769d0b112 107
avilei 2:4ae769d0b112 108 dutyCycle = rl.out(tempDutyCycle);
avilei 2:4ae769d0b112 109 currentSector = getSector();
avilei 2:4ae769d0b112 110
avilei 3:4c71d3475814 111 if (dutyCycle == 0) {
avilei 3:4c71d3475814 112 // Stop the motor
avilei 2:4ae769d0b112 113 coast();
avilei 3:4c71d3475814 114 return;
avilei 2:4ae769d0b112 115 }
avilei 2:4ae769d0b112 116
avilei 3:4c71d3475814 117 // Move to next sector (i.e. commute phase)
avilei 3:4c71d3475814 118 if (dutyCycle > 0 ) {
avilei 3:4c71d3475814 119 // Move forward
avilei 3:4c71d3475814 120 currentSector++;
avilei 3:4c71d3475814 121 if(currentSector > 5) currentSector = 0;
avilei 3:4c71d3475814 122 } else {
avilei 3:4c71d3475814 123 // Move backward
avilei 3:4c71d3475814 124 currentSector--;
avilei 3:4c71d3475814 125 if(currentSector < 0) currentSector = 5;
avilei 3:4c71d3475814 126 }
avilei 3:4c71d3475814 127
avilei 3:4c71d3475814 128 // Get the absolute value of the duty cycle for the PWM
avilei 3:4c71d3475814 129 float d = (dutyCycle > 0) ? dutyCycle : -dutyCycle;
avilei 3:4c71d3475814 130
avilei 3:4c71d3475814 131 // Update the logic inputs and the enable pins
avilei 3:4c71d3475814 132 //printf("Commutation sector %d\n\r", currentSector);
avilei 3:4c71d3475814 133 for (int i = 0; i < 3; i++) {
avilei 3:4c71d3475814 134 *EN[i] = (tab[currentSector][i] == ST_BLDC_OFF) ? 0 : 1;
avilei 3:4c71d3475814 135 *IN[i] = (tab[currentSector][i] == ST_BLDC_HIGH) ? d : 0;
avilei 3:4c71d3475814 136 /*
avilei 3:4c71d3475814 137 int ena = (tab[currentSector][i] == ST_BLDC_OFF) ? 0 : 1;
avilei 3:4c71d3475814 138 float inp = (tab[currentSector][i] == ST_BLDC_HIGH) ? d : 0;
avilei 3:4c71d3475814 139 printf("EN[%d] = %d, IN[%d] = %f\n\r", i, ena, i, inp);
avilei 3:4c71d3475814 140 */
avilei 3:4c71d3475814 141 }
avilei 2:4ae769d0b112 142 }