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
Line Sensing 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 Let’s look at one specific frame Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 4 / 33
Line Sensing Problem Setup frame[100] - mean(frame[100]) Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 5 / 33
Line Sensing Problem Setup How can we find the index of the line? Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 6 / 33
Line Sensing Argmax Based Detection Take the argmax! Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 7 / 33
Line Sensing Argmax Based Detection Any potential issues? Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 8 / 33
Line Sensing Argmax Based Detection What about- noise, track crossings, other bright objects, etc.? Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 9 / 33
Line Sensing Argmax Based Detection What about- noise, track crossings, other bright objects, multiple peaks, etc.? Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 10 / 33
Line Sensing Gradients and Smoothing I(x,t) = Intensity at pixel x What if we look at the derivative dI ( x ) dx ? Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 11 / 33
Line Sensing Gradients and Smoothing dI ( x ) ≈ I ( x +1) − I ( x ) dx 1 Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 12 / 33
Line Sensing Gradients and Smoothing Convolution! ˙ I ( x ) = I ( x + 1) − I ( x ) = I ∗ [1 , − 1] Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 13 / 33
Line Sensing Gradients and Smoothing Convolution! ˙ I ( x ) = I ( x + 1) − I ( x ) = I ∗ [1 , − 1] Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 14 / 33
Line Sensing Gradients and Smoothing Convolution! ˙ I ( x ) = I ( x + 1) − I ( x ) = I ∗ [1 , − 1] Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 15 / 33
Line Sensing Gradients and Smoothing Convolution! What about all that high frequency noise? Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 16 / 33
Line Sensing Gradients and Smoothing Low Pass Filter! I ( x ) ∗ LPF ∗ [1 , − 1] = I ( x ) ∗ ( LPF ∗ [1 , − 1]) Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 17 / 33
Line Sensing Gradients and Smoothing Low Pass Filter (Gaussian)! I ( x ) ∗ LPF ∗ [1 , − 1] = I ( x ) ∗ ( LPF ∗ [1 , − 1]) Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 18 / 33
Line Sensing Gradients and Smoothing Low Pass Filter (Gaussian)! High frequency noise gone! Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 19 / 33
Line Sensing Temporal Differencing What about... Temporal Differencing? dI ( x ) ≈ I ( t ) − I ( t − 1) dt ∆ t Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 20 / 33
Line Sensing Temporal Differencing What about... Temporal Differencing? Good for removing constant bias in parts of the image. Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 21 / 33
Line Sensing Temporal Differencing More Ideas! ◮ 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 Potential Issues? ◮ What to do if there are multiple track crossings? ◮ Can’t find the line ◮ Other Issues? Solutions! ◮ 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 Embedded Software/ MCUXpresso Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 24 / 33
Embedded Software/ MCUXpresso Modularity 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 How to delay? // 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 over } } Ducky (UCB EECS) Mechatronics Design Lab 7 & 8 Mar 2018 (Week 8) 26 / 33
Embedded Software/ MCUXpresso Timing Use a Timer!! (PIT or FTM) // 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 over } } uint32_t get_curr_time_ms (){ return systime; } // Initialize this interrupt somewhere to run at 1Khz // Then systime is in units of ms void PIT0_IRQHandler (void) { /* Clear interrupt flag.*/ systime ++; /* hopefully atomic operation */ 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 What about FTM’s??? /* 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. chnlNumber = BOARD_FTM_CHANNEL ; ftmParam.level = PWM_LEVEL; ftmParam. dutyCyclePercent = init_duty_cycle ; ftmParam. firstEdgeDelayPercent = 0U; 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 Can Get Rid of FTM Interrupts!!! /* 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. chnlNumber = BOARD_FTM_CHANNEL ; ftmParam.level = PWM_LEVEL; ftmParam. dutyCyclePercent = init_duty_cycle ; ftmParam. firstEdgeDelayPercent = 0U; 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 Can Get Rid of FTM Interrupts (also here)!! void update_duty_cycle (uint8_t updated_duty_cycle ) { FTM_DisableInterrupts (BOARD_FTM_BASEADDR , FTM_CHANNEL_INTERRUPT_ENABLE ); /* Disable channel output 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 output 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
Recommend
More recommend