-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathduoji.h
205 lines (180 loc) · 5.29 KB
/
duoji.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* Author: Liang Junyong
* reference: mraa/example/c/pwm.c mraa/example/c/gpio.c
*
* usage: main函数在程序开始时直接调用init()来初始化所有引脚
* 程序结束时调用deinit()来释放所有引脚!!!
* 设逆时针旋转为正方向。0度->占空比0.875,180度->占空比0.975
* 调用rotate(mraa_pwm_context dev, float degree)来使舵机旋转到指定角度
* 注意旋转角度为0-180度,调用完rotate函数要至少usleep(400000)来确保
* 舵机不丢步。
* 调用mraa_gpio_write(gpio1, 0)来使GPIO输出高低电平
*/
#pragma once
/* standard headers */
//#include <cstdlib>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
/* mraa header */
#include "c/pwm.h"
#include "mraa/pwm.h"
#include "mraa/gpio.h"
/* PWM declaration */
#define PWM0 11
#define PWM1 13
/* GPIO declaration */
#define GPIO_PIN_1 15
/* PWM period(周期) in ms */
#define PWM_FREQ 20
/* 定义针脚 */
mraa_pwm_context pwm0;
mraa_pwm_context pwm1;
mraa_gpio_context gpio1;
/* 定义函数 */
void init();
void rotate(mraa_pwm_context dev, float degree);
void deinit();
/* 初始化函数 */
void init(){
/* 定义状态码 */
mraa_result_t status = MRAA_SUCCESS;
/* 初始化引脚,如果初始化失败则退出 */
gpio1 = mraa_gpio_init(GPIO_PIN_1);
pwm0 = mraa_pwm_init(PWM0);
pwm1 = mraa_pwm_init(PWM1);
if (gpio1 == NULL) {
fprintf(stderr, "Failed to initialize GPIO %d\n", GPIO_PIN_1);
mraa_deinit();
return EXIT_FAILURE;
}
if (pwm0 == NULL) {
fprintf(stderr, "Failed to initialize PWM0\n");
mraa_deinit();
return EXIT_FAILURE;
}
if (pwm1 == NULL) {
fprintf(stderr, "Failed to initialize PWM1\n");
mraa_deinit();
return EXIT_FAILURE;
}
/* set PWM period */
status = mraa_pwm_period_ms(pwm0, PWM_FREQ);
if (status != MRAA_SUCCESS) {
mraa_result_print(status);
mraa_pwm_close(pwm0);
mraa_deinit();
return EXIT_FAILURE;
}
status = mraa_pwm_period_ms(pwm1, PWM_FREQ);
if (status != MRAA_SUCCESS) {
mraa_result_print(status);
mraa_pwm_close(pwm1);
mraa_deinit();
return EXIT_FAILURE;
}
/* enable PWM */
status = mraa_pwm_enable(pwm0, 1);
if (status != MRAA_SUCCESS) {
mraa_result_print(status);
mraa_pwm_close(pwm0);
mraa_deinit();
return EXIT_FAILURE;
}
status = mraa_pwm_enable(pwm1, 1);
if (status != MRAA_SUCCESS) {
mraa_result_print(status);
mraa_pwm_close(pwm1);
mraa_deinit();
return EXIT_FAILURE;
}
/* set GPIO to output */
status = mraa_gpio_dir(gpio1, MRAA_GPIO_OUT);
if (status != MRAA_SUCCESS) {
mraa_result_print(status);
mraa_deinit();
return EXIT_FAILURE;
}
}
/* rotate TO 1-180 degree */
void rotate(mraa_pwm_context dev, float degree){
/* 定义状态码 */
mraa_result_t status = MRAA_SUCCESS;
/* 容错判断 */
// if(degree > 180 || degree < 0){
// fprintf(stderr, "Invalid degree\n");
// deinit();
// return EXIT_FAILURE;
// }
if(degree < 0){
degree = 0;
}
if(degree > 180){
degree = 180;
}
/* write PWM duty cyle */
float duty;
duty = 0.875 + (degree / 1800);
status = mraa_pwm_write(dev, duty);
if (status != MRAA_SUCCESS) {
mraa_result_print(status);
mraa_pwm_close(dev);
mraa_deinit();
return EXIT_FAILURE;
}
}
/* 释放所有引脚 */
void deinit(){
mraa_pwm_close(pwm0);
mraa_pwm_close(pwm1);
mraa_gpio_close(gpio1);
mraa_deinit();
}
/**
* A structure representing a PWM pin
*/
/** struct _pwm {
* int pin; the pin number, as known to the os.
* int chipid; the chip id, which the pwm resides
* int duty_fp; File pointer to duty file
* int period; Cache the period to speed up setting duty
* mraa_boolean_t owner; Owner of pwm context
* mraa_adv_func_t* advance_func; override function table
**/
/**
* A structure representing a gpio pin.
*/
/** struct _gpio {
*
* int pin; the pin number, as known to the os.
* int phy_pin; pin passed to clean init. -1 none and raw
* int value_fp; the file pointer to the value of the gpio
* void (* isr)(void *); the interrupt service request
* void *isr_args; args return when interrupt service request triggered
* pthread_t thread_id; the isr handler thread id
* int isr_value_fp; the isr file pointer on the value
* #ifndef HAVE_PTHREAD_CANCEL
* int isr_control_pipe[2]; a pipe used to interrupt the isr from polling the value fd
* #endif
* mraa_boolean_t isr_thread_terminating; is the isr thread being terminated?
* mraa_boolean_t owner; If this context originally exported the pin
* mraa_result_t (*mmap_write) (mraa_gpio_context dev, int value);
* int (*mmap_read) (mraa_gpio_context dev);
* mraa_adv_func_t* advance_func; override function table
* #if defined(MOCKPLAT)
* mraa_gpio_dir_t mock_dir; mock direction of the pin
* int mock_state; mock state of the pin
* #endif
* @}
* #ifdef PERIPHERALMAN
* AGpio *bgpio;
* #endif
* struct _gpio_group *gpio_group;
* unsigned int num_chips;
* int *pin_to_gpio_table;
* unsigned int num_pins;
* mraa_gpio_events_t events;
* int *provided_pins;
* struct _gpio *next;
* };
**/