-
Notifications
You must be signed in to change notification settings - Fork 0
/
Draw-Robot.ts
205 lines (192 loc) · 7.91 KB
/
Draw-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
/**
* Custom blocks
*/
//% weight=100 color=#4a0087 icon=""
namespace drawRobot {
// witdth of car in m
let halveCarWidth = 0.25;
//max drive Speed in m/s
let maxSpeed = 0.4;
//speed of the Chalk 0.2 m/s
let chalkSpeed = 0.15;
//left Pull 6.5
export let leftPull = 6;
//right Pull
export let rightPull = 0;
let angleTimeMatcher = 0.4;
let stop = false;
let stoped = false;
let isStraight = false;
let rampUpFinished = false;
let steerLeft = false;
let steerRight = false;
let next = false;
//%block="Drive(Radio Group: $radioGroup )"
export function drive(radioGroup: number,path: () => void) {
radio.setGroup(radioGroup);
radio.onReceivedString(receivedString => {
if (receivedString == "stop") {
stop = !stop;
stoped = true;
}
if (receivedString == "left") {
steerLeft = true;
}
if (receivedString == "right") {
steerRight = true;
}
if(receivedString=="next"){
next = true;
}
})
path();
next = false;
Kitronik_Robotics_Board.motorOff(Kitronik_Robotics_Board.Motors.Motor1);
Kitronik_Robotics_Board.motorOff(Kitronik_Robotics_Board.Motors.Motor2);
basic.showIcon(IconNames.Skull);
}
//%block="Left(Angle: $angle °, Radius: $cmradius cm)"
//% angle.min=1 angle.max=360 angle.defl=90
//% cmradius.min=10 cmradius.max=1000 cmradius.defl=10
export function left(angle: number, cmradius: number) {
let radius = cmradius / 100;
let innerRadius = radius - halveCarWidth;
let outerRadius = radius + halveCarWidth;
let outerPath = getPath(outerRadius, angle);
let innerPath = getPath(innerRadius, angle);
let chalkpath = getPath(radius, angle);
let time = driveTime(chalkpath);
let mChalkpath = getPath(radius, angle * angleTimeMatcher)
let matchedTime = driveTime(mChalkpath);
let leftPercent = getPercent(innerPath, time) - leftPull;
let rightPercent = getPercent(outerPath, time);
isStraight = false;
driveChekStopAndConfig(matchedTime, leftPercent, rightPercent);
}
//%block="Right(Angle: $angle °, Radius $cmradius cm)"
//% angle.min=1 angle.max=360 angle.defl=90
//% cmradius.min=10 cmradius.max=1000 cmradius.defl=10
export function right(angle: number, cmradius: number) {
let radius = cmradius / 100;
let innerRadius = radius - halveCarWidth;
let outerRadius = radius + halveCarWidth;
let outerPath = getPath(outerRadius, angle);
let innerPath = getPath(innerRadius, angle);
let chalkpath = getPath(radius, angle);
let time = driveTime(chalkpath);
let mChalkpath = getPath(radius, angle * angleTimeMatcher)
let matchedTime = driveTime(mChalkpath);
let leftPercent = getPercent(innerPath, time) - leftPull;
let rightPercent = getPercent(outerPath, time);
isStraight = false;
driveChekStopAndConfig(matchedTime, leftPercent, rightPercent);
}
//%block="Straight ahead(Time: $time sec)"
//% time.min=0 time.max=60 time.defl=1
export function straight(time: number) {
isStraight = true;
driveChekStopAndConfig(time, 100 - leftPull, 100 - rightPull);
}
function getPath(radius: number, angle: number): number {
let circumference = 2 * Math.PI * radius;
let sector = angle / 360;
let sectorLength = circumference * sector
return sectorLength;
}
function getPercent(path: number, time: number) {
let speed = path / time;
let percent = (speed * 100) / maxSpeed;
return percent;
}
function driveTime(path: number): number {
let time = path / chalkSpeed;
return time;
}
function getRampupStep(motorToChek: number, checkerMotor: number){
let rampUpStep = 0;
if(motorToChek>checkerMotor){
rampUpStep = motorToChek/checkerMotor;
}
else{
rampUpStep = 1;
}
return rampUpStep;
}
function rampUp(motor1: number, motor2:number):number{
let m1Step = getRampupStep(motor1, motor2);
let m2Step = getRampupStep(motor2, motor1);
let m1Ramp = 0;
let m2Ramp = 0;
let endTime = 0;
while(!rampUpFinished){
m1Ramp += 5 * m1Step;
m2Ramp += 5 * m2Step;
Kitronik_Robotics_Board.motorOn(Kitronik_Robotics_Board.Motors.Motor1, Kitronik_Robotics_Board.MotorDirection.Forward, m1Ramp);
Kitronik_Robotics_Board.motorOn(Kitronik_Robotics_Board.Motors.Motor2, Kitronik_Robotics_Board.MotorDirection.Forward, m2Ramp);
basic.pause(100);
if (motor1 - m1Ramp < 5 || motor2 - m2Ramp < 5) {
rampUpFinished = true;
}
}
endTime = input.runningTime() / 1000;
return endTime;
}
function driveChekStopAndConfig(time: number, motor1: number, motor2: number) {
let orgMotor1 = motor1;
let orgMotor2 = motor2;
let startTime = input.runningTime()/1000
let timeNeededToRampUp = rampUp(motor1,motor2);
let passedTime = timeNeededToRampUp-startTime;
time = time - passedTime;
Kitronik_Robotics_Board.motorOn(Kitronik_Robotics_Board.Motors.Motor1, Kitronik_Robotics_Board.MotorDirection.Forward, motor1);
Kitronik_Robotics_Board.motorOn(Kitronik_Robotics_Board.Motors.Motor2, Kitronik_Robotics_Board.MotorDirection.Forward, motor2);
while (time > 0&&!next) {
if (stop) {
Kitronik_Robotics_Board.motorOff(Kitronik_Robotics_Board.Motors.Motor1);
Kitronik_Robotics_Board.motorOff(Kitronik_Robotics_Board.Motors.Motor2);
basic.pause(250);
stoped = true;
} else {
if (steerLeft) {
if(motor1<20){
motor2 = motor2 + 10;
}else{
motor1 = motor1 - 10;
}
steerLeft = false;
Kitronik_Robotics_Board.motorOn(Kitronik_Robotics_Board.Motors.Motor1, Kitronik_Robotics_Board.MotorDirection.Forward, motor1);
Kitronik_Robotics_Board.motorOn(Kitronik_Robotics_Board.Motors.Motor2, Kitronik_Robotics_Board.MotorDirection.Forward, motor2);
}
if (steerRight) {
if (motor2 < 20) {
motor1 = motor1 + 10;
} else {
motor2 = motor2 - 10;
}
steerRight = false;
Kitronik_Robotics_Board.motorOn(Kitronik_Robotics_Board.Motors.Motor1, Kitronik_Robotics_Board.MotorDirection.Forward, motor1);
Kitronik_Robotics_Board.motorOn(Kitronik_Robotics_Board.Motors.Motor2, Kitronik_Robotics_Board.MotorDirection.Forward, motor2);
}
if (motor1 > 100) {
motor1 = 100;
}
if (motor2 > 100) {
motor2 = 100;
}
if (stoped) {
Kitronik_Robotics_Board.motorOn(Kitronik_Robotics_Board.Motors.Motor1, Kitronik_Robotics_Board.MotorDirection.Forward, motor2);
Kitronik_Robotics_Board.motorOn(Kitronik_Robotics_Board.Motors.Motor2, Kitronik_Robotics_Board.MotorDirection.Forward, motor1);
stoped = false;
}
if (time < 1) {
basic.pause(time * 1000);
time = 0;
}
else {
basic.pause(500);
time = time - 0.5;
}
}
}
}
}