-
Notifications
You must be signed in to change notification settings - Fork 1
/
robot.ts
215 lines (187 loc) · 6.32 KB
/
robot.ts
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
/*******************************************************************************
* Functions for SUMO:BIT robot
*
* Company: Cytron Technologies Sdn Bhd
* Website: http://www.cytron.io
* Email: [email protected]
*******************************************************************************/
enum SumobitCountdown {
//% block="3 seconds"
Three = 3,
//% block="5 seconds"
Five = 5,
};
enum SumobitDirection {
//% block="left"
Left = 0,
//% block="right"
Right = 1,
};
enum SumobitSearch {
//% block="normal"
Normal = 0,
//% block="defense"
Defense = 1,
};
namespace sumobit {
let initialSpeed: number;
let searchDirection: number = -1;
let searchMillis = control.millis();
/**
* Motor speed initialization
* @param speed Intitial Robot Speed eg: 255
*/
//% group="Robot Kit"
//% weight=19
//% blockGap=8
//% blockId="sumobit_robot_initial_speed"
//% block="set robot speed %speed"
//% speed.min=0 speed.max=255
//% second.fieldOptions.decompileLiterals=true
//% subcategory="Robot Kit"
//% blockHidden=true
export function setSpeed(speed: number): void {
speed = initialSpeed;
}
/**
* Start a countdown timer on LED (5 or 3 second).
* @param second The duration of the countdown. eg: SumobitCountdown.Five
*/
//% group="Robot Kit"
//% weight=18
//% blockGap=8
//% blockId="sumobit_robot_countdown"
//% block="start countdown %second"
//% second.defl=SumobitCountdown.Five
//% second.fieldOptions.decompileLiterals=true
//% subcategory="Robot Kit"
export function countdown(second: SumobitCountdown): void {
let startTime = control.millis();
while (control.millis() - startTime < (second - 1) * 1000) {
basic.showNumber(second - Math.round((control.millis() - startTime) / 1000))
basic.pause(210)
}
}
/**
* Backoff routine
* @param direction Backoff turn direction eg: SumobitDirection.Right
* @param speed Motor speed when reverse and turn (0-255). eg: 120
* @param acceleration Motor acceleration factor (1-9). eg: 9
*/
//% group="Robot Kit"
//% weight=17
//% blockGap=8
//% blockId="sumobit_robot_backoff"
//% block="backoff %direction speed:%speed || acceleration:%acceleration"
//% expandableArgumentMode="toggle"
//% direction.defl=SumobitDirection.Right
//% speed.min=0 speed.max=255
//% speed.defl=120
//% acceleration.min=1 acceleration.max=9
//% acceleration.defl=9
//% second.fieldOptions.decompileLiterals=true
//% subcategory="Robot Kit"
export function backoff(direction: SumobitDirection, speed: number = 120, acceleration: number = 9): void {
//stop motor
sumobit.stopMotor(1000)
basic.pause(50)
//reverse
sumobit.setMotorsSpeed(speed * -1, speed * -1, acceleration)
basic.pause(350 - speed)
//rotate robot
switch (direction) {
case SumobitDirection.Right:
searchDirection = 1;
sumobit.setMotorsSpeed(speed * 1, speed * -1, 9);
break;
case SumobitDirection.Left:
searchDirection = -1;
sumobit.setMotorsSpeed(speed * -1, speed * 1, 9);
break;
}
basic.pause(380 - speed)
sumobit.stopMotor(1000)
basic.pause(50)
}
/**
* Attack routine
* @param speed Motor speed when Front Centre sensor detects opponent (0-255). eg: 120
* @param acceleration Motor acceleration factor (1-9). eg: 9
*/
//% group="Robot Kit"
//% weight=16
//% blockGap=8
//% blockId="sumobit_robot_attack"
//% block="attack speed:%speed || acceleration:%acceleration"
//% expandableArgumentMode="toggle"
//% speed.min=0 speed.max=255
//% speed.defl=120
//% acceleration.min=1 acceleration.max=9
//% acceleration.defl=9
//% second.fieldOptions.decompileLiterals=true
//% subcategory="Robot Kit"
export function attack(speed: number = 120, acceleration: number = 9): void {
// Opponent in Front Centre
if (oppSensorDetection(2)) {
sumobit.setMotorsSpeed(speed, speed, acceleration)
}
// Opponent in Front Right
else if (oppSensorDetection(3)) {
sumobit.setMotorsSpeed(255, 0, 9)
}
// Opponent in Front Left
else if (oppSensorDetection(1)) {
sumobit.setMotorsSpeed(0, 255, 9)
}
// Opponent at Right
else if (oppSensorDetection(4)) {
sumobit.setMotorsSpeed(255, -255, 9)
}
// Opponent at Left
else if (oppSensorDetection(0)) {
sumobit.setMotorsSpeed(-255, 255, 9)
}
}
/**
* Robot search routine
* @param mode The search mode. eg: SumobitSearch.Normal
* @param speed Motor speed while searching for opponent. eg: 120
* @param acceleration Motor acceleration factor (1-9). eg: 9
*/
//% group="Robot Kit"
//% weight=15
//% blockGap=8
//% blockId="sumobit_robot_search"
//% block="search %mode speed:%speed || acceleration:%acceleration"
//% mode.defl= SumobitSearch.Normal
//% speed.min=0 speed.max=255
//% speed.defl=120
//% acceleration.min=1 acceleration.max=9
//% acceleration.defl=9
//% subcategory="Robot Kit"
//% expandableArgumentMode="toggle"
export function search(mode: SumobitSearch, speed: number = 120, acceleration: number = 9): void {
switch (mode) {
case SumobitSearch.Normal:
if (searchDirection = 1) {
// curve to right
sumobit.setMotorsSpeed(speed, speed * 0.85, acceleration)
}
else if (searchDirection = -1) {
// curve to left
sumobit.setMotorsSpeed(speed * 0.85, speed, acceleration)
}
break;
case SumobitSearch.Defense:
if (control.millis() - searchMillis > 4500) {
sumobit.setMotorsSpeed(speed, speed, acceleration)
basic.pause(50)
searchMillis = control.millis()
}
else {
sumobit.stopMotor(1000)
}
break;
}
}
}