CQ_KIT_Ver1_5

Dependencies:   mbed RateLimiter BLDCmotorDriverCQ_KIT_Ver1_5

main.cpp

Committer:
kenya8787
Date:
23 months ago
Revision:
13:038b62c7ac17
Parent:
12:33614e1dc638

File content as of revision 13:038b62c7ac17:

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

// 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 <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()
{
float t = temperature;
//    if (temperature > 0.55f){
    if (temperature < 0.38f){
        printf("Overheating... Turning off now,temp %1.2f \n\r",t);
        M.setDutyCycle(0);
        M.coast(); 
    }
}

// Pin to check current on the X-NUCLEO-IHM07M1 board
AnalogIn current_u(P_CURR1),current_v(P_CURR2),current_w(P_CURR3);

void checkCurrent()
{

}

int main() {
    printf("Press 'w' to speed up, 's' to speed down\n\r");
    
    // 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
//    ticker.attach_us(checkCurrent,1000000); // current control interbal 10ms
//    ticker.attach(checkCurrent,1); // current control interbal 10ms
    
    while(true) { 

//        char c = getchar();
//        if((c == 'w') && (dc < 0.9f)) {
//            dc += 0.1f;
//            M.setDutyCycle(dc);
//        }
//        if((c == 's') && (dc > -0.9f)) {
//            dc -= 0.1f;
//            M.setDutyCycle(dc);
//        }
        AnalogIn SPEED(P_SPEED);
        float S;
        S = SPEED + 0.001f;
        if(S >0.999f) S = 0.999;
        dc = S;
        M.setDutyCycle(dc);
        
        float im[3];
        im[0] = current_u;
        im[1] = current_v;
        im[2] = current_w;

        printf("iu,iv,iw= %1.3f,%1.3f,%1.3f \n\r",im[0],im[1],im[2]);

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

    }   
}