1 /* 2 * Copyright (C) 2022 HiHope Open Source Organization . 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * 14 * limitations under the License. 15 */ 16 17 #include <stdio.h> 18 #include <stdlib.h> 19 #include <memory.h> 20 21 #include "ohos_init.h" 22 #include "cmsis_os2.h" 23 #include "iot_gpio.h" 24 #include "hi_io.h" 25 #include "hi_time.h" 26 27 #define GPIO2 2 28 #define COUNT 10 29 #define TASK_STAK_SIZE (1024*10) 30 set_angle(unsigned int duty)31void set_angle(unsigned int duty) 32 { 33 unsigned int time = 20000; 34 IoTGpioSetDir(GPIO2, IOT_GPIO_DIR_OUT); 35 IoTGpioSetOutputVal(GPIO2, IOT_GPIO_VALUE1); 36 hi_udelay(duty); 37 IoTGpioSetOutputVal(GPIO2, IOT_GPIO_VALUE0); 38 hi_udelay(time - duty); 39 } 40 41 /* Turn 45 degrees to the left of the steering gear 42 1、依据角度与脉冲的关系,设置高电平时间为1000微秒 43 2、不断地发送信号,控制舵机向左旋转45度 44 */ engine_turn_left_45(void)45void engine_turn_left_45(void) 46 { 47 unsigned int angle = 1000; 48 for (int i = 0; i < COUNT; i++) { 49 set_angle(angle); 50 } 51 } 52 53 /* Turn 90 degrees to the left of the steering gear 54 1、依据角度与脉冲的关系,设置高电平时间为500微秒 55 2、不断地发送信号,控制舵机向左旋转90度 56 */ engine_turn_left_90(void)57void engine_turn_left_90(void) 58 { 59 unsigned int angle = 500; 60 for (int i = 0; i < COUNT; i++) { 61 set_angle(angle); 62 } 63 } 64 65 /* Turn 45 degrees to the right of the steering gear 66 1、依据角度与脉冲的关系,设置高电平时间为2000微秒 67 2、不断地发送信号,控制舵机向右旋转45度 68 */ engine_turn_right_45(void)69void engine_turn_right_45(void) 70 { 71 unsigned int angle = 2000; 72 for (int i = 0; i < COUNT; i++) { 73 set_angle(angle); 74 } 75 } 76 77 /* Turn 90 degrees to the right of the steering gear 78 1、依据角度与脉冲的关系,设置高电平时间为2500微秒 79 2、不断地发送信号,控制舵机向右旋转90度 80 */ engine_turn_right_90(void)81void engine_turn_right_90(void) 82 { 83 unsigned int angle = 2500; 84 for (int i = 0; i < COUNT; i++) { 85 set_angle(angle); 86 } 87 } 88 89 /* The steering gear is centered 90 1、依据角度与脉冲的关系,设置高电平时间为1500微秒 91 2、不断地发送信号,控制舵机居中 92 */ regress_middle(void)93void regress_middle(void) 94 { 95 unsigned int angle = 1500; 96 for (int i = 0; i < COUNT; i++) { 97 set_angle(angle); 98 } 99 } 100 101 /* 任务实现 */ RobotTask(void * parame)102void RobotTask(void* parame) 103 { 104 (void)parame; 105 unsigned int time = 200; 106 printf("start test sg90\r\n"); 107 engine_turn_left_45(); 108 printf("Turn 45 degrees to the left of the steering gear\r\n"); 109 osDelay(time); 110 engine_turn_left_90(); 111 printf("Turn 90 degrees to the left of the steering gear\r\n"); 112 osDelay(time); 113 regress_middle(); 114 printf("The steering gear is centered"); 115 osDelay(time); 116 engine_turn_right_45(); 117 printf("Turn 45 degrees to the left of the steering gear\r\n"); 118 osDelay(time); 119 engine_turn_right_90(); 120 printf("Turn 90 degrees to the left of the steering gear\r\n"); 121 osDelay(time); 122 regress_middle(); 123 printf("The steering gear is centered"); 124 } 125 126 RobotDemo(void)127static void RobotDemo(void) 128 { 129 osThreadAttr_t attr; 130 131 attr.name = "RobotTask"; 132 attr.attr_bits = 0U; 133 attr.cb_mem = NULL; 134 attr.cb_size = 0U; 135 attr.stack_mem = NULL; 136 attr.stack_size = TASK_STAK_SIZE; 137 attr.priority = osPriorityNormal; 138 139 if (osThreadNew(RobotTask, NULL, &attr) == NULL) { 140 printf("[RobotDemo] Failed to create RobotTask!\n"); 141 } 142 } 143 APP_FEATURE_INIT(RobotDemo); 144