-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotors.c
255 lines (239 loc) · 6.84 KB
/
motors.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/**************************************************************
* sources:
* https://www.electronicwings.com/raspberry-pi/raspberry-pi-pwm-generation-using-python-and-c
* https://github.com/sbcshop/MotorShield/blob/master/PiMotor.py
**************************************************************/
// resources:
//
#include "motors.h"
#include <stdlib.h>
#include <wiringPi.h>
#include <softPwm.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#define STOP 0
#define FORWARD 1
#define LEFT 2
#define RIGHT 3
#define REVERSE 4
// #include <dos.h>
// struct for each motor configurations
// arrowpins={1:33,2:35,3:37,4:36}
// motor variable initialization
// pins 11,15,13,11,13,15,4
struct motor motor1 = {"motor1", 0, 3, 2, 0, 2, 3, 0};
struct motor *motor1Ptr = &motor1;
struct motor motor2 = {"motor2", 6, 4, 5, 6, 5, 4, 0};
struct motor *motor2Ptr = &motor2;
struct motor motor3 = {"motor3", 12, 13, 14, 12, 14, 13, 0};
struct motor *motor3Ptr = &motor3;
struct motor motor4 = {"motor4", 26, 10, 11, 26, 11, 10, 0};
struct motor *motor4Ptr = &motor4;
// params motor name, motor configuration
// it calls the initHelper function to set up the pins for the motor being initialized
int init(char motor[6], char config[7])
{
int check = -1;
if (motor == NULL || config == NULL || (strcmp(motor, "") == 0) || (strcmp(config, "") == 0))
{
printf("invalid input, ");
printf("%s motor: ", motor);
printf("%s config: ", config);
}
else if (strcmp(motor, "motor1") == 0)
{
check = initHelper(&motor1, config);
}
else if (strcmp(motor, "motor2") == 0)
{
check = initHelper(&motor2, config);
}
else if (strcmp(motor, "motor3") == 0)
{
check = initHelper(&motor3, config);
}
else if (strcmp(motor, "motor4") == 0)
{
check = initHelper(&motor4, config);
}
return check;
}
// params: pointer to the motor being initialized, configuration of the motor
// it configures the pins the motor will use.
int initHelper(struct motor *mot, char config[7])
{
// configuration 1
// using pointer type 1
if (strcmp(config, "config1") == 0)
{
(*mot).e = (*mot).config1.e;
(*mot).f = (*mot).config1.f;
(*mot).r = (*mot).config1.r;
// set the pins
pinMode((*mot).config1.e, OUTPUT);
pinMode((*mot).config1.f, OUTPUT);
pinMode((*mot).config1.r, OUTPUT);
softPwmCreate((*mot).config1.e, 0, 100); //set PWM channal with range
softPwmWrite((*mot).config1.e, 0);
// PinMode((*mot).config1.e,PWM_OUTPUT);
// pwmWrite((*mot).config.e,0)
softPwmWrite((*mot).config1.e, 0);
digitalWrite((*mot).config1.f, LOW);
digitalWrite((*mot).config1.r, LOW);
return 0;
}
// configuration 2
// using pointer type 2
if (strcmp(config, "config2") == 0)
{
mot->e = (*mot).config2.e;
mot->f = (*mot).config2.f;
mot->r = (*mot).config2.r;
// set the pins
// setting pins
pinMode(mot->config2.e, OUTPUT);
pinMode(mot->config2.f, OUTPUT);
pinMode(mot->config2.r, OUTPUT);
softPwmCreate(mot->config1.e, 0, 100); //set PWM channal with range
// PinMode(mot->config1.e,PWM_OUTPUT);
// pwmWrite(mot->config.e,0)
softPwmWrite(mot->config2.e, 0);
digitalWrite(mot->config2.f, LOW);
digitalWrite(mot->config2.r, LOW);
return 0;
}
else
{
printf("invalid config");
}
return -1;
}
// params: struc motor pointer, int speed value, string motor configuration.
// sets the configuration pins to high voltage and the pin module to the speed value.
int foward(struct motor *mot, int speed, char config[7])
{
// pwmWrite(mot->config.e,speed)
softPwmWrite(mot->e, speed);
digitalWrite(mot->f, HIGH);
digitalWrite(mot->r, LOW);
return 0;
}
// params: struc motor pointer, int speed value, string motor configuration.
// sets the configuration pins to high voltage and the pin module to the speed value.
int reverse(struct motor *mot, int speed, char config[7])
{
softPwmWrite(mot->e, speed);
digitalWrite(mot->f, LOW);
digitalWrite(mot->r, HIGH);
return 0;
}
// params: struc motor pointer, int speed value, string motor configuration.
// sets the configuration pins to 0 voltage and the pin module to 0.
int stop(struct motor *mot, char config[7])
{
// pwmWrite(mot->config.e,speed)
softPwmWrite(mot->e, 0);
digitalWrite(mot->f, LOW);
digitalWrite(mot->r, LOW);
return 0;
}
int allForward(int i)
{
foward(&motor1, 26, "config2");
foward(&motor2, 30, "config1");
foward(&motor3, 26, "config1");
foward(&motor4, 26, "config2");
delay(80);
foward(&motor1, i, "config2");
foward(&motor2, i, "config1");
foward(&motor3, i, "config1");
foward(&motor4, i, "config2");
return 0;
}
int allReverse(int i)
{
reverse(&motor1, 26, "config1");
reverse(&motor2, 20, "config2");
reverse(&motor3, 26, "config1");
reverse(&motor4, 26, "config2");
delay(80);
reverse(&motor1, i, "config1");
reverse(&motor2, i, "config2");
reverse(&motor3, i, "config1");
reverse(&motor4, i, "config2");
return 0;
}
int left(int i)
{
reverse(&motor1, 60, "config1");
foward(&motor2, 60, "config2");
reverse(&motor3, 60, "config1");
foward(&motor4, 60, "config2");
delay(80);
reverse(&motor1, i, "config1");
foward(&motor2, i, "config2");
reverse(&motor3, i, "config1");
foward(&motor4, i, "config2");
return 0;
}
int right(int i)
{
foward(&motor1, 60, "config1");
reverse(&motor2, 60, "config2");
foward(&motor3, 60, "config1");
reverse(&motor4, 60, "config2");
delay(80);
foward(&motor1, i, "config1");
reverse(&motor2, i, "config2");
foward(&motor3, i, "config1");
reverse(&motor4, i, "config2");
return 0;
}
int stopAll()
{
stop(&motor1, "config1");
stop(&motor2, "config2");
stop(&motor3, "config1");
stop(&motor4, "config2");
}
/*
param: pointer to the action in main
*/
int run(int *action)
{
int m1 = init("motor1", "config2");
int m2 = init("motor2", "config1");
int m3 = init("motor3", "config1");
int m4 = init("motor4", "config2");
// speed
int i = 10;
int prev =0;
while (*action != -1)
{
if (*action == FORWARD && prev != FORWARD)
{
allForward(i);
}
if (*action == REVERSE && prev != REVERSE)
{
allReverse(i);
}
if (*action == LEFT && prev != LEFT)
{
//turn left
left(i);
}
if (*action == RIGHT && prev != RIGHT)
{
//turn right
right(i);
}
if (*action == STOP && prev != STOP)
{
stopAll();
}
prev = *action;
}
return 0;
}