/
RobotMotionService.kt
195 lines (173 loc) · 9.42 KB
/
RobotMotionService.kt
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
package app.service.robot
import app.model.robot.Servo
import app.model.orientation.RelativePosition
import app.model.robot.constants.ServoState
import app.model.robot.constants.ServoPositions
import org.koin.core.KoinComponent
import org.koin.core.inject
import org.slf4j.LoggerFactory
import java.util.*
import kotlin.collections.HashMap
/*
Second layer of the driver
Manages movements, and medium-level operations. (Turn a hand, etc.)
*/
class RobotMotionService : KoinComponent {
var servos = HashMap<ServoPositions, Servo>()
val servoControler : ServoService by inject()
val movementHandSleep: Long = 600
val movementArmSleep: Long = 400
private val logger = LoggerFactory.getLogger(RobotMotionService::class.java)
fun init()
{
servos = servoControler.initServos()
}
fun release()
{
executeCommand(servos[ServoPositions.ARM_TOP]!!, ServoState.OUTSIDE, false)
executeCommand(servos[ServoPositions.ARM_LEFT]!!, ServoState.OUTSIDE, false)
executeCommand(servos[ServoPositions.ARM_BOTTOM]!!, ServoState.OUTSIDE, false)
executeCommand(servos[ServoPositions.ARM_RIGHT]!!, ServoState.OUTSIDE, false)
}
fun welcomePosition()
{
executeCommand(servos[ServoPositions.ARM_RIGHT]!!, ServoState.OUTSIDE, false)
executeCommand(servos[ServoPositions.ARM_LEFT]!!, ServoState.OUTSIDE, false)
executeCommand(servos[ServoPositions.ARM_TOP]!!, ServoState.OUTSIDE, false)
executeCommand(servos[ServoPositions.ARM_BOTTOM]!!, ServoState.OUTSIDE, false)
executeCommand(servos[ServoPositions.HAND_TOP]!!, ServoState.NOT_TURNED, false)
executeCommand(servos[ServoPositions.HAND_BOTTOM]!!, ServoState.NOT_TURNED, false)
executeCommand(servos[ServoPositions.HAND_LEFT]!!, ServoState.NOT_TURNED, false)
executeCommand(servos[ServoPositions.HAND_RIGHT]!!, ServoState.NOT_TURNED, false)
println("Appuyez sur entrée une fois le rubik's cube entré dans son emplacement")
Scanner(System.`in`).nextLine()
executeCommand(servos[ServoPositions.HAND_BOTTOM]!!, ServoState.NOT_TURNED, false)
executeCommand(servos[ServoPositions.HAND_TOP]!!, ServoState.NOT_TURNED, false)
executeCommand(servos[ServoPositions.HAND_LEFT]!!, ServoState.NOT_TURNED, false)
executeCommand(servos[ServoPositions.HAND_RIGHT]!!, ServoState.NOT_TURNED, false)
executeCommand(servos[ServoPositions.ARM_BOTTOM]!!, ServoState.INSIDE, true)
executeCommand(servos[ServoPositions.ARM_TOP]!!, ServoState.INSIDE, false)
executeCommand(servos[ServoPositions.ARM_LEFT]!!, ServoState.INSIDE, false)
executeCommand(servos[ServoPositions.ARM_RIGHT]!!, ServoState.INSIDE, false)
println("Appuyez sur entrée pour lancer la séquence")
Scanner(System.`in`).nextLine()
}
fun turnCube(direction: RelativePosition)
{
if(direction == RelativePosition.RIGHT)
{
executeCommand(servos[ServoPositions.ARM_TOP]!!, ServoState.OUTSIDE, true)
executeCommand(servos[ServoPositions.HAND_TOP]!!, ServoState.TURNED, true)
executeCommand(servos[ServoPositions.ARM_TOP]!!, ServoState.INSIDE, true)
executeCommand(servos[ServoPositions.ARM_LEFT]!!, ServoState.OUTSIDE, false)
executeCommand(servos[ServoPositions.ARM_RIGHT]!!, ServoState.OUTSIDE, true)
executeCommand(servos[ServoPositions.HAND_TOP]!!, ServoState.NOT_TURNED, false)
executeCommand(servos[ServoPositions.HAND_BOTTOM]!!, ServoState.TURNED, true)
executeCommand(servos[ServoPositions.ARM_LEFT]!!, ServoState.INSIDE, false)
executeCommand(servos[ServoPositions.ARM_RIGHT]!!, ServoState.INSIDE, true)
executeCommand(servos[ServoPositions.ARM_BOTTOM]!!, ServoState.OUTSIDE, true)
executeCommand(servos[ServoPositions.HAND_BOTTOM]!!, ServoState.NOT_TURNED, true)
executeCommand(servos[ServoPositions.ARM_BOTTOM]!!, ServoState.INSIDE, true)
}
else if(direction == RelativePosition.LEFT)
{
executeCommand(servos[ServoPositions.ARM_BOTTOM]!!, ServoState.OUTSIDE, true)
executeCommand(servos[ServoPositions.HAND_BOTTOM]!!, ServoState.TURNED, true)
executeCommand(servos[ServoPositions.ARM_BOTTOM]!!, ServoState.INSIDE, true)
executeCommand(servos[ServoPositions.ARM_RIGHT]!!, ServoState.OUTSIDE, false)
executeCommand(servos[ServoPositions.ARM_LEFT]!!, ServoState.OUTSIDE, true)
executeCommand(servos[ServoPositions.HAND_BOTTOM]!!, ServoState.NOT_TURNED, false)
executeCommand(servos[ServoPositions.HAND_TOP]!!, ServoState.TURNED, true)
executeCommand(servos[ServoPositions.ARM_RIGHT]!!, ServoState.INSIDE, false)
executeCommand(servos[ServoPositions.ARM_LEFT]!!, ServoState.INSIDE, true)
executeCommand(servos[ServoPositions.ARM_TOP]!!, ServoState.OUTSIDE, true)
executeCommand(servos[ServoPositions.HAND_TOP]!!, ServoState.NOT_TURNED, true)
executeCommand(servos[ServoPositions.ARM_TOP]!!, ServoState.INSIDE, true)
}
else if(direction == RelativePosition.TOP)
{
executeCommand(servos[ServoPositions.ARM_LEFT]!!, ServoState.OUTSIDE, true)
executeCommand(servos[ServoPositions.HAND_LEFT]!!, ServoState.TURNED, true)
executeCommand(servos[ServoPositions.ARM_LEFT]!!, ServoState.INSIDE, true)
executeCommand(servos[ServoPositions.ARM_TOP]!!, ServoState.OUTSIDE, false)
executeCommand(servos[ServoPositions.ARM_BOTTOM]!!, ServoState.OUTSIDE, true)
executeCommand(servos[ServoPositions.HAND_RIGHT]!!, ServoState.TURNED, false)
executeCommand(servos[ServoPositions.HAND_LEFT]!!, ServoState.NOT_TURNED, true)
executeCommand(servos[ServoPositions.ARM_TOP]!!, ServoState.INSIDE, false)
executeCommand(servos[ServoPositions.ARM_BOTTOM]!!, ServoState.INSIDE, true)
executeCommand(servos[ServoPositions.ARM_RIGHT]!!, ServoState.OUTSIDE, true)
executeCommand(servos[ServoPositions.HAND_RIGHT]!!, ServoState.NOT_TURNED, true)
executeCommand(servos[ServoPositions.ARM_RIGHT]!!, ServoState.INSIDE, true)
}
else if(direction == RelativePosition.BOTTOM)
{
executeCommand(servos[ServoPositions.ARM_RIGHT]!!, ServoState.OUTSIDE, true)
executeCommand(servos[ServoPositions.HAND_RIGHT]!!, ServoState.TURNED, true)
executeCommand(servos[ServoPositions.ARM_RIGHT]!!, ServoState.INSIDE, true)
executeCommand(servos[ServoPositions.ARM_BOTTOM]!!, ServoState.OUTSIDE, false)
executeCommand(servos[ServoPositions.ARM_TOP]!!, ServoState.OUTSIDE, true)
executeCommand(servos[ServoPositions.HAND_LEFT]!!, ServoState.TURNED, false)
executeCommand(servos[ServoPositions.HAND_RIGHT]!!, ServoState.NOT_TURNED, true)
executeCommand(servos[ServoPositions.ARM_BOTTOM]!!, ServoState.INSIDE, false)
executeCommand(servos[ServoPositions.ARM_TOP]!!, ServoState.INSIDE, true)
executeCommand(servos[ServoPositions.ARM_LEFT]!!, ServoState.OUTSIDE, true)
executeCommand(servos[ServoPositions.HAND_LEFT]!!, ServoState.NOT_TURNED, true)
executeCommand(servos[ServoPositions.ARM_LEFT]!!, ServoState.INSIDE, true)
}
}
fun turnHand(positionOnRobot : ServoPositions, clockwise : Boolean)
{
if(!isEveryHandHolding())
{
println("Can't operate if hands are not holding")
}
else if(!positionOnRobot.name.startsWith("HAND", true))
{
println("Can't turn a arm like a hand")
}
else
{
var hand = servos[positionOnRobot]!!
var arm = Servo()
if(positionOnRobot == ServoPositions.HAND_TOP) arm = servos[ServoPositions.ARM_TOP]!!
if(positionOnRobot == ServoPositions.HAND_LEFT) arm = servos[ServoPositions.ARM_LEFT]!!
if(positionOnRobot == ServoPositions.HAND_BOTTOM) arm = servos[ServoPositions.ARM_BOTTOM]!!
if(positionOnRobot == ServoPositions.HAND_RIGHT) arm = servos[ServoPositions.ARM_RIGHT]!!
if(clockwise)
{
executeCommand(hand, ServoState.TURNED, true)
executeCommand(arm, ServoState.OUTSIDE, true)
executeCommand(hand, ServoState.NOT_TURNED, true)
executeCommand(arm, ServoState.INSIDE, true)
}
else
{
executeCommand(arm, ServoState.OUTSIDE, true)
executeCommand(hand, ServoState.TURNED, true)
executeCommand(arm, ServoState.INSIDE, true)
executeCommand(hand, ServoState.NOT_TURNED, true)
}
}
}
fun executeCommand(servo : Servo, positionOfServo: ServoState, wait : Boolean)
{
servoControler.moveServo(servo, positionOfServo)
if(wait)
{
if(servo.isAHand())
{
//logger.info("Waiting ! (hand)")
Thread.sleep(movementHandSleep)
}
else if(servo.isAnArm())
{
//logger.info("Waiting ! (arm)")
Thread.sleep(movementArmSleep)
}
}
}
fun isEveryHandHolding() : Boolean
{
return servos.all { it.value.getServoPosition() == ServoState.INSIDE || it.value.getServoPosition() == ServoState.NOT_TURNED }
}
}