• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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)
set_angle(unsigned int duty)30 void set_angle(unsigned int duty)
31 {
32     unsigned int time = 20000;
33     IoTGpioSetDir(GPIO2, IOT_GPIO_DIR_OUT);
34     IoTGpioSetOutputVal(GPIO2, IOT_GPIO_VALUE1);
35     hi_udelay(duty);
36     IoTGpioSetOutputVal(GPIO2, IOT_GPIO_VALUE0);
37     hi_udelay(time - duty);
38 }
39 
40 /* Turn 45 degrees to the left of the steering gear
41 1、依据角度与脉冲的关系,设置高电平时间为1000微秒
42 2、不断地发送信号,控制舵机向左旋转45度
43 */
engine_turn_left_45(void)44 void engine_turn_left_45(void)
45 {
46     unsigned int angle = 1000;
47     for (int i = 0; i < COUNT; i++) {
48         set_angle(angle);
49     }
50 }
51 
52 /* Turn 90 degrees to the left of the steering gear
53 1、依据角度与脉冲的关系,设置高电平时间为500微秒
54 2、不断地发送信号,控制舵机向左旋转90度
55 */
engine_turn_left_90(void)56 void engine_turn_left_90(void)
57 {
58     unsigned int angle = 500;
59     for (int i = 0; i < COUNT; i++) {
60         set_angle(angle);
61     }
62 }
63 
64 /* Turn 45 degrees to the right of the steering gear
65 1、依据角度与脉冲的关系,设置高电平时间为2000微秒
66 2、不断地发送信号,控制舵机向右旋转45度
67 */
engine_turn_right_45(void)68 void engine_turn_right_45(void)
69 {
70     unsigned int angle = 2000;
71     for (int i = 0; i < COUNT; i++) {
72         set_angle(angle);
73     }
74 }
75 
76 /* Turn 90 degrees to the right of the steering gear
77 1、依据角度与脉冲的关系,设置高电平时间为2500微秒
78 2、不断地发送信号,控制舵机向右旋转90度
79 */
engine_turn_right_90(void)80 void engine_turn_right_90(void)
81 {
82     unsigned int angle = 2500;
83     for (int i = 0; i < COUNT; i++) {
84         set_angle(angle);
85     }
86 }
87 
88 /* The steering gear is centered
89 1、依据角度与脉冲的关系,设置高电平时间为1500微秒
90 2、不断地发送信号,控制舵机居中
91 */
regress_middle(void)92 void regress_middle(void)
93 {
94     unsigned int angle = 1500;
95     for (int i = 0; i < COUNT; i++) {
96         set_angle(angle);
97     }
98 }
99 
100 /* 任务实现 */
RobotTask(void * parame)101 void RobotTask(void* parame)
102 {
103     (void)parame;
104     printf("The steering gear is centered\r\n");
105     regress_middle();
106 }
107 
108 
RobotDemo(void)109 static void RobotDemo(void)
110 {
111     osThreadAttr_t attr;
112 
113     attr.name = "RobotTask";
114     attr.attr_bits = 0U;
115     attr.cb_mem = NULL;
116     attr.cb_size = 0U;
117     attr.stack_mem = NULL;
118     attr.stack_size = TASK_STAK_SIZE;
119     attr.priority = osPriorityNormal;
120 
121     if (osThreadNew(RobotTask, NULL, &attr) == NULL) {
122         printf("[RobotDemo] Failed to create RobotTask!\n");
123     }
124 }
125 
126 APP_FEATURE_INIT(RobotDemo);
127