Skip to content

Commit f0551a6

Browse files
author
Steve Torres
committed
Initial commit for autonomy functionality
1 parent 89cf507 commit f0551a6

File tree

3 files changed

+86
-32
lines changed

3 files changed

+86
-32
lines changed

src/main/java/com/magicperf/robot/controller/RobotController.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
import org.slf4j.Logger;
66
import org.slf4j.LoggerFactory;
77
import org.springframework.beans.factory.annotation.Autowired;
8-
import org.springframework.web.bind.annotation.PostMapping;
9-
import org.springframework.web.bind.annotation.RequestBody;
10-
import org.springframework.web.bind.annotation.RequestMapping;
11-
import org.springframework.web.bind.annotation.RestController;
8+
import org.springframework.web.bind.annotation.*;
129

1310
@RestController
1411
@RequestMapping("/robot")
@@ -21,7 +18,14 @@ public class RobotController {
2118
@PostMapping("/")
2219
public void action(@RequestBody CommandModel commandModel){
2320
logger.info("-------Starting Action-------");
24-
robotService.move(commandModel.getCommand(), Long.valueOf(commandModel.getTime()));
21+
robotService.executeMove(commandModel.getCommand(), Long.valueOf(commandModel.getTime()));
2522
logger.info("-------Ending Action-------");
2623
}
24+
25+
@GetMapping("/autoEnabled")
26+
public void enableAuto(){
27+
logger.info("-------Enabling Autonomy-------");
28+
robotService.enableAuto();
29+
logger.info("-------Autonomy Enabled-------");
30+
}
2731
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.magicperf.robot.service;
22

33
public interface RobotService {
4-
void move(String command, Long time);
4+
void executeMove(String command, Long time);
5+
void enableAuto();
56
}

src/main/java/com/magicperf/robot/service/impl/RobotServiceImpl.java

+75-26
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,23 @@ public class RobotServiceImpl implements RobotService {
2323

2424
private ExecutorService executorService;
2525

26+
private Boolean autoEnabled;
27+
2628
final GpioController gpio = GpioFactory.getInstance();
2729
final GpioPinDigitalOutput input3 = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_07, PinState.LOW);
2830
final GpioPinDigitalOutput input4 = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_11, PinState.LOW);
2931
final GpioPinDigitalOutput input2 = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_13, PinState.LOW);
3032
final GpioPinDigitalOutput input1 = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_15, PinState.LOW);
3133
final GpioPinDigitalInput irInput1 = gpio.provisionDigitalInputPin(RaspiPin.GPIO_16, PinPullResistance.PULL_UP);
3234
final GpioPinDigitalInput irInput2 = gpio.provisionDigitalInputPin(RaspiPin.GPIO_04, PinPullResistance.PULL_UP);
35+
final GpioPinDigitalInput irInput3 = gpio.provisionDigitalInputPin(RaspiPin.GPIO_05, PinPullResistance.PULL_UP);
36+
final GpioPinDigitalInput irInput4 = gpio.provisionDigitalInputPin(RaspiPin.GPIO_06, PinPullResistance.PULL_UP);
37+
final GpioPinDigitalInput irInput5 = gpio.provisionDigitalInputPin(RaspiPin.GPIO_23, PinPullResistance.PULL_UP);
38+
final GpioPinDigitalInput irInput6 = gpio.provisionDigitalInputPin(RaspiPin.GPIO_25, PinPullResistance.PULL_UP);
3339

3440
@PostConstruct
3541
public void init(){
42+
autoEnabled = false;
3643
executorService = Executors.newFixedThreadPool(2);
3744
input3.setShutdownOptions(true, PinState.LOW, PinPullResistance.OFF);
3845
input4.setShutdownOptions(true, PinState.LOW, PinPullResistance.OFF);
@@ -44,89 +51,131 @@ public void init(){
4451
irInput1.addListener(new GpioPinListenerDigital(){
4552
@Override
4653
public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent gpioPinDigitalStateChangeEvent) {
47-
reverse(Long.valueOf(250));
54+
turnLeft();
55+
try {
56+
Thread.sleep(250);
57+
}catch(Exception e){
58+
logger.error(e.getMessage());
59+
}
60+
resetPins();
4861
}
4962
});
5063
irInput2.addListener(new GpioPinListenerDigital(){
5164
@Override
5265
public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent gpioPinDigitalStateChangeEvent) {
53-
forward(Long.valueOf(250));
66+
turnRight();
67+
try {
68+
Thread.sleep(250);
69+
}catch(Exception e){
70+
logger.error(e.getMessage());
71+
}
5472
}
5573
});
74+
executorService.submit(new PlaySound("Protoss/Units/Dragoon/pdrwht01.wav"));
5675
}
5776

