-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathmotor.c
189 lines (162 loc) · 3.33 KB
/
motor.c
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
#include <wiringPi.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <softPwm.h>
#include <ctype.h>
#include <getopt.h>
const int speedMax = 200;
// Motor 1 1st: GPIO 0, aka pin 11
const int motor1pin1 = 22;
// Motor 1 2nd: GPIO 1, aka pin 12
const int motor1pin2 = 23;
// Motor 2 1st: GPIO 3, aka pin 15
const int motor2pin1 = 24;
// Motor 2 2nd: GPIO 4, aka pin 16
const int motor2pin2 = 25;
void motor1(int pin1, int pin2)
{
digitalWrite(motor1pin1, pin1);
digitalWrite(motor1pin2, pin2);
}
void motor2(int pin1, int pin2)
{
digitalWrite(motor2pin1, pin1);
digitalWrite(motor2pin2, pin2);
}
void brake()
{
softPwmWrite(motor1pin1, 0);
softPwmWrite(motor1pin2, 0);
softPwmWrite(motor2pin1, 0);
softPwmWrite(motor2pin2, 0);
motor1(LOW, LOW);
motor2(LOW, LOW);
}
void enablePWM(pin, speed)
{
if (0 != softPwmCreate(pin, 0, speed))
{
printf("ERROR: Cannot enable software PWM for pin %d", pin);
}
}
int init()
{
if (-1 == wiringPiSetup())
{
printf("setup wiringPi failed!\n");
return 1;
}
signal(SIGINT, brake);
// Set pin mode
pinMode(motor1pin1, OUTPUT);
pinMode(motor1pin2, OUTPUT);
pinMode(motor2pin1, OUTPUT);
pinMode(motor2pin2, OUTPUT);
//Software PWM
enablePWM(motor1pin1, speedMax);
enablePWM(motor1pin2, speedMax);
enablePWM(motor2pin1, speedMax);
enablePWM(motor2pin2, speedMax);
brake();
return 0;
}
void forward(int speed)
{
softPwmWrite(motor1pin1, speed);
softPwmWrite(motor2pin1, speed);
motor1(HIGH, LOW);
motor2(HIGH, LOW);
}
void back(int speed)
{
softPwmWrite(motor1pin2, speed);
softPwmWrite(motor2pin2, speed);
motor1(LOW, HIGH);
motor2(LOW, HIGH);
}
right(int speed)
{
softPwmWrite(motor1pin2, speed);
softPwmWrite(motor2pin2, 0);
motor1(LOW, HIGH);
motor2(LOW, LOW);
}
left(int speed)
{
softPwmWrite(motor1pin2, 0);
softPwmWrite(motor2pin2, speed);
motor1(LOW, LOW);
motor2(LOW, HIGH);
}
int main(int argc, char **argv)
{
char *direction = "";
opterr = 0;
int argument = 0;
int speed = 80;
while ((argument = getopt (argc, argv, "d:s:")) != -1)
{
switch (argument)
{
case 'd':
direction = optarg;
break;
case 's':
speed = atoi(optarg);
break;
case '?':
if (optopt == 'd')
{
fprintf (stderr, "Option -%c requires an argument: forward, back, stop, left or right.\n", optopt);
}
else if (isprint (optopt))
{
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
}
else
{
fprintf (stderr,
"Unknown option character `\\x%x'.\n",
optopt);
}
return 1;
default:
abort();
}
}
printf("Direction: %s Speed: %d\n", direction, speed);
if (0 < init())
{
return 1;
}
if (0 == strcmp(direction, "forward"))
{
printf("Moving forward...\n");
forward(speed);
}
else if (0 == strcmp(direction, "back"))
{
printf("Moving backward...\n");
back(speed);
}
else if (0 == strcmp(direction, "left"))
{
printf("Turning left...\n");
left(speed);
}
else if (0 == strcmp(direction, "right"))
{
printf("Turning right...\n");
right(speed);
}
else
{
printf("Unknown direction. Stopping...\n");
brake();
return 3;
}
sleep(1);
brake();
return 0;
}