f334

Dependencies:   CANnucleo FastPWM3 mbed

Fork of Hobbyking_Cheetah_Compact by Ben Katz

main.cpp

Committer:
benkatz
Date:
2017-11-02
Revision:
23:db1263aae417
Parent:
22:c8a1f2071bb0

File content as of revision 23:db1263aae417:

/*
const unsigned int BOARDNUM = 0x2;

//const unsigned int a_id =                            

const unsigned int TX_ID = 0x0100;
    
const unsigned int cmd_ID = (BOARDNUM<<8) + 0x7;

*/

//#include "CANnucleo.h"
#include "mbed.h"
#include "PositionSensor.h"
#include "structs.h"
#include "foc.h"
#include "hw_setup.h"
#include "math_ops.h"
#include "current_controller_config.h"
#include "hw_config.h"
#include "motor_config.h"

GPIOStruct gpio;
ControllerStruct controller;
COMStruct com;


/*
CANnucleo::CAN          can(PB_8, PB_9);  // CAN Rx pin name, CAN Tx pin name
CANnucleo::CANMessage   rxMsg;
CANnucleo::CANMessage   txMsg;
*/
int                     ledState;
int                     counter = 0;
int canCmd = 1000;
volatile bool           msgAvailable = false;

//DigitalOut toggle(PA_0);
Ticker  loop;
/**
 * @brief   'CAN receive-complete' interrup handler.
 * @note    Called on arrival of new CAN message.
 *          Keep it as short as possible.
 * @param   
 * @retval  
 */
 /*
void onMsgReceived() {
    msgAvailable = true;
    //printf("ping\n\r");
}

void sendCMD(int TX_addr, int val){
    txMsg.clear();      //clear Tx message storage
    txMsg.id = TX_addr;
    txMsg << val;
    can.write(txMsg);
    //wait(.1);
    
    }
    
void readCAN(void){
    if(msgAvailable) { 
    msgAvailable = false;               // reset flag for next use
    can.read(rxMsg);                    // read message into Rx message storage
    // Filtering performed by software:           
    if(rxMsg.id == cmd_ID) {             // See comments in CAN.cpp for filtering performed by hardware
            rxMsg >> canCmd;  
            }             // extract first data item
        }
        }
    
void cancontroller(void){
    //printf("%d\n\r", canCmd);
    readCAN();
    //sendCMD(TX_ID, canCmd);
    
    //sendCMD(TX_ID+b_ID, b1);
    //sendCMD(TX_ID+c_ID, c1);
    }
    
*/

Serial pc(PA_2, PA_3);

//PositionSensorAM5147 spi(16384, 0 , NPP);   ///1  I really need an eeprom or something to store this....
//PositionSensorEncoder encoder(4096, 0, 21); 

int count = 0;
       

// Current Sampling IRQ
volatile float dtc1 = 0;
volatile float dtc2 = 0;
volatile float dtc3 = 0;
extern "C" void TIM1_UP_TIM16_IRQHandler(void) {
  if (TIM1->SR & TIM_SR_UIF ) {
        
        GPIOC->ODR ^= (1 << 4);
        
        ADC1->CR  |= ADC_CR_ADSTART;  
        volatile int eoc;
        while(!eoc){
            eoc = ADC1->ISR & ADC_ISR_EOC;
            }
        
        controller.adc1_raw = ADC1->DR;
        controller.adc2_raw = ADC2->DR;
        
        //spi.Sample();
        //controller.dtheta_mech = spi.GetMechVelocity();
        //controller.theta_elec = spi.GetElecPosition();
        
        commutate(&controller, &gpio, controller.theta_elec);   
        //TIM1->CCR1 = 0x708*(dtc3);
        //TIM1->CCR2 = 0x708*(dtc2);
        //TIM1->CCR3 = 0x708*(dtc1);
        GPIOC->ODR ^= (1 << 4);
        
        //gpio.pwm_u->write(1.0f);  //write duty cycles
        

         
      }
  TIM1->SR = 0x0; // reset the status register
}

       
void enter_torque_mode(void){
    
    TIM1->CR1 ^= TIM_CR1_UDIS; //enable interrupt
    controller.i_d_ref = 0;
    controller.i_q_ref = 0;
    reset_foc(&controller);
    gpio.enable->write(1);
    }
    
       
int main() {


    controller.v_bus = V_BUS;
    //spi.ZeroPosition();
    Init_All_HW(&gpio);

    wait(.1);
    //TIM1->CR1 |= TIM_CR1_UDIS;
    gpio.enable->write(1);
    gpio.pwm_u->write(1.0f);  //write duty cycles
    gpio.pwm_v->write(1.0f);
    gpio.pwm_w->write(1.0f);
    zero_current(&controller.adc1_offset, &controller.adc2_offset);
    //gpio.enable->write(0);
    reset_foc(&controller);
    
    //TIM1->CR1 ^= TIM_CR1_UDIS; //enable interrupt
    gpio.enable->write(1);
    gpio.pwm_u->write(1.0f - .05f);  //write duty cycles
    gpio.pwm_v->write(1.0f - .05f);
    gpio.pwm_w->write(1.0f - .1f);
    
    wait(.1);
    //loop.attach(&commutate, .000025);
    //can.frequency(1000000);                     // set bit rate to 1Mbps
    //can.attach(&onMsgReceived);                 // attach 'CAN receive-complete' interrupt handler
    //can.filter(0x020 << 25, 0xF0000004, CANAny, 0);
    
    pc.baud(921600);
    wait(.01);
    pc.printf("HobbyKing Cheetah v1.1\n\r");
    pc.printf("ADC1 Offset: %d    ADC2 Offset: %d", controller.adc1_offset, controller.adc2_offset);
    wait(.01);
    
    
    enter_torque_mode();
    //enter_zeroing_mode();
    
    GPIOC->ODR = 0;
    
    while(1) {

    }
}