AIoT应用创新大赛-基于TencentOS Tiny 的遥控小车
AIoT應用創新大賽-基于TencentOS Tiny 的遙控小車 - 云+社區 - 騰訊云https://cloud.tencent.com/developer/article/1951791
一,項目介紹。
很早就有做個智能小車的想法,但由于時間有限和囊中羞澀,一直沒有付出行動,感謝騰訊TencentOS Tiny團隊的信任,給了這么一次機會,雖然離自己的設想的功能還差很多,但至少邁出了重要的一步,更重要的是能學習TencentOS Tiny實時操作系統 和i.MX RT系列應用處理器,在此表示感謝。
最初的設想是能做個可以實時傳輸視頻的巡檢車,但是由于項目時間和目前硬件限制,目前只能說是做了個基于TencentOS Tiny 的遙控小車。主要實現了以下功能:
- 基于騰訊云平臺,利用騰訊連連H5面板來控制小車動作。
- 小車的行駛,包括前進、后退、左轉向、右轉向。
- 小車避障,利用超聲波模塊測距,前進過程中,當距離小于30厘米,小車自動剎車,停止前進。
- 實時上報前方障礙物的距離,在H5面板中顯示。
遙控小車
二,項目整體架構圖
整體架構圖
三,小車硬件組成。
1,小車底盤。四個直流電機驅動。
小車底盤
2,TencentOS Tiny AIoT開發套件
TencentOS Tiny AIoT開發板
開發板特性:
- 內置TencentOS Tiny開源物聯網操作系統。
- 核心板采用的RT1062處理器屬于i.MX RT 系列 MCU,是由 NXP 推出的跨界處理器,跨界是指該系列MCU的定位既非傳統的微控制器、也非傳統的微處理器,i.MX RT 系列 MCU 則綜合了兩者的優勢,既具備高頻率(最高主頻600M)、高處理性能,也具備中斷響應迅速、實時性高的特點。
- 1M RAM 16M SDRAM 64MB qspi flash 128MB spi flash。
- 板載Type-C接口CMSIS DAP仿真器。
- 板載PCIE接口,可擴展4G類物聯網模組。
- 板載物聯網俱樂部WAN Interface接口,可支持NB-IoT、WiFi、4G cat1、LoRa等模組。
- 板載物聯網俱樂部E53 Interface接口,可擴展全系E53傳感器。
- 板載標準24P DVP攝像頭接口,可支持最高500萬像素攝像頭。
- 板載RGB顯示接口,可轉換HDMI輸出。
- 板載高性能音頻解碼芯片,可做語音識別測試。
- 預留SD卡、用戶按鍵、SPI Flash。
3,HG7881CP 四路直流電機驅動模塊。
電機驅動模塊
4,HC-SR04超聲波測距模塊。
HC-SR04
HC-SR04模塊工作原理:
(1)采用IO觸發測距,給至少10us的高電平信號;
(2)模塊自動發送8個40khz的方波,自動檢測是否有信號返回;
(3)有信號返回,通過IO輸出一高電平,高電平持續的時間就是超聲波從發射到返回的時間.
測試距離=(高電平時間*聲速(340M/S))/2;
超聲波時序圖
5,ESP8266模組
ESP8266模組
四,項目涉及到的知識點。
1,騰訊云平臺的利用,H5面板配置及調試。
2,TencentOS Tiny的使用。
3,了解MQTT協議,了解cJSON數據格式。
4,利用ESP8266模塊給騰訊云平臺發送接收數據。
5,官方開發工具MCUXpresso IDE的使用。
6,RT1062的GPIO的配置。
7,RT1062利用輸入捕獲測量脈寬。
五,軟件介紹。
1,TencentOS tiny物聯網操作系統
TencentOS tiny是騰訊面向物聯網領域開發的實時操作系統,現已捐贈給開放原子開源基金會進行孵化,具有低功耗,低資源占用,模塊化,安全可靠等特點,可有效提升物聯網終端產品開發效率。TencentOS tiny 提供精簡的 RTOS 內核,內核組件可裁剪可配置,可快速移植到多種主流 MCU (如NXP Arm Cortex-M 全系列)及模組芯片上。而且,基于RTOS內核提供了豐富的物聯網組件,內部集成主流物聯網協議棧(如 CoAP/MQTT/TLS/DTLS/LoRaWAN/NB-IoT 等),可助力物聯網終端設備及業務快速接入騰訊云物聯網平臺。
· 資源占用極少
TencentOS Tiny 內核具有超低資源占用的特點,RAM 0.8KB,ROM 1.8KB;在類似煙感和紅外等實際場景下,TencentOS tiny 的資源占用僅為:RAM 2.69KB、ROM 12.38KB。
· 高效功耗管理框架
完整包含 MCU 和外圍設備功耗管理,用戶可以根據業務場景選擇可參考的低功耗方案,有效降低設備耗電,延長設備壽命。
· 自動移植工具
TencentOS tiny 提供多種編譯器快速移植指南和移植工具,可實現向新硬件開發板的一鍵移植,省時省力,有效提升開發效率。
· 最后一屏調試工具
TencentOS tiny 可以自動獲取故障現場信息,并保持在端側存儲設備中,觸發重啟后會自動上傳故障信息,可有效解決遠程物聯網設備故障信息獲取難題,提升故障分析解決效率。
· 安全分級方案
TencentOS tiny 提供了多個等級的 IoT 安全方案。您可以根據業務場景和成本要求選擇合適的安全解決方案,方便客戶在安全需求和成本控制之間進行有效平衡。
2,云平臺下發數據解析。
static void tos_topic_handler(void* client, message_data_t* msg) {(void) client;cJSON* cjson_root = NULL;cJSON* cjson_status = NULL;cJSON* cjson_params = NULL;//add zydchar* status = NULL;// char* cmd_value = NULL; //add zyd// char cmd_value = 0; //add zydk_event_flag_t event_flag = report_fail;/* ��?��? */MQTT_LOG_I("-----------------------------------------------------------------------------------");MQTT_LOG_I("%s:%d %s()...\ntopic: %s, qos: %d. \nmessage:\n\t%s\n", __FILE__, __LINE__, __FUNCTION__, msg->topic_name, msg->message->qos, (char*)msg->message->payload);MQTT_LOG_I("-----------------------------------------------------------------------------------\n");/* ?��cjson�����?���?���� */cjson_root = cJSON_Parse((char*)msg->message->payload);if (cjson_root == NULL) {printf("report reply message parser fail\r\n");event_flag = report_fail;goto exit;}/*解析 下發 指令 */cjson_params = cJSON_GetObjectItem(cjson_root, "params");if (cjson_params != NULL) {cJSON* cjson_onoff = cJSON_GetObjectItem(cjson_params,"onoff");if(cjson_onoff != NULL) car_cmd.onoff = cjson_onoff->valueint; cJSON* cjson_drive = cJSON_GetObjectItem(cjson_params,"drive");if(cjson_drive != NULL) car_cmd.drive = cjson_drive->valueint;cJSON* cjson_reverse = cJSON_GetObjectItem(cjson_params,"reverse");if(cjson_reverse != NULL) car_cmd.reverse = cjson_reverse->valueint;cJSON* cjson_right = cJSON_GetObjectItem(cjson_params,"right");if(cjson_right != NULL) car_cmd.right = cjson_right->valueint;cJSON* cjson_left = cJSON_GetObjectItem(cjson_params,"left");if(cjson_left != NULL) car_cmd.left = cjson_left->valueint;}/* ��?status?? */cjson_status = cJSON_GetObjectItem(cjson_root, "status");status = cJSON_GetStringValue(cjson_status);if (cjson_status == NULL || status == NULL) {printf("report reply status parser fail\r\n");event_flag = report_fail;goto exit;}/* �ж�status?? */if (strstr(status,"success")) {event_flag = report_success;}else {event_flag = report_fail;}exit:cJSON_Delete(cjson_root);cjson_root = NULL;status = NULL;tos_event_post(&report_result_event, event_flag);return; }復制
3,云平臺上傳數據。
void mqttclient_task(void) {int error;int lightness = 0;mqtt_client_t *client = NULL;mqtt_message_t msg;k_event_flag_t match_flag;char host_ip[20];memset(&msg, 0, sizeof(msg));#ifdef USE_ESP8266 esp8266_sal_init(esp8266_port);// esp8266_join_ap("TencentOS", "tencentostiny");esp8266_join_ap("TP-LINK_AF26", "xn20190213");// esp8266_join_ap("TP-LINK_EA9E", "20160130"); #endif#ifdef USE_EC600Sec600s_sal_init(HAL_UART_PORT_0); #endifmqtt_log_init();client = mqtt_lease();tos_event_create(&report_result_event, (k_event_flag_t)0u);/* Domain Format: <your product ID>.iotcloud.tencentdevices.com */tos_sal_module_parse_domain("O0CBVXMB0X.iotcloud.tencentdevices.com",host_ip,sizeof(host_ip));/*These infomation is generated by mqtt_config_gen.py tool in "TencentOS-tiny\tools" directory.*/mqtt_set_port(client, "1883");mqtt_set_host(client, host_ip);mqtt_set_client_id(client, "O0CBVXMB0XCar");mqtt_set_user_name(client, "O0CBVXMB0XCar;21010406;12365;4294967295");mqtt_set_password(client, "63d7adbf99fe8a2c93876da22dd150bdc6f78dea;hmacsha1");mqtt_set_clean_session(client, 1);error = mqtt_connect(client);//MQTT_LOG_D("mqtt connect error is %#0x", error);error = mqtt_subscribe(client, "$thing/down/property/O0CBVXMB0X/Car", QOS0, tos_topic_handler);// MQTT_LOG_D("mqtt subscribe error is %#0x", error);while (1) {memset(&msg, 0, sizeof(msg));// snprintf(report_buf, sizeof(report_buf), REPORT_DATA_TEMPLATE, lightness++);snprintf(report_buf, sizeof(report_buf), REPORT_DATA_TEMPLATE, distance);// if (lightness > 100) {// lightness = 0;// }msg.qos = QOS0;msg.payload = (void *) report_buf;error = mqtt_publish(client, "$thing/up/property/O0CBVXMB0X/Car", &msg);// MQTT_LOG_D("mqtt publish error is %#0x", error);tos_event_pend(&report_result_event, report_success|report_fail,&match_flag,TOS_TIME_FOREVER,TOS_OPT_EVENT_PEND_ANY | TOS_OPT_EVENT_PEND_CLR);if (match_flag == report_success) {// printf("report to Tencent IoT Explorer success\r\n");}else if (match_flag == report_fail){// printf("report to Tencent IoT Explorer fail\r\n");}///tos_task_delay(1000);} }復制
4,電機控制及超聲波測距程序。(脈寬的測量程序這里參考野火i.MXRT1052開發板中例程)
#include "pin_mux.h" #include "board.h" #include "fsl_gpio.h" #include "fsl_common.h" #include "fsl_iomuxc.h" #include "fsl_debug_console.h" #include "clock_config.h" #include "fsl_gpt.h" #include "car_contrl.h" #include "tos_k.h" #include "pad_config.h" #include "bsp_nvic.h"//#define SR04_ECHO_GPIO GPIO1 //#define SR04_ECHO_GPIO_PIN 01U#define SR04_TRIG_GPIO GPIO1 #define SR04_TRIG_GPIO_PIN 02U /******************************************************************************** Variables******************************************************************************/ /* Whether the SW is turned on */ volatile bool g_InputSignal = false;/* Symbols to be used with GPIO driver */ #define MOTOR_D1_GPIO GPIO2 /*!< GPIO peripheral base pointer */ #define MOTOR_D1_GPIO_PIN 30U /*!< GPIO pin number *//* Symbols to be used with GPIO driver */ #define MOTOR_D2_GPIO GPIO2 /*!< GPIO peripheral base pointer */ #define MOTOR_D2_GPIO_PIN 31U /*!< GPIO pin number *//* Symbols to be used with GPIO driver */ #define MOTOR_B1_GPIO GPIO1 /*!< GPIO peripheral base pointer */ #define MOTOR_B1_GPIO_PIN 16U /*!< GPIO pin number */ /* Symbols to be used with GPIO driver */ #define MOTOR_B2_GPIO GPIO1 /*!< GPIO peripheral base pointer */ #define MOTOR_B2_GPIO_PIN 17U /*!< GPIO pin number */#define MOTOR_REVERSE_EN() GPIO_PinWrite(MOTOR_D1_GPIO, MOTOR_D1_GPIO_PIN, 1U);\GPIO_PinWrite(MOTOR_D2_GPIO, MOTOR_D2_GPIO_PIN, 0U);\GPIO_PinWrite(MOTOR_B1_GPIO, MOTOR_B1_GPIO_PIN, 1U);\GPIO_PinWrite(MOTOR_B2_GPIO, MOTOR_B2_GPIO_PIN, 0U)#define MOTOR_DRIVE_EN() GPIO_PinWrite(MOTOR_D1_GPIO, MOTOR_D1_GPIO_PIN, 0U);\GPIO_PinWrite(MOTOR_D2_GPIO, MOTOR_D2_GPIO_PIN, 1U);\GPIO_PinWrite(MOTOR_B1_GPIO, MOTOR_B1_GPIO_PIN, 0U);\GPIO_PinWrite(MOTOR_B2_GPIO, MOTOR_B2_GPIO_PIN, 1U)#define MOTOR_RIGHT_EN() GPIO_PinWrite(MOTOR_B1_GPIO, MOTOR_B1_GPIO_PIN, 0U);\GPIO_PinWrite(MOTOR_B2_GPIO, MOTOR_B2_GPIO_PIN, 1U)#define MOTOR_LEFT_EN() GPIO_PinWrite(MOTOR_D1_GPIO, MOTOR_D1_GPIO_PIN, 0U);\GPIO_PinWrite(MOTOR_D2_GPIO, MOTOR_D2_GPIO_PIN, 1U)#define MOTOR_STOP() GPIO_PinWrite(MOTOR_D1_GPIO, MOTOR_D1_GPIO_PIN, 0U);\GPIO_PinWrite(MOTOR_D2_GPIO, MOTOR_D2_GPIO_PIN, 0U);\GPIO_PinWrite(MOTOR_B1_GPIO, MOTOR_B1_GPIO_PIN, 0U);\GPIO_PinWrite(MOTOR_B2_GPIO, MOTOR_B2_GPIO_PIN, 0U);#define SR04_TRIG_ON() GPIO_PinWrite(SR04_TRIG_GPIO, SR04_TRIG_GPIO_PIN, 1U); #define SR04_TRIG_OFF() GPIO_PinWrite(SR04_TRIG_GPIO, SR04_TRIG_GPIO_PIN, 0U);//*****************// ctrl_cmd_t car_cmd; volatile uint64_t timer = 0; volatile int distance = 0; int trig_cnt = 0; // 定時器輸入捕獲用戶自定義變量結構體定義 volatile GPT_ICUserValueTypeDef GPT_ICUserValueStructure = {0,0,0,0,0};/** * @brief 配置GPT相關引腳功能 * @param 無 * @retval 無 */ void GPT_GPIO_Config(void) {/*定義GPIO引腳配置結構體*/gpio_pin_config_t gpt_config;IOMUXC_SetPinMux(IOMUXC_GPIO_EMC_40_GPT2_CAPTURE2, 0U);IOMUXC_SetPinConfig(IOMUXC_GPIO_EMC_40_GPT2_CAPTURE2, GPT_COMPARE_PAD_CONFIG_DATA);gpt_config.direction = kGPIO_DigitalInput; //輸入模式//gpt_config.outputLogic = 0; //默認高電平gpt_config.interruptMode = kGPIO_NoIntmode; //不使用中斷/* 初始化 GPT COMPARE1 GPIO. */GPIO_PinInit(GPT2_CAPTURE2_GPIO, GPT2_CAPTURE2_GPIO_PIN, &gpt_config); }/** * @brief 配置GPT工作模式 * @param 無 * @retval 無 */ void GPT_Config(void) {gpt_config_t gptConfig;/*初始化GPT引腳*/GPT_GPIO_Config();/*GPT的時鐘設置*/CLOCK_SetMux(kCLOCK_PerclkMux, EXAMPLE_GPT_CLOCK_SOURCE_SELECT);CLOCK_SetDiv(kCLOCK_PerclkDiv, EXAMPLE_GPT_CLOCK_DIVIDER_SELECT);/*初始化GPT*/GPT_GetDefaultConfig(&gptConfig);GPT_Init(EXAMPLE_GPT, &gptConfig);/* 設置時鐘分頻 */GPT_SetClockDivider(EXAMPLE_GPT, GPT_DIVIDER);/*設置位輸入模式*/GPT_SetInputOperationMode(EXAMPLE_GPT,kGPT_InputCapture_Channel2,kGPT_InputOperation_RiseEdge);/*使能輸入捕獲中斷*/GPT_EnableInterrupts(EXAMPLE_GPT, kGPT_InputCapture2InterruptEnable);/*使能溢出中斷*/GPT_EnableInterrupts(EXAMPLE_GPT,kGPT_RollOverFlagInterruptEnable);/*設置中斷優先級,*/set_IRQn_Priority(GPT_IRQ_ID,Group4_PreemptPriority_6, Group4_SubPriority_0);/*使能中斷*/EnableIRQ(GPT_IRQ_ID);/* 開啟定時器 */GPT_StartTimer(EXAMPLE_GPT); }/*定義中斷服務函數*/ void EXAMPLE_GPT_IRQHandler(void) {//tos_knl_irq_enter();//PRINTF("GPIO_GetPinsInterrupt_zyd3333\r\n");/**當要被捕獲的信號的周期大于定時器的最長定時時,定時器就會溢出,產生更新中斷*這個時候我們需要把這個最長的定時周期加到捕獲信號的時間里面去*/if ( GPT_GetStatusFlags(EXAMPLE_GPT,kGPT_RollOverFlag) != false ){if ( GPT_ICUserValueStructure.Capture_StartFlag != 0 ){GPT_ICUserValueStructure.Capture_Period ++;}GPT_ClearStatusFlags(EXAMPLE_GPT, kGPT_RollOverFlag);}/*捕獲中斷*/if (GPT_GetStatusFlags(EXAMPLE_GPT,kGPT_InputCapture2Flag) != false){if(GPT_ICUserValueStructure.Capture_FinishFlag != 1){/*第一次捕獲*/if ( GPT_ICUserValueStructure.Capture_StartFlag == 0 ){/*清除溢出次數*/GPT_ICUserValueStructure.Capture_Period = 0;/*讀取當前計數值*/GPT_ICUserValueStructure.Capture_CcrValue_1 = GPT_GetInputCaptureValue(EXAMPLE_GPT,kGPT_InputCapture_Channel2);//PRINTF("GPIO_GetPinsInterrupt_zyd1111 RiseEdge %d \r\n",GPT_ICUserValueStructure.Capture_CcrValue_1);/*當第一次捕獲到上升沿之后,就把捕獲邊沿配置為上升沿*/GPT_SetInputOperationMode(EXAMPLE_GPT,kGPT_InputCapture_Channel2,kGPT_InputOperation_FallEdge);/*開始捕獲標志置1*/GPT_ICUserValueStructure.Capture_StartFlag = 1;}/*上升沿捕獲中斷,第二次捕獲*/else{/*獲取捕獲比較寄存器的值,這個值就是捕獲到的高電平的時間的值*/GPT_ICUserValueStructure.Capture_CcrValue_2 = GPT_GetInputCaptureValue(EXAMPLE_GPT,kGPT_InputCapture_Channel2);//PRINTF("GPIO_GetPinsInterrupt_zyd2222 FallEdge%d \r\n",GPT_ICUserValueStructure.Capture_CcrValue_2);/*當第二次捕獲到上升沿之后,就把捕獲邊沿配置為下降沿,好開啟新的一輪捕獲*/GPT_SetInputOperationMode(EXAMPLE_GPT,kGPT_InputCapture_Channel2,kGPT_InputOperation_RiseEdge);/*開始捕獲標志清0*/GPT_ICUserValueStructure.Capture_StartFlag = 0;/*捕獲完成標志置1 */GPT_ICUserValueStructure.Capture_FinishFlag = 1;}}GPT_ClearStatusFlags(EXAMPLE_GPT, kGPT_InputCapture2Flag);}//tos_knl_irq_leave(); }void delay(uint32_t count) {volatile uint32_t i = 0;for (i = 0; i < count; ++i){__asm("NOP");} } void motor_ctrl_entry(void *arg) {/*初始化并開啟GPT定時器*/GPT_Config();SR04_TRIG_ON();delay(2000);//20usSR04_TRIG_OFF();while (1) {if (car_cmd.onoff){USER_LED_ON();}else{USER_LED_OFF();}if ((car_cmd.drive)&&(distance>=300)){MOTOR_DRIVE_EN();}else if(car_cmd.reverse){MOTOR_REVERSE_EN();}else if(car_cmd.right){MOTOR_RIGHT_EN();}else if(car_cmd.left){MOTOR_LEFT_EN();}else{MOTOR_STOP();}trig_cnt++;if(GPT_ICUserValueStructure.Capture_FinishFlag){/*得到計數值,timer 為64位數據,32位很可能會溢出*/timer = GPT_ICUserValueStructure.Capture_Period * 0xffffffff;timer += GPT_ICUserValueStructure.Capture_CcrValue_2;timer -= GPT_ICUserValueStructure.Capture_CcrValue_1;/*將計數值轉化為時間,單位(ms)*///timer = timer / ((EXAMPLE_GPT_CLK_FREQ)/1000); //單位mstimer = (172*timer) / ((EXAMPLE_GPT_CLK_FREQ)/1000);//單程聲速 344/2 =172 距離單位mmdistance =(int)(timer);//PRINTF("the result is: %lld ms \r\n",timer);PRINTF("the result is: %lld ms \r\n",distance);GPT_ICUserValueStructure.Capture_FinishFlag = 0;SR04_TRIG_ON();delay(2000);//20usSR04_TRIG_OFF();trig_cnt = 0;}if(trig_cnt>10){trig_cnt = 0;SR04_TRIG_ON();delay(2000);//20usSR04_TRIG_OFF();}//PRINTF("###I am task1 %d \r\n",GPT_ICUserValueStructure.Capture_Period);tos_task_delay(20);} }復制
六,騰訊連連H5面板。
1,電腦端調試界面。
調試界面
2,手機端界面。
這里用的是標準面板,使用方法還是很簡單的,不過界面和功能有待優化,有時間使用SDK開發,應該就會非常好了。
手機端
七,總結。
以前很少接觸物聯網方面的項目,隨著物聯網的普及應用,以后工作中可能會大量用到,通過這個項目,確實學到了很多知識,受益良多。自己按照教程,移植TencentOS Tiny 內核,僅僅幾個步驟,非常簡單。騰訊云IoT explorer平臺初次使用,利用H5標準面板,上手非常快。工作中一直使用NXP的單片機,可以說非常有感情,如果能得到一塊RT1062開發板,內心也是無限滿足了,哈哈。
這次開發用的是NXP官方的開發工具MCUXpresso IDE ,用慣了keil,剛開始使用不太順手,不過用習慣了,還是很好用的,功能非常強大。最重要的是keil是要版權的,官方能提供免費的IDE,真的該多多支持。
由于時間太緊,好多功能沒來的及做,后面,有時間會繼續完善,繼續學習TencentOS Tiny和RT1062。
原創聲明,本文系作者授權云+社區發表,未經許可,不得轉載。
如有侵權,請聯系?yunjia_community@tencent.com?刪除。
總結
以上是生活随笔為你收集整理的AIoT应用创新大赛-基于TencentOS Tiny 的遥控小车的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Css 盒子塌陷
- 下一篇: 腾讯在乳腺癌影像AI诊断方向的探索