Ben Katz / Hobbyking_Cheetah_Compact

Motor control for robots. More compact, less object-oriented revision.

Dependencies:   FastPWM3 mbed

Fork of Hobbyking_Cheetah_V1 by Ben Katz

Committer:
benkatz
Date:
Mon May 01 15:22:58 2017 +0000
Revision:
26:2b865c00d7e9
Parent:
22:60276ba87ac6
- Added CAN Send/Receive; - Updated to most recent MBED (Broke SPI 16-bit read/write.  Hacked to work with 2 8-bit writes now); - Removed most sine/cos calculations; - Fixed sign error in autocalibration routine

Who changed what in which revision?

UserRevisionLine numberNew contents of line
benkatz20:bf9ea5125d52 1
benkatz20:bf9ea5125d52 2#include "mbed.h"
benkatz20:bf9ea5125d52 3#include "hw_setup.h"
benkatz20:bf9ea5125d52 4#include "hw_config.h"
benkatz20:bf9ea5125d52 5#include "structs.h"
benkatz20:bf9ea5125d52 6#include "FastPWM.h"
benkatz20:bf9ea5125d52 7
benkatz20:bf9ea5125d52 8void Init_PWM(GPIOStruct *gpio){
benkatz20:bf9ea5125d52 9
benkatz22:60276ba87ac6 10 RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN; // enable the clock to GPIOC
benkatz22:60276ba87ac6 11 RCC->APB1ENR |= 0x00000001; // enable TIM2 clock
benkatz22:60276ba87ac6 12 RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; // enable TIM1 clock
benkatz20:bf9ea5125d52 13
benkatz22:60276ba87ac6 14 GPIOC->MODER |= (1 << 10); // set pin 5 to be general purpose output for LED
benkatz20:bf9ea5125d52 15 gpio->enable = new DigitalOut(ENABLE_PIN);
benkatz20:bf9ea5125d52 16 gpio->pwm_u = new FastPWM(PIN_U);
benkatz20:bf9ea5125d52 17 gpio->pwm_v = new FastPWM(PIN_V);
benkatz20:bf9ea5125d52 18 gpio->pwm_w = new FastPWM(PIN_W);
benkatz20:bf9ea5125d52 19
benkatz22:60276ba87ac6 20
benkatz22:60276ba87ac6 21
benkatz20:bf9ea5125d52 22 //ISR Setup
benkatz20:bf9ea5125d52 23
benkatz22:60276ba87ac6 24 NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn); //Enable TIM1 IRQ
benkatz20:bf9ea5125d52 25
benkatz22:60276ba87ac6 26 TIM1->DIER |= TIM_DIER_UIE; // enable update interrupt
benkatz22:60276ba87ac6 27 TIM1->CR1 = 0x40; // CMS = 10, interrupt only when counting up
benkatz20:bf9ea5125d52 28 TIM1->CR1 |= TIM_CR1_UDIS;
benkatz22:60276ba87ac6 29 TIM1->CR1 |= TIM_CR1_ARPE; // autoreload on,
benkatz22:60276ba87ac6 30 TIM1->RCR |= 0x001; // update event once per up/down count of tim1
benkatz20:bf9ea5125d52 31 TIM1->EGR |= TIM_EGR_UG;
benkatz20:bf9ea5125d52 32
benkatz20:bf9ea5125d52 33 //PWM Setup
benkatz20:bf9ea5125d52 34
benkatz22:60276ba87ac6 35 TIM1->PSC = 0x0; // no prescaler, timer counts up in sync with the peripheral clock
benkatz20:bf9ea5125d52 36 //TIM1->ARR = 0x1194; // 20 khz
benkatz22:60276ba87ac6 37 TIM1->ARR = 0x8CA; // set auto reload, 40 khz
benkatz22:60276ba87ac6 38 TIM1->CCER |= ~(TIM_CCER_CC1NP); // Interupt when low side is on.
benkatz22:60276ba87ac6 39 TIM1->CR1 |= TIM_CR1_CEN; // enable TIM1
benkatz20:bf9ea5125d52 40
benkatz20:bf9ea5125d52 41 }
benkatz20:bf9ea5125d52 42
benkatz20:bf9ea5125d52 43void Init_ADC(void){
benkatz20:bf9ea5125d52 44 // ADC Setup
benkatz22:60276ba87ac6 45 RCC->APB2ENR |= RCC_APB2ENR_ADC2EN; // clock for ADC2
benkatz22:60276ba87ac6 46 RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; // clock for ADC1
benkatz22:60276ba87ac6 47 RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN; // Enable clock for GPIOC
benkatz20:bf9ea5125d52 48
benkatz22:60276ba87ac6 49 ADC->CCR = 0x00000006; // Regular simultaneous mode only
benkatz22:60276ba87ac6 50 ADC1->CR2 |= ADC_CR2_ADON;//0x00000001; // ADC1 ON
benkatz22:60276ba87ac6 51 ADC1->SQR3 = 0x000000A; // use PC_0 as input
benkatz22:60276ba87ac6 52 ADC2->CR2 |= ADC_CR2_ADON;//0x00000001; // ADC1 ON
benkatz22:60276ba87ac6 53 ADC2->SQR3 = 0x0000000B; // use PC_1 as input
benkatz22:60276ba87ac6 54 GPIOC->MODER |= 0x0000000f; // Alternate function, PC_0, PC_1 are analog inputs
benkatz20:bf9ea5125d52 55
benkatz20:bf9ea5125d52 56 }
benkatz20:bf9ea5125d52 57
benkatz20:bf9ea5125d52 58void Init_DAC(void){
benkatz22:60276ba87ac6 59 RCC->APB1ENR |= 0x20000000; // Enable clock for DAC
benkatz22:60276ba87ac6 60 DAC->CR |= 0x00000001; // DAC control reg, both channels ON
benkatz22:60276ba87ac6 61 GPIOA->MODER |= 0x00000300; // PA04 as analog output
benkatz20:bf9ea5125d52 62 }
benkatz20:bf9ea5125d52 63
benkatz20:bf9ea5125d52 64void Init_All_HW(GPIOStruct *gpio){
benkatz20:bf9ea5125d52 65 Init_PWM(gpio);
benkatz20:bf9ea5125d52 66 Init_ADC();
benkatz20:bf9ea5125d52 67 //Init_DAC();
benkatz20:bf9ea5125d52 68
benkatz20:bf9ea5125d52 69 }