robot

Dependencies:   FastPWM3 mbed

Committer:
bwang
Date:
Fri Feb 09 23:24:25 2018 +0000
Revision:
187:523cf8c962e4
Child:
191:66861311bdcd
02/09/2018 18:22 - moved hard-coded config values to defaults.h, started filling out errors.cpp and callbacks.cpp

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bwang 187:523cf8c962e4 1 #include "CommandProcessor.h"
bwang 187:523cf8c962e4 2
bwang 187:523cf8c962e4 3 #include "defaults.h"
bwang 187:523cf8c962e4 4 #include "globals.h"
bwang 187:523cf8c962e4 5 #include "main.h"
bwang 187:523cf8c962e4 6
bwang 187:523cf8c962e4 7 char linebuf[128];
bwang 187:523cf8c962e4 8 int index = 0;
bwang 187:523cf8c962e4 9
bwang 187:523cf8c962e4 10 void rxCallback() {
bwang 187:523cf8c962e4 11 while (io.pc->readable()) {
bwang 187:523cf8c962e4 12 char c = io.pc->getc();
bwang 187:523cf8c962e4 13 if (c != 127 && c != '\r' && c != '\t') {
bwang 187:523cf8c962e4 14 linebuf[index] = c;
bwang 187:523cf8c962e4 15 index++;
bwang 187:523cf8c962e4 16 io.pc->putc(c);
bwang 187:523cf8c962e4 17 } else if (c == 127) {
bwang 187:523cf8c962e4 18 if (index > 0) {
bwang 187:523cf8c962e4 19 index--;
bwang 187:523cf8c962e4 20 io.pc->putc(c);
bwang 187:523cf8c962e4 21 }
bwang 187:523cf8c962e4 22 } else if (c == '\r') {
bwang 187:523cf8c962e4 23 linebuf[index] = 0;
bwang 187:523cf8c962e4 24 io.pc->putc(c);
bwang 187:523cf8c962e4 25 processCmd(io.pc, io.pref, linebuf);
bwang 187:523cf8c962e4 26 index = 0;
bwang 187:523cf8c962e4 27 io.pc->putc('>');
bwang 187:523cf8c962e4 28 }
bwang 187:523cf8c962e4 29 }
bwang 187:523cf8c962e4 30 }
bwang 187:523cf8c962e4 31
bwang 187:523cf8c962e4 32 extern "C" void TIM1_UP_TIM10_IRQHandler(void) {
bwang 187:523cf8c962e4 33 int start_state = io.throttle_in->state();
bwang 187:523cf8c962e4 34
bwang 187:523cf8c962e4 35 if (TIM1->SR & TIM_SR_UIF ) {
bwang 187:523cf8c962e4 36 ADC1->CR2 |= 0x40000000;
bwang 187:523cf8c962e4 37 volatile int delay;
bwang 187:523cf8c962e4 38 for (delay = 0; delay < 35; delay++);
bwang 187:523cf8c962e4 39
bwang 187:523cf8c962e4 40 read.adval1 = ADC1->DR;
bwang 187:523cf8c962e4 41 read.adval2 = ADC2->DR;
bwang 187:523cf8c962e4 42
bwang 187:523cf8c962e4 43 commutate();
bwang 187:523cf8c962e4 44 }
bwang 187:523cf8c962e4 45 TIM1->SR = 0x00;
bwang 187:523cf8c962e4 46 int end_state = io.throttle_in->state();
bwang 187:523cf8c962e4 47 if (start_state != end_state) io.throttle_in->block();
bwang 187:523cf8c962e4 48 }
bwang 187:523cf8c962e4 49
bwang 187:523cf8c962e4 50 void update_velocity() {
bwang 187:523cf8c962e4 51 read.last_p_mech = read.p_mech;
bwang 187:523cf8c962e4 52 read.p_mech = io.pos->GetMechPosition();
bwang 187:523cf8c962e4 53
bwang 187:523cf8c962e4 54 float dp_mech = read.p_mech - read.last_p_mech;
bwang 187:523cf8c962e4 55 if (dp_mech < -PI) dp_mech += 2 * PI;
bwang 187:523cf8c962e4 56 if (dp_mech > PI) dp_mech -= 2 * PI;
bwang 187:523cf8c962e4 57
bwang 187:523cf8c962e4 58 float w_raw = dp_mech * F_SW; //rad/s
bwang 187:523cf8c962e4 59
bwang 187:523cf8c962e4 60 read.w = control.velocity_filter->update(w_raw);
bwang 187:523cf8c962e4 61 }