H3_ircut.c
4.5 KB
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "./H3_ircut.h"
#include "BaseConfig.h"
//如果wiringPi库安装了,则使用wiringPi库,否则使用sysfs
#ifdef WIRINGPI_STATUS_ON
#include <wiringPi.h>
#endif
#define AMPLIFIER_IRCUT_PIN_1 7
#define AMPLIFIER_IRCUT_PIN_2 19
#define MOTOR_IRCUT_PIN 18
//sys方案输入,未进行测试
int gpio_test_in(unsigned int gpio_chip_num, unsigned int gpio_offset_num)
{
FILE *fp;
char file_name[50];
unsigned char buf[10];
unsigned int gpio_num;
gpio_num = gpio_chip_num * 8 + gpio_offset_num;
sprintf(file_name, "/sys/class/gpio/export");//��Ҫ������ GPIO ��� export
fp = fopen(file_name, "w");
if (fp == NULL)
{
printf("Cannot open %s.\n", file_name);
return -1;
}
fprintf(fp, "%d", gpio_num);
fclose(fp);
sprintf(file_name, "/sys/class/gpio/gpio%d/direction", gpio_num);//���� GPIO ����
fp = fopen(file_name, "rb+");
if (fp == NULL)
{
printf("Cannot open %s.\n", file_name);
return -1;
}
fprintf(fp, "in");//��������
fclose(fp);
sprintf(file_name, "/sys/class/gpio/gpio%d/value", gpio_num);//�鿴 GPIO ����ֵ��
fp = fopen(file_name, "rb+");
if (fp == NULL)
{
printf("Cannot open %s.\n", file_name);
return -1;
}
memset(buf, 0, 10);
fread(buf, sizeof(char), sizeof(buf) - 1, fp);
printf("%s: gpio%d_%d = %d\n", __func__,gpio_chip_num, gpio_offset_num, buf[0]-48);
fclose(fp);
sprintf(file_name, "/sys/class/gpio/unexport");
fp = fopen(file_name, "w");
if (fp == NULL)
{
printf("Cannot open %s.\n", file_name);
return -1;
}
fprintf(fp, "%d", gpio_num);
fclose(fp);
return (int)(buf[0]-48);
}
//sys方案输入,未进行测试
int gpio_test_out(unsigned int gpio_chip_num, unsigned int gpio_offset_num,unsigned int gpio_out_val)
{
FILE *fp;
char file_name[50];
unsigned char buf[10];
unsigned int gpio_num;
gpio_num = gpio_chip_num * 8 + gpio_offset_num;
sprintf(file_name, "/sys/class/gpio/export");
fp = fopen(file_name, "w");
if (fp == NULL)
{
printf("Cannot open %s.\n", file_name);
return -1;
}
fprintf(fp, "%d", gpio_num);
fclose(fp);
sprintf(file_name, "/sys/class/gpio/gpio%d/direction", gpio_num);
fp = fopen(file_name, "rb+");
if (fp == NULL)
{
printf("Cannot open %s.\n", file_name);
return -1;
}
fprintf(fp, "out");
fclose(fp);
sprintf(file_name, "/sys/class/gpio/gpio%d/value", gpio_num);
fp = fopen(file_name, "rb+");
if (fp == NULL)
{
printf("Cannot open %s.\n", file_name);
return -1;
}
if (gpio_out_val)
strcpy(buf,"1");
else
strcpy(buf,"0");
fwrite(buf, sizeof(char), sizeof(buf) - 1, fp);
printf("%s: gpio%d_%d = %s\n", __func__,gpio_chip_num, gpio_offset_num, buf);
fclose(fp);
sprintf(file_name, "/sys/class/gpio/unexport");
fp = fopen(file_name, "w");
if (fp == NULL)
{
printf("Cannot open %s.\n", file_name);
return -1;
}
fprintf(fp, "%d", gpio_num);
fclose(fp);
return 0;
}
/********************
*
*
* H3引脚初始化
*
*
* ****************/
T_JZsdkReturnCode H3_ircut_init()
{
#ifdef WIRINGPI_STATUS_ON
//wiringPi库的初始化
wiringPiSetup() ;
//静音引脚初始化
pinMode (AMPLIFIER_IRCUT_PIN_1, OUTPUT) ;
digitalWrite(AMPLIFIER_IRCUT_PIN_1,LOW);
pinMode (AMPLIFIER_IRCUT_PIN_2, OUTPUT) ;
digitalWrite(AMPLIFIER_IRCUT_PIN_2,LOW);
//H10要初始化电机引脚
if (DEVICE_VERSION == JZ_H10)
{
pinMode(MOTOR_IRCUT_PIN,PWM_OUTPUT);//设置输出模式
JZSDK_LOG_INFO("H3 gimbal Init Complete");
}
JZSDK_LOG_INFO("H3 Init Complete");
#endif
return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
/***********************
*
*
* H3 pwm控制
*
* ***************************/
T_JZsdkReturnCode H3_ircut_pwm(int pwm_duty)
{
#ifdef WIRINGPI_STATUS_ON
pwmWrite(MOTOR_IRCUT_PIN,pwm_duty);
//printf("pwm控制:%d\n",pwm_duty);
#endif
}
/***********************
*
* H3引脚控制
* 静音角控制
*
* *******************/
T_JZsdkReturnCode H3_ircut_set_amplifier(int ircut_mode)
{
#ifdef WIRINGPI_STATUS_ON
if (ircut_mode == JZ_FLAGCODE_ON)
{
digitalWrite(AMPLIFIER_IRCUT_PIN_1,HIGH);
digitalWrite(AMPLIFIER_IRCUT_PIN_2,HIGH);
}
else if (ircut_mode == JZ_FLAGCODE_OFF)
{
digitalWrite(AMPLIFIER_IRCUT_PIN_1,LOW);
digitalWrite(AMPLIFIER_IRCUT_PIN_2,LOW);
}
else
{
JZSDK_LOG_ERROR("H3引脚控制错误");
return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
}
#endif
return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}