forked from CytronTechnologies/pxt-sumobit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobot.ts
170 lines (137 loc) · 4.56 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
/*******************************************************************************
* Functions for SUMO:BIT robot
*
* Company: Cytron Technologies Sdn Bhd
* Website: http://www.cytron.io
* Email: [email protected]
*******************************************************************************/
let sumobitInitialSpeed: number;
enum SumobitCountdown {
//% block="3 seconds"
Three = 3,
//% block="5 seconds"
Five = 5,
};
enum SumobitDirection {
//% block="left"
Left = 0,
//% block="right"
Right = 1,
};
enum SumobitAttack{
//% block="testing"
Test = 0,
//% block="game"
Game = 1,
};
namespace sumobit {
/**
* 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
//% advanced=true
export function setSpeed(speed: number): void {
speed = sumobitInitialSpeed;
}
/**
* Start LED matrix countdown (5 or 3 second)
* * @param second Countdown Second eg: SumobitCountdown.Five
*/
//% group="Robot Kit"
//% weight=18
//% blockGap=8
//% blockId="sumobit_robot_countdown"
//% block="start countdown %second"
//% second.fieldOptions.decompileLiterals=true
//% advanced=true
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)
}
}
/**
* Preset backoff routine
* * @param direction Backoff turn direction eg: SumobitDirection.Right
*/
//% group="Robot Kit"
//% weight=17
//% blockGap=8
//% blockId="sumobit_robot_backoff"
//% block="backoff %direction"
//% second.fieldOptions.decompileLiterals=true
//% advanced=true
export function backoff(direction: SumobitDirection): void {
let speed = sumobitInitialSpeed
//stop motor
sumobit.stopMotor(1000)
basic.pause(50)
//reverse
sumobit.setMotorsSpeed(speed / -1, speed / -1, 9)
basic.pause(350 - speed)
switch (direction) {
case SumobitDirection.Right:
sumobit.setMotorsSpeed(speed / -1, speed / 1, 9);
case SumobitDirection.Left:
sumobit.setMotorsSpeed(speed / 1, speed / -1, 9);
}
basic.pause(380 - speed)
sumobit.stopMotor(1000)
basic.pause(50)
}
/**
* Attack routine for testing or game
* * @param
*/
//% group="Robot Kit"
//% weight=16
//% blockGap=8
//% blockId="sumobit_robot_attack"
//% block="attack %mode"
//% second.fieldOptions.decompileLiterals=true
//% advanced=true
export function attack(mode: SumobitAttack): void {
let speed = sumobitInitialSpeed
switch(mode){
case SumobitAttack.Test:
if (oppSensorDetection(2)) {
} else if (oppSensorDetection(0)) {
sumobit.setMotorsSpeed(0, speed, 9)
basic.pause(300)
} else if (oppSensorDetection(4)) {
sumobit.setMotorsSpeed(speed, 0, 9)
basic.pause(300)
} else if (oppSensorDetection(1)) {
sumobit.setMotorsSpeed(speed * 0.5, speed, 9)
basic.pause(100)
} else if (oppSensorDetection(2)) {
sumobit.setMotorsSpeed(speed, speed * 0.5, 9)
basic.pause(100)
}
case SumobitAttack.Game:
if (oppSensorDetection(2)) {
sumobit.setMotorsSpeed(speed, speed,9)
} else if (oppSensorDetection(0)) {
sumobit.setMotorsSpeed(0, speed,9)
basic.pause(300)
} else if (oppSensorDetection(4)) {
sumobit.setMotorsSpeed(speed, 0, 9)
basic.pause(300)
} else if (oppSensorDetection(1)) {
sumobit.setMotorsSpeed(speed*0.5, speed, 9)
basic.pause(100)
} else if (oppSensorDetection(2)) {
sumobit.setMotorsSpeed(speed, speed*0.5, 9)
basic.pause(100)
}
}
}
}