EECS 192: Mechatronics Design Lab Discussion 11: Embedded Software GSI: Varun Tolani 4 & 5 April 2018 (Week 11) 1 Intro 2 Timers 3 Communication & UART 4 Structuring Your Code Ducky (UCB EECS) Mechatronics Design Lab 4 & 5 April 2018 (Week 11) 1 / 24
Intro Ducky (UCB EECS) Mechatronics Design Lab 4 & 5 April 2018 (Week 11) 2 / 24
Intro What does all this mean? Ducky (UCB EECS) Mechatronics Design Lab 4 & 5 April 2018 (Week 11) 3 / 24
Intro Ducky (UCB EECS) Mechatronics Design Lab 4 & 5 April 2018 (Week 11) 4 / 24
Timers Timers Ducky (UCB EECS) Mechatronics Design Lab 4 & 5 April 2018 (Week 11) 5 / 24
Timers What are some of the different kinds of timers? ◮ Periodic Interrupt Timer (PIT) ◮ Flexible Timer Module (FTM) ◮ Programmable Delay Block (PDB) ◮ Real Time Clock (RTC) Ducky (UCB EECS) Mechatronics Design Lab 4 & 5 April 2018 (Week 11) 6 / 24
Timers When do we use these timers? ◮ Periodic Interrupt Timer (PIT) ◮ Running functions periodically (i.e. control) ◮ Flexible Timer Module (FTM) ◮ Running functions periodically (i.e. control) ◮ Generating PWM ◮ Programmable Delay Block (PDB) ◮ Delaying for a specific amount of time ◮ Real Time Clock (RTC) ◮ Tracking execution time (i.e. get curr time) Ducky (UCB EECS) Mechatronics Design Lab 4 & 5 April 2018 (Week 11) 7 / 24
Timers PIT (Review) #define PIT_IRQ_ID PIT0_IRQn #define PIT_SOURCE_CLOCK CLOCK_GetFreq ( kCLOCK_BusClk ) // config -> enableRunInDebug = false; PIT_GetDefaultConfig (& pitConfig); PIT_Init(PIT , &pitConfig); /* Set timer period for channel 0 */ PIT_SetTimerPeriod (PIT , kPIT_Chnl_0 , USEC_TO_COUNT (500000U, PIT_SOURCE_CLOCK )); // .5s timing /* Enable timer interrupts for channel 0 */ PIT_EnableInterrupts (PIT , kPIT_Chnl_0 , kPIT_TimerInterruptEnable ); /* Enable at the NVIC */ EnableIRQ( PIT_IRQ_ID ); PIT_StartTimer (PIT , kPIT_Chnl_0 ); Ducky (UCB EECS) Mechatronics Design Lab 4 & 5 April 2018 (Week 11) 8 / 24
Timers FTM as a timer (prescaler) #define BOARD_FTM_BASEADDR FTM0 #define BOARD_FTM_IRQ_NUM FTM0_IRQn #define BOARD_FTM_HANDLER FTM0_IRQHandler #define FTM_SOURCE_CLOCK ( CLOCK_GetFreq ( kCLOCK_BusClk )/4) FTM_GetDefaultConfig (& ftmInfo); ftmInfo.prescale = kFTM_Prescale_Divide_4 ; /* Initialize FTM module */ FTM_Init(BOARD_FTM_BASEADDR , &ftmInfo); FTM_SetTimerPeriod (BOARD_FTM_BASEADDR , USEC_TO_COUNT (1000U, FTM_SOURCE_CLOCK )); // Enable Interrupts FTM_EnableInterrupts (BOARD_FTM_BASEADDR , kFTM_TimeOverflowInterruptEnable ); EnableIRQ( BOARD_FTM_IRQ_NUM ); FTM_StartTimer (BOARD_FTM_BASEADDR , kFTM_SystemClock ); Ducky (UCB EECS) Mechatronics Design Lab 4 & 5 April 2018 (Week 11) 9 / 24
Timers FTM ◮ There are 4 FTM’s’s (0-3) ◮ Each FTM has up to 8 channels (think h bridge or even brushless motor control) ◮ Each FTM runs is locked to 1 frequency (but not duty cycle!!!) ◮ FTM allows you to use a prescaler (to count very slowly without overflow) Ducky (UCB EECS) Mechatronics Design Lab 4 & 5 April 2018 (Week 11) 10 / 24
Timers FTM for PWM (review) #define BOARD_FTM_BASEADDR FTM0 #define BOARD_FTM_IRQ_NUM FTM0_IRQn #define BOARD_FTM_HANDLER FTM0_IRQHandler #define FTM_SOURCE_CLOCK kCLOCK_BusClk ftmParam. chnlNumber = BOARD_FTM_CHANNEL ; ftmParam.level = PWM_LEVEL; ftmParam. dutyCyclePercent = init_duty_cycle ; ftmParam. firstEdgeDelayPercent = 0U; FTM_GetDefaultConfig (& ftmInfo); ftmInfo.prescale = kFTM_Prescale_Divide_128 ; // Optional for slow PWM FTM_Init(BOARD_FTM_BASEADDR , &ftmInfo); // Dont Need Interrupts Here !!! FTM_SetupPwm (BOARD_FTM_BASEADDR , &ftmParam , 1U, kFTM_CenterAlignedPwm , freq_hz , FTM_SOURCE_CLOCK ); FTM_StartTimer (BOARD_FTM_BASEADDR , kFTM_SystemClock ); Ducky (UCB EECS) Mechatronics Design Lab 4 & 5 April 2018 (Week 11) 11 / 24
Communication & UART Communication and UART Ducky (UCB EECS) Mechatronics Design Lab 4 & 5 April 2018 (Week 11) 12 / 24
Communication & UART UART Universal Asynchronous Reciever and Transmitter ◮ Asynchronous Digital Data Transfer Protocol ◮ Data format and Data speed (baud rate) are configurable ◮ Used for serial communication (USB, ethernet, etc.) ◮ Transmits data as byte string ◮ User chosen baud rate (bits/second) Ducky (UCB EECS) Mechatronics Design Lab 4 & 5 April 2018 (Week 11) 13 / 24
Communication & UART UART UART Tx- Transmit, Rx- Receive, G- Gnd (only need 3 pins for UART data transmission) Ducky (UCB EECS) Mechatronics Design Lab 4 & 5 April 2018 (Week 11) 14 / 24
Communication & UART UART Baud Rate Baud = bits/second Some common baud rates are 9600, 38400, 115200 Ducky (UCB EECS) Mechatronics Design Lab 4 & 5 April 2018 (Week 11) 15 / 24
Communication & UART UART Bluetooth on K64F We can just plug in our bluesmirf chip directly to the K64F! Ducky (UCB EECS) Mechatronics Design Lab 4 & 5 April 2018 (Week 11) 16 / 24
Communication & UART Telemetry Telemetry ◮ Why Telemetry ◮ Necessary for checkpoint 9 and race 1/2 ◮ Invaluable for real time debugging ◮ Telemetry uses UART! ◮ Can do asynchronous communication ◮ Floating Point! ◮ Easy to switch between USB & Bluetooth Ducky (UCB EECS) Mechatronics Design Lab 4 & 5 April 2018 (Week 11) 17 / 24
Communication & UART Telemetry Our Telemetry System telemetry uart.c (in telemetry skeleton code) #define DEMO_UART UART0 #define DEMO_UART_CLKSRC UART0_CLK_SRC #define DEMO_UART_CLK_FREQ CLOCK_GetFreq ( UART0_CLK_SRC ) #define DEMO_UART_IRQn UART0_RX_TX_IRQn #define DEMO_UART_IRQHandler UART0_RX_TX_IRQHandler void init_uart(void){ uart_config_t config; /* * config. baudRate_Bps = 115200U; * config. parityMode = kUART_ParityDisabled ; * config. stopBitCount = kUART_OneStopBit ; * config. txFifoWatermark = 0; * config. rxFifoWatermark = 1; * config.enableTx = false; * config.enableRx = false; */ UART_GetDefaultConfig (& config); config. baudRate_Bps = 115200U; config.enableTx = true; config.enableRx = true; UART_Init(DEMO_UART , &config , DEMO_UART_CLK_FREQ ); } UART0 routes to USB if plugged in! For bluetooth need to use UART corresponding to PTC14/15 Ducky (UCB EECS) Mechatronics Design Lab 4 & 5 April 2018 (Week 11) 18 / 24
Communication & UART Telemetry Our Telemetry System (as of April 3) Data flows 1 way Ducky (UCB EECS) Mechatronics Design Lab 4 & 5 April 2018 (Week 11) 19 / 24
Communication & UART Telemetry Tips for Telemetry ◮ Get telemetry working via usb first ◮ Use the same baud rate on your laptop, K64, and bluetooth chip (will not work if you ignore this) ◮ The bluetooth chips come preconfigured from previous years to run at a certain baud rate (usually 9600, 38400, or 115200). You can change this if needed. ◮ Using telemetry with a slower baud rate is recommended (9600 or 38400). ◮ Logging too many variables (especially waterfall plots) will slow down your cpu. ◮ Known Telemetry Issues ◮ Can only transfer 32bit values as of now ◮ Data only flows 1 direction ◮ Blocking writes used Ducky (UCB EECS) Mechatronics Design Lab 4 & 5 April 2018 (Week 11) 20 / 24
Structuring Your Code Structuring Your Code Ducky (UCB EECS) Mechatronics Design Lab 4 & 5 April 2018 (Week 11) 21 / 24
Structuring Your Code Code Structure 1 Closed Loop Control & Telemetry int main (){ init_pit(freq , pit_handler ); while (1){ telemetry.do_io (); } } void pit_handler (){ estimate_velocity (); find_line (); calculate_steering_error (); calculate_velocity_error (); apply_steering_control (); apply_velocity_control (); } Ducky (UCB EECS) Mechatronics Design Lab 4 & 5 April 2018 (Week 11) 22 / 24
Structuring Your Code Code Structure 2 Closed Loop Control & Telemetry int main (){ init_pit(freq , pit_handler ); while (1){ PRINTF(’debugging\r\n’); telemetry.do_io (); } } void pit_handler (){ estimate_velocity (); find_line (); calculate_steering_error (); calculate_velocity_error (); apply_steering_control (); apply_velocity_control (); } Why might this be a bad idea? Ducky (UCB EECS) Mechatronics Design Lab 4 & 5 April 2018 (Week 11) 23 / 24
Structuring Your Code Code Structure 3 Closed Loop Control & Telemetry int main (){ init_pit(freq , pit_handler ); init_idle_task (); while (1){ SPRINTF(’debugging\r\n’); telemetry.do_io (); } } void idle_task (){ PRINTF(queue.pop ()); } void pit_handler (){ estimate_velocity (); find_line (); calculate_steering_error (); calculate_velocity_error (); apply_steering_control (); apply_velocity_control (); } Ducky (UCB EECS) Mechatronics Design Lab 4 & 5 April 2018 (Week 11) 24 / 24
Recommend
More recommend