-
Notifications
You must be signed in to change notification settings - Fork 2
/
soccer.ino
176 lines (151 loc) · 2.89 KB
/
soccer.ino
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
/*
* copyright (c) 2023, TEAM RAW
* Divyanshu-Modi <divyan.m05@student.sfit.ac.in>
* Date: 10-09-2023
*/
#include "PS4Controller.h"
#include "stdbool.h"
/*
* struct motor
* dir_pin - Set the pin to be used to set direction of motor.
* pwm_pin - Set the pin to be used to send pwm signals to motor.
* state - To set the state to HIGH or LOW for the dir_pin.
* pwm - To set the pulse width on the pwm pin.
* invert - invert the state pins.
*/
struct motor {
uint8_t dir_pin;
uint8_t pwm_pin;
uint8_t state;
uint8_t pwm;
uint8_t channel;
bool invert;
};
/*
* Macros
*/
#define DEBUG
#define PWM_RES 8
#define PWM_FREQ 5000
#define MAX(x) map(x, 20, 128, 0, 200)
#define MIN(x) map(x, -20, -128, 0, 200)
#define MT_CTRL(id, d_pin, p_pin) \
struct motor mot##id { \
.dir_pin = d_pin, \
.pwm_pin = p_pin, \
.state = LOW, \
.pwm = 0, \
.channel = id, \
.invert = false, \
}
MT_CTRL(1, 5, 27);
MT_CTRL(2, 13, 18);
/*
* Functions
*/
void __motor_write(struct motor mot)
{
if (mot.invert) {
if (mot.state == HIGH)
mot.state = LOW:
else
mot.state = HIGH:
}
ledcWrite(mot.channel, mot.pwm);
digitalWrite(mot.dir_pin, mot.state);
}
void forward(int pwm)
{
#ifdef DEBUG
Serial.printf("%s\n", __func__);
#endif
mot1.state = HIGH;
mot2.state = HIGH;
mot1.pwm = pwm;
mot2.pwm = pwm;
__motor_write(mot1);
__motor_write(mot2);
}
void backward(int pwm)
{
#ifdef DEBUG
Serial.printf("%s\n", __func__);
#endif
mot1.state = LOW;
mot2.state = LOW;
mot1.pwm = pwm;
mot2.pwm = pwm;
__motor_write(mot1);
__motor_write(mot2);
}
void left(int pwm)
{
#ifdef DEBUG
Serial.printf("%s\n", __func__);
#endif
mot1.state = HIGH;
mot2.state = LOW;
mot1.pwm = pwm;
mot2.pwm = pwm;
__motor_write(mot1);
__motor_write(mot2);
}
void right(int pwm)
{
#ifdef DEBUG
Serial.printf("%s\n", __func__);
#endif
mot1.state = LOW;
mot2.state = HIGH;
mot1.pwm = pwm;
mot2.pwm = pwm;
__motor_write(mot1);
__motor_write(mot2);
}
void stop()
{
#ifdef DEBUG
Serial.printf("%s\n", __func__);
#endif
mot1.state = LOW;
mot2.state = LOW;
mot1.pwm = 0;
mot2.pwm = 0;
__motor_write(mot1);
__motor_write(mot2);
}
static inline void __pin_init(struct motor mot)
{
pinMode(mot.dir_pin, OUTPUT);
pinMode(mot.pwm_pin, OUTPUT);
}
static inline void __ledc_init(struct motor mot)
{
ledcAttachPin(mot.pwm_pin, mot.channel);
ledcSetup(mot.channel, PWM_FREQ, PWM_RES);
}
void setup() {
__pin_init(mot1);
__pin_init(mot2);
__ledc_init(mot1);
__ledc_init(mot2);
#ifdef DEBUG
Serial.begin(9600);
#endif
PS4.begin("78:21:84:DF:23:E8");
}
void loop() {
int LX = PS4.LStickX();
int LY = PS4.LStickY();
if (LX > 20) {
forward(MAX(LX));
} else if (LX < -20) {
backward(MIN(LX));
} else if (LY > 20) {
left(MAX(LY));
} else if (LY < -20) {
right(MIN(LY));
} else {
stop();
}
}