EECS 192: Mechatronics Design Lab
Discussion 8: Camera & MCUX GSI: Varun Tolani 7 & 8 Mar 2018 (Week 8)
1 Line Sensing 2 Embedded Software/ MCUXpresso 3 Summary
Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 1 / 33
EECS 192: Mechatronics Design Lab Discussion 8: Camera & MCUX - - PowerPoint PPT Presentation
EECS 192: Mechatronics Design Lab Discussion 8: Camera & MCUX GSI: Varun Tolani 7 & 8 Mar 2018 (Week 8) 1 Line Sensing 2 Embedded Software/ MCUXpresso 3 Summary Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 1 /
1 Line Sensing 2 Embedded Software/ MCUXpresso 3 Summary
Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 1 / 33
Line Sensing
Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 2 / 33
Line Sensing Problem Setup Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 3 / 33
Line Sensing Problem Setup
Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 4 / 33
Line Sensing Problem Setup
Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 5 / 33
Line Sensing Problem Setup
Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 6 / 33
Line Sensing Argmax Based Detection
Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 7 / 33
Line Sensing Argmax Based Detection
Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 8 / 33
Line Sensing Argmax Based Detection
Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 9 / 33
Line Sensing Argmax Based Detection
Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 10 / 33
Line Sensing Gradients and Smoothing
dx ?
Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 11 / 33
Line Sensing Gradients and Smoothing
dI(x) dx
1
Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 12 / 33
Line Sensing Gradients and Smoothing
Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 13 / 33
Line Sensing Gradients and Smoothing
Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 14 / 33
Line Sensing Gradients and Smoothing
Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 15 / 33
Line Sensing Gradients and Smoothing
Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 16 / 33
Line Sensing Gradients and Smoothing
Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 17 / 33
Line Sensing Gradients and Smoothing
Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 18 / 33
Line Sensing Gradients and Smoothing
Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 19 / 33
Line Sensing Temporal Differencing
dI(x) dt
∆t
Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 20 / 33
Line Sensing Temporal Differencing
Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 21 / 33
Line Sensing Temporal Differencing
◮ Difference of Gaussians (Bandpass- not just Lowpass) ◮ Cross Correlation with known signal ◮ Convolutional Neural Networks??
Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 22 / 33
Line Sensing Issues
◮ What to do if there are multiple track crossings? ◮ Can’t find the line ◮ Other Issues?
◮ Maintain a history (previous line pos, camera scan, etc.) ◮ Ignore the problem(s) and go fast! ◮ Anything else?
Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 23 / 33
Embedded Software/ MCUXpresso
Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 24 / 33
Embedded Software/ MCUXpresso Modularity
◮ Code is starting to get complicated
◮ Periodic Interrupt TImers (PIT), Flexible Timer Module (FTM),
UART, GPIO, ADC etc.
◮ Many tasks to run on a single core CPU
◮ Velocity Measurement ◮ Line Finding ◮ PID Controllers (steering and velocity) ◮ Telemetry ◮ Others?
◮ How do we make all of this work together???
Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 25 / 33
Embedded Software/ MCUXpresso Timing
// Why might this be a bad idea? void delay(void) { volatile uint32_t i = 0U; for (i = 0U; i < 80000U; ++i) { __asm("NOP"); /* delay */ } } // Where does get_curr_time_ms () come from ??? void delay_ms(uint32_t t){ uint32_t end_time = t + get_curr_time_ms (); while( get_curr_time_ms () < end_time ){ // Block until the delay is
} } Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 26 / 33
Embedded Software/ MCUXpresso Timing
// Where does get_curr_time_ms () come from ??? void delay_ms(uint32_t t){ uint32_t end_time = t + get_curr_time_ms (); while( get_curr_time_ms () < end_time ){ // Block until the delay is
} } uint32_t get_curr_time_ms (){ return systime; } // Initialize this interrupt somewhere to run at 1Khz // Then systime is in units
void PIT0_IRQHandler (void) { /* Clear interrupt flag.*/ systime ++; /* hopefully atomic
*/ PIT_ClearStatusFlags (PIT , kPIT_Chnl_0 , kPIT_TimerFlag ); pitIsrFlag0 = true; } Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 27 / 33
Embedded Software/ MCUXpresso Timing
/* Initialize the Flexible Timer Module to Produce PWM with init_duty_cycle at freq_hz */ void init_pwm(uint32_t freq_hz , uint8_t init_duty_cycle ) { duty_cycle = init_duty_cycle ; ftm_config_t ftmInfo; ftm_chnl_pwm_signal_param_t ftmParam; /* Configure ftm params with for pwm freq - freq_hz , duty cycle - init_duty_cycle */
ftmParam.level = PWM_LEVEL;
FTM_GetDefaultConfig (& ftmInfo ); /* Initialize FTM module */ FTM_Init(BOARD_FTM_BASEADDR , &ftmInfo ); FTM_SetupPwm (BOARD_FTM_BASEADDR , &ftmParam , 1U, kFTM_CenterAlignedPwm , freq_hz , FTM_SOURC /* Enable channel interrupt flag.*/ FTM_EnableInterrupts (BOARD_FTM_BASEADDR , FTM_CHANNEL_INTERRUPT_ENABLE ); EnableIRQ( FTM_INTERRUPT_NUMBER ); FTM_StartTimer (BOARD_FTM_BASEADDR , kFTM_SystemClock ); } Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 28 / 33
Embedded Software/ MCUXpresso Timing
/* Initialize the Flexible Timer Module to Produce PWM with init_duty_cycle at freq_hz */ void init_pwm(uint32_t freq_hz , uint8_t init_duty_cycle ) { duty_cycle = init_duty_cycle ; ftm_config_t ftmInfo; ftm_chnl_pwm_signal_param_t ftmParam; /* Configure ftm params with for pwm freq - freq_hz , duty cycle - init_duty_cycle */
ftmParam.level = PWM_LEVEL;
FTM_GetDefaultConfig (& ftmInfo ); /* Initialize FTM module */ FTM_Init(BOARD_FTM_BASEADDR , &ftmInfo ); FTM_SetupPwm (BOARD_FTM_BASEADDR , &ftmParam , 1U, kFTM_CenterAlignedPwm , freq_hz , FTM_SOURC FTM_StartTimer (BOARD_FTM_BASEADDR , kFTM_SystemClock ); } Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 29 / 33
Embedded Software/ MCUXpresso Timing
void update_duty_cycle (uint8_t updated_duty_cycle ) { FTM_DisableInterrupts (BOARD_FTM_BASEADDR , FTM_CHANNEL_INTERRUPT_ENABLE ); /* Disable channel
before updating the dutycycle */ FTM_UpdateChnlEdgeLevelSelect (BOARD_FTM_BASEADDR , BOARD_FTM_CHANNEL , 0U); /* Update PWM duty cycle */ FTM_UpdatePwmDutycycle (BOARD_FTM_BASEADDR , BOARD_FTM_CHANNEL , kFTM_CenterAlignedPwm , upda /* Software trigger to update registers */ FTM_SetSoftwareTrigger (BOARD_FTM_BASEADDR , true ); /* Start channel
with updated dutycycle */ FTM_UpdateChnlEdgeLevelSelect (BOARD_FTM_BASEADDR , BOARD_FTM_CHANNEL , PWM_LEVEL ); /* Delay to view the updated PWM dutycycle */ delay (); // Can be removed when using PWM for realtime applications /* Enable interrupt flag to update PWM dutycycle */ FTM_EnableInterrupts (BOARD_FTM_BASEADDR , FTM_CHANNEL_INTERRUPT_ENABLE ); } Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 30 / 33
Embedded Software/ MCUXpresso Timing
void update_duty_cycle (uint8_t updated_duty_cycle ) { /* Disable channel
before updating the dutycycle */ FTM_UpdateChnlEdgeLevelSelect (BOARD_FTM_BASEADDR , BOARD_FTM_CHANNEL , 0U); /* Update PWM duty cycle */ FTM_UpdatePwmDutycycle (BOARD_FTM_BASEADDR , BOARD_FTM_CHANNEL , kFTM_CenterAlignedPwm , upda /* Software trigger to update registers */ FTM_SetSoftwareTrigger (BOARD_FTM_BASEADDR , true ); /* Start channel
with updated dutycycle */ FTM_UpdateChnlEdgeLevelSelect (BOARD_FTM_BASEADDR , BOARD_FTM_CHANNEL , PWM_LEVEL ); }
Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 31 / 33
Embedded Software/ MCUXpresso Timing
◮ Remove unecessary interrupts (FTM’s doing PWM for example). This
◮ Build modular code, test components in isolation ◮ PRINTF can mess up your timing (especially the floating point
◮ Minimize dynamic memory allocation aka malloc & free (memory
◮ Use the debugger tool!! ◮ See skeleton code (frdmk64f skeleton) ◮ Keep things simple!
Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 32 / 33
Summary
◮ Many ways to do line detection (max, differencing, gaussian
◮ Embedded Software/ MCUX tips
Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 33 / 33