5877
@Override
59-
public void move(String command, Long time){
78+
public void executeMove(String command, Long time){
79+
if(autoEnabled){
80+
return;
81+
}
6082
logger.info("Starting move for command: " + command);
6183
if(time == null){
6284
logger.error("Time is null!");
85+
executorService.submit(new PlaySound("Protoss/Units/Dragoon/pdrwht02.wav"));
6386
return;
6487
}
88+
String wavFilePath = null;
6589
if(StringUtils.equalsIgnoreCase(command,"W")){
6690
logger.info("Moving forward for: " + time);
67-
forward(time);
68-
executorService.submit(new PlaySound("Marine_Attack00.wav"));
91+
forward();
92+
wavFilePath = "Protoss/Units/Dragoon/pdrwht05.wav";
6993
}else if(StringUtils.equalsIgnoreCase(command,"S")){
7094
logger.info("Moving reverse for: " + time);
71-
reverse(time);
95+
reverse();
96+
wavFilePath = "Protoss/Units/Dragoon/pdrwht06.wav";
7297
}else if(StringUtils.equalsIgnoreCase(command,"A")){
7398
logger.info("Turning left for: " + time);
74-
turnLeft(time);
99+
turnLeft();
100+
wavFilePath = "Protoss/Units/Dragoon/pdrwht07.wav";
75101
}else if(StringUtils.equalsIgnoreCase(command,"D")){
76102
logger.info("Turning right for: " + time);
77-
turnRight(time);
103+
turnRight();
104+
wavFilePath = "Protoss/Units/Dragoon/pdryes01.wav";
78105
}else if(StringUtils.equalsIgnoreCase(command,"Q")){
79106
logger.info("Pivot left for: " + time);
80-
pivotLeft(time);
107+
pivotLeft();
108+
wavFilePath = "Protoss/Units/Dragoon/Marine_Attack00.wav";
81109
}else if(StringUtils.equalsIgnoreCase(command,"E")){
82110
logger.info("Pivot right for: " + time);
83-
pivotRight(time);
111+
pivotRight();
112+
wavFilePath = "Protoss/Units/Dragoon/Marine_Attack00.wav";
84113
}else{
85114
logger.error("No Command recognized!: " + command);
86115
return;
87116
}
117+
try {
118+
Thread.sleep(time);
119+
resetPins();
120+
}catch(Exception e){
121+
logger.error(e.getMessage());
122+
}
123+
executorService.submit(new PlaySound(wavFilePath));
124+
}
125+
126+
@Override
127+
public void enableAuto() {
128+
this.autoEnabled = true;
129+
forward();
88130
}
89131

90-
private void forward(Long time){
132+
private void forward(){
91133
input4.low();
92134
input2.low();
93-
input3.pulse(time,false);
94-
input1.pulse(time,false);
135+
input3.high();
136+
input1.high();
95137
}
96138

97-
private void reverse(Long time){
139+
private void reverse(){
98140
input3.low();
99141
input1.low();
100-
input4.pulse(time,false);
101-
input2.pulse(time,false);
142+
input4.high();
143+
input2.high();
102144
}
103145

104-
private void turnLeft(Long time){
146+
private void turnLeft(){
105147
input1.low();
106148
input2.low();
107149
input4.low();
108-
input3.pulse(time,false);
150+
input3.high();
109151
}
110152

111-
private void turnRight(Long time){
153+
private void turnRight(){
112154
input2.low();
113155
input3.low();
114156
input4.low();
115-
input1.pulse(time,false);
157+
input1.high();
116158
}
117159

118-
private void pivotLeft(Long time){
160+
private void pivotLeft(){
119161
input4.low();
120162
input1.low();
121-
input3.pulse(time,false);
122-
input2.pulse(time,false);
163+
input3.high();
164+
input2.high();
123165
}
124166

125-
private void pivotRight(Long time){
167+
private void pivotRight(){
126168
input3.low();
127169
input2.low();
128-
input4.pulse(time,false);
129-
input1.pulse(time,false);
170+
input4.high();
171+
input1.high();
172+
}
173+
174+
private void resetPins(){
175+
input1.low();
176+
input2.low();
177+
input3.low();
178+
input4.low();
130179
}
131180

132181
@PreDestroy

0 commit comments

Comments
 (0)