控制microROS控制板上的LED指示灯闪烁。
如下图所示,LED指示灯为板载元器件,所以不需要外接其他设备,只需要把type-C数据线连接电脑与microROS控制板作为烧录固件功能即可。

程序源码对应的虚拟机路径为:
~/esp/Samples/esp32_samples/led
初始化LED外设,其中LED_GPIO对应硬件电路的GPIO45,GPIO模式为输出模式。
xxxxxxxxxxvoid Led_Init(void){// zero-initialize the config structure.gpio_config_t io_conf = {};//disable interrupt 禁用中断io_conf.intr_type = GPIO_INTR_DISABLE;//set as output mode 设置为输出模式io_conf.mode = GPIO_MODE_OUTPUT;//bit mask of the pins that you want to set 引脚编号设置io_conf.pin_bit_mask = (1ULL<<LED_GPIO);//disable pull-down mode 禁用下拉io_conf.pull_down_en = 0;//disable pull-up mode 禁用上拉io_conf.pull_up_en = 0;//configure GPIO with the given settings 配置GPIO口gpio_config(&io_conf);// 关闭LED灯Led_Off();}
打开LED灯
xxxxxxxxxxvoid Led_On(void){gpio_set_level(LED_GPIO, LED_ACTIVE_LEVEL);}
关闭LED灯
xxxxxxxxxxvoid Led_Off(void){gpio_set_level(LED_GPIO, !LED_ACTIVE_LEVEL);}
控制LED灯状态,state传入0则LED灯灭,state传入1则LED灯亮。
xxxxxxxxxxvoid Led_State(uint8_t state){if (state == 0){gpio_set_level(LED_GPIO, !LED_ACTIVE_LEVEL);}else{gpio_set_level(LED_GPIO, LED_ACTIVE_LEVEL);}}
LED闪烁,需每10毫秒调用一次。interval表示间隔的时间,单位为10ms。
xxxxxxxxxxvoid Led_Flash(uint16_t interval){static uint16_t state = 0;static uint16_t count = 0;count++;if (count >= interval){count = 0;state = (state + 1) % 2;Led_State(state);}}
在app_main里调用Led_Init函数,并且持续调用Led_Flash函数,从而让LED闪烁。
xLed_Init();while (1){Led_Flash(50);vTaskDelay(pdMS_TO_TICKS(10));}
使用Type-C数据线连接虚拟机/电脑与microROS控制板,如果系统弹窗选择连接到虚拟机上。
激活ESP-IDF开发环境,注意每次打开新终端都需要先激活ESP-IDF开发环境才可以编译固件。
xxxxxxxxxxsource ~/esp/esp-idf/export.sh
进入项目目录
xxxxxxxxxxcd ~/esp/Samples/esp32_samples/led
编译、烧录、打开串口模拟器
xxxxxxxxxxidf.py build flash monitor
如果需要退出串口模拟器,请按Ctrl+]。
串口模拟器打印"hello yahboom"欢迎词,并且MCU指示灯每隔0.5秒闪一次。
