2.007 PulleyInterface mbed code. Biomimetics robotics lab. Sangbae Kim. Ben Katz. For use with PulleyInterface.mlapp
main.cpp
- Committer:
- abraham1
- Date:
- 2017-02-06
- Revision:
- 5:f4c237d0bb32
- Parent:
- 4:f8a45966e63b
- Child:
- 6:73e417b1c521
File content as of revision 5:f4c237d0bb32:
///setup code for encoder on pins PA0 and PA1 (A0 and A1)/// #include "mbed.h" #include "time.h" #include "stdio.h" #include "ctype.h" #define PI 3.14159265358979323846 InterruptIn button(USER_BUTTON); PwmOut pwm(D5);//do not use D3 DigitalOut a(D2); DigitalOut b(D4); AnalogIn currentSense(A5); //hook up to Vout on current sensor Serial pc(USBTX, USBRX, 115200); DigitalOut green(LED2); const int CPR = 900; // Encoder counts per revolution. Change to match your encoder const double VREF = 3; //Microcontroller reference voltage const float currentSensorOutputRatio = 0.185; // Volts/Amp. Divide Voltage by cSenseOutput to get current void EncoderInitialise(void) { // configure GPIO PA0 & PA1 as inputs for Encoder RCC->AHB1ENR |= 0x00000001; // Enable clock for GPIOA GPIOA->MODER |= GPIO_MODER_MODER0_1 | GPIO_MODER_MODER1_1 ; //PA0 & PA1 as Alternate Function /*!< GPIO port mode register, Address offset: 0x00 */ GPIOA->OTYPER |= GPIO_OTYPER_OT_0 | GPIO_OTYPER_OT_1 ; //PA0 & PA1 as Inputs /*!< GPIO port output type register, Address offset: 0x04 */ GPIOA->OSPEEDR |= 0x00000011;//|= GPIO_OSPEEDER_OSPEEDR0 | GPIO_OSPEEDER_OSPEEDR1 ; // Low speed /*!< GPIO port output speed register, Address offset: 0x08 */ GPIOA->PUPDR |= GPIO_PUPDR_PUPDR0_1 | GPIO_PUPDR_PUPDR1_1 ; // Pull Down /*!< GPIO port pull-up/pull-down register, Address offset: 0x0C */ GPIOA->AFR[0] |= 0x00000011 ; // AF01 for PA0 & PA1 /*!< GPIO alternate function registers, Address offset: 0x20-0x24 */ GPIOA->AFR[1] |= 0x00000000 ; // /*!< GPIO alternate function registers, Address offset: 0x20-0x24 */ // configure TIM2 as Encoder input RCC->APB1ENR |= 0x00000001; // Enable clock for TIM2 TIM2->CR1 = 0x0001; // CEN(Counter Enable)='1' < TIM control register 1 TIM2->SMCR = 0x0003; // SMS='011' (Encoder mode 3) < TIM slave mode control register TIM2->CCMR1 = 0x0101; // CC1S='01' CC2S='01' < TIM capture/compare mode register 1 TIM2->CCMR2 = 0x0000; // < TIM capture/compare mode register 2 TIM2->CCER = 0x0011; // CC1P CC2P < TIM capture/compare enable register TIM2->PSC = 0x0000; // Prescaler = (0+1) < TIM prescaler TIM2->ARR = CPR; // reload at CPR < TIM auto-reload register TIM2->CNT = 0x0000; //reset the counter before we use it } //Zero encoder count// void ZeroEncoder() { TIM2->CNT=0 ; //reset timer count to zero } int GetCounts(void) { int count = TIM2->CNT; //Read the timer count register return count; } void pressed() { float pwm_float = pwm.read(); int pwmV = (int)(100*pwm_float); if(pwmV == 0){ pwm.write(0.05); } else if (pwmV == 5){ pwm.write(0.2); } else if (pwmV == 20){ pwm.write(0.75); } else if (pwmV == 75){ pwm.write(0.0); } else { pwm.write(0.0); } } int main() { int endcount, startcount; double time_between_readings; double velocity; double currentSensed = 0; clock_t start, end; int ticks; a=1; b=0; pwm.write(0); button.fall(&pressed); double updatePeriod = 0.01; /* must select carefully */ double publishFrequency = 0.05; /* seconds. rate to publish to matlab */ double samplesPerPublish = (int)(publishFrequency/updatePeriod); /*this improves time response of filter and maintains smoothness*/ int publishCounter = 1; double filterRatio = 0.045; double currentFilterRatio = 0.02; float currentSensorOffset = 0; int i; for(i=1;i<301;i++){ currentSensorOffset += currentSense.read(); } currentSensorOffset = currentSensorOffset*VREF/300; EncoderInitialise(); fflush(pc); /*** wait here for matlab information like voltage before starting? ***/ while(1) { while(1) { green = true; if(pc.readable()) { char charIn = pc.getc(); if(charIn == 'g'){ end = clock(); break; } } wait(0.05); } while(1) { green = false; wait(updatePeriod); clock_t absoluteStart = clock(); start = end; end = clock(); time_between_readings = ((double)(end - start)) / CLOCKS_PER_SEC; startcount = endcount; endcount = GetCounts(); ticks = endcount-startcount; if(abs(ticks)>CPR/2) /***** for rollover case: *****/ { ticks = ((ticks<0)-(ticks>0))*(CPR-abs(ticks)); } velocity = filterRatio*((double)ticks)/CPR*2*PI/time_between_readings + (1-filterRatio)*velocity; /* with filtering*/ currentSensed = currentFilterRatio*((double)currentSense.read()*VREF-currentSensorOffset) + (1-currentFilterRatio)*currentSensed; if(pc.readable()) { char charIn = pc.getc(); if(charIn == 'r'){ fflush(pc); /* TODO: purge much better than this! */ // green = !green; break; } else if(isdigit(charIn)) { double abrahamsCommand = (double)(charIn - '0'); pwm.write(abrahamsCommand/10.0); } } if(publishCounter == samplesPerPublish) { printf("%+8f,%+8f,%+8f,%+8f\n", currentSensed/currentSensorOutputRatio, pwm.read(), velocity, ((double)(end-absoluteStart)/CLOCKS_PER_SEC)); publishCounter = 1; } publishCounter++; } } }