1 /*
2 * Copyright (c) 2022 Unionman Technology Co., Ltd.
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 * limitations under the License.
14 */
15
16 #include <string.h>
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <unistd.h>
20
21 #include "hilog/log.h"
22 #include "um_pwm.h"
23
24 #define INIT_DUTY 500000
25 #define MAX_DUTY 2500000
26
user_help(void)27 void user_help(void)
28 {
29 printf("Please input like this: pwm_test <int> <int>\n");
30 printf("the first number is to write the PWM_PIN to the PWM range[1,2] \n");
31 printf("the second number is to write the duty_cycle to the PWM range[-2500000,2500000] \n");
32 }
33
get_final_duty(int pwm_duty,int final_duty)34 int get_final_duty(int pwm_duty, int final_duty)
35 {
36 int real_duty = 0;
37 // 判断最终转到的角度是否超过溢出值,输出实际转到的角度
38 if (final_duty < INIT_DUTY) {
39 real_duty = INIT_DUTY;
40 printf("Beyond the angle of rotation");
41 } else if (final_duty > MAX_DUTY) {
42 real_duty = MAX_DUTY;
43 printf("Beyond the angle of rotation");
44 } else {
45 real_duty = final_duty;
46 }
47 return real_duty;
48 }
49
main(int argc,char ** argv)50 int main(int argc, char **argv)
51 {
52 int pwmChannel = PWM1;
53 int isEnable = PWM_IS_ENABLED;
54 int final_duty = 0;
55 double cur_angle = 0;
56 int cur_duty = 0;
57 int pwm_pin = atoi(argv[1]);
58 int pwm_duty = atoi(argv[2]);
59
60 if (argc > 3L || argc < 2L) {
61 user_help();
62 HILOG_DEBUG(LOG_CORE, "The number of parameters is incorrect!\n");
63 return PWM_ERR;
64 }
65
66 if (pwm_pin < 1L || pwm_pin > 2L || pwm_duty > MAX_DUTY || pwm_duty < -MAX_DUTY) {
67 user_help();
68 HILOG_DEBUG(LOG_CORE, "The wrong parameter!\n");
69 return PWM_ERR;
70 } else {
71 pwmChannel = pwm_pin;
72 }
73
74 init_pmw(pwmChannel);
75 set_pwm_period(pwmChannel, 2600000L);
76 // 0.5ms对应MG996R舵机0度
77 cur_duty = get_pwm_dutyCycle(pwmChannel);
78 if (cur_duty < INIT_DUTY) {
79 set_pwm_dutyCycle(pwmChannel, INIT_DUTY);
80 cur_duty = get_pwm_dutyCycle(pwmChannel);
81 }
82
83 HILOG_INFO(LOG_CORE, "Init Sucess!\n");
84 printf("The current period %d, duty_cycle:%d.\n", get_pwm_period(pwmChannel), get_pwm_dutyCycle(pwmChannel));
85 // 根据占空比计算当前角度
86 cur_angle = 1.0 * (cur_duty - INIT_DUTY) / INIT_DUTY * 45L;
87 printf("The current angle %.2f, about to turn:%.2f.\n", cur_angle, 1.0 * pwm_duty / INIT_DUTY * 45L);
88
89 final_duty = pwm_duty + cur_duty;
90 final_duty = get_final_duty(pwm_duty, final_duty);
91
92 set_pwm_dutyCycle(pwmChannel, final_duty);
93 cur_duty = get_pwm_dutyCycle(pwmChannel);
94 // 计算旋转后的角度
95 printf("The angle after rotation is %.2f.\n", 1.0 * (cur_duty - INIT_DUTY) / INIT_DUTY * 45L);
96 set_pwm_enable(pwmChannel, isEnable);
97
98 return 0;
99 }
100