some core set adapted codes

This commit is contained in:
Peli de Halleux 2018-01-15 21:27:19 -08:00
parent e1f623a94d
commit 3c2be25384
3 changed files with 615 additions and 0 deletions

View File

@ -0,0 +1,215 @@
# Gyroboy LabView
```typescript
let mSum = 0;
let mPos = 0;
let mSpd = 0;
let mD = 0;
let mDP1 = 0;
let mDP2 = 0;
let mDP3 = 0;
let Crdv = 0;
let cLo = 0;
let gAng = 0;
let ok = false;
let pwr = 0;
let Cstr = 0;
let Cdrv = 0;
let gMn = 0;
let gMx = 0;
let gSum = 0;
let gyro = 0;
let gOS = 0;
let gSpd = 0;
let tInt = 0.014;
let lpwr = 0
let rpwr = 0
let tStart = 0
let st = 0
let oldDr = 0
function RST() {
motors.largeA.reset()
motors.largeD.reset()
motors.largeA.setRegulated(false)
motors.largeD.setRegulated(false)
sensors.gyro2.reset()
sensors.gyro2.rate()
control.timer2.reset()
loops.pause(5000)
mSum = 0;
mPos = 0;
mD = 0;
mDP1 = 0;
mDP2 = 0;
mDP3 = 0;
Crdv = 0;
cLo = 0;
gAng = 0;
ok = false;
pwr = 0;
st = 0;
Cstr = 0;
Cdrv = 0;
}
function OS() {
// OSL
do {
gMn = 1000;
gMx = -100;
gSum = 0;
// gChk
for (let i = 0; i < 200; i++) {
gyro = sensors.gyro2.rate()
gSum = gyro;
gMx = Math.max(gMx, gyro)
gMn = Math.min(gMn, gyro)
loops.pause(4);
}
} while (gMx - gMn > 2);
gOS = gSum / 200;
}
function GT() {
if (cLo == 0) {
tInt = 0.014;
control.timer1.reset();
} else {
tInt = control.timer1.seconds() / cLo;
}
cLo++;
}
function GG() {
gyro = sensors.gyro2.rate();
gOS = 0.0005 * gyro + (1 - 0.0005) * gOS
gSpd = gyro - gOS;
gAng = gAng + tInt * gSpd;
}
function GM() {
let temp = mSum
mSum = motors.largeD.angle() + motors.largeA.angle();
mD = mSum - temp;
mPos = mPos + mD;
mSpd = ((mDP1 + mDP2 + mDP3 + mD) / 4) / tInt;
mDP3 = mDP2;
mDP2 = mDP1;
mDP1 = mD;
}
function EQ() {
mPos = mPos - Cdrv * tInt;
pwr = (0.8 * gSpd + 15 * gAng) + (0.08 * mSpd + 0.12 * mPos) - 0.01 * Cdrv
if (pwr > 100) pwr = 100
else if (pwr < -100) pwr = -100
}
function cntrl() {
mPos = mPos - tInt * Cdrv
lpwr = (pwr + Cstr * 0.1)
rpwr = (pwr - Cstr * 0.1)
}
function CHK() {
if (Math.abs(pwr) < 100)
control.timer2.reset();
if (control.timer2.seconds() > 2) {
ok = true;
}
}
// M
loops.forever(function () {
RST();
brick.showImage(images.eyesSleeping)
OS()
gAng = -0.25;
music.playSoundEffect(sounds.movementsSpeedUp)
brick.showImage(images.eyesAwake)
st = 1;
// BALANCE
while (!ok) {
GT();
let t1 = control.timer1.millis()
GG();
GM();
EQ();
cntrl();
motors.largeA.setSpeed(lpwr)
motors.largeD.setSpeed(rpwr)
CHK()
let t2 = control.timer1.millis();
let p = 5 - (t2 - t1);
loops.pause(Math.max(1, p))
}
motors.stopAllMotors()
st = 0;
brick.setLight(BrickLight.RedPulse);
brick.showImage(images.eyesKnockedOut)
music.playSoundEffect(sounds.movementsSpeedDown)
sensors.touch3.pauseUntil(TouchSensorEvent.Pressed)
brick.setLight(BrickLight.Off);
})
// BHV
loops.forever(function () {
switch (st) {
case 0:
Cdrv = 0;
Cstr = 0;
break;
case 1:
Cdrv = 40;
loops.pause(4000);
Cdrv = 0;
music.playTone(1000, 100);
st = 2;
break;
case 2:
switch (sensors.color1.color()) {
case ColorSensorColor.Red:
music.playTone(2000, 100);
Cdrv = 0;
Cstr = 0;
break;
case ColorSensorColor.Green:
music.playTone(2000, 100);
Cdrv = 150;
Cstr = 0;
break;
case ColorSensorColor.Blue:
music.playTone(2000, 100);
Cstr = 70;
break;
case ColorSensorColor.Yellow:
music.playTone(2000, 100);
Cstr = -70;
break;
case ColorSensorColor.White:
music.playTone(2000, 100);
Cdrv = -75;
break;
}
if (sensors.ultrasonic4.distance() < 25) {
Cstr = 0;
oldDr = Cdrv;
Cdrv = -10;
motors.mediumC.setSpeed(30, 30, MoveUnit.Degrees);
motors.mediumC.setSpeed(-30, 60, MoveUnit.Degrees);
motors.mediumC.setSpeed(30, 30, MoveUnit.Degrees);
if (Math.randomRange(-1, 1) >= 1)
Cstr = 70;
else
Cstr = -70;
loops.pause(4000);
music.playTone(2000, 100)
Cstr = 0;
Cdrv = oldDr;
}
break;
}
loops.pause(80);
})
```

View File

@ -0,0 +1,349 @@
# Puppy
```typescript
let P_T = 0;
let ISS = 0;
let F_T = 0;
let P_C = 0;
let F_C = 0;
let DB_S = 0;
let NS = false;
let IBP = 0;
let IAP = 0;
let C = false;
let TC = false;
let OTC = false;
let COL = 0;
let OCOL = 0;
let _C = false;
let GTO = 0;
function DN() {
motors.largeAD.setBrake(true);
motors.largeAD.tank(50, 50, 1, MoveUnit.Seconds);
loops.pause(100);
motors.largeA.clearCounts()
motors.largeD.clearCounts()
}
function MNRH() {
motors.mediumC.setBrake(true)
brick.showImage(images.legoEv3icon)
brick.setLight(BrickLight.OrangePulse)
while (!brick.buttonEnter.wasPressed()) {
if (brick.buttonUp.wasPressed()) {
motors.mediumC.setSpeed(-100);
} else if (brick.buttonDown.wasPressed()) {
motors.mediumC.setSpeed(100);
} else {
motors.mediumC.stop();
}
}
motors.mediumC.stop();
motors.mediumC.clearCounts();
brick.setLight(BrickLight.Green);
}
function IS(t: number) {
ISS = t;
switch (t) {
case 0:
brick.showImage(images.eyesNeutral);
break;
case 1:
brick.showImage(images.eyesSleeping);
break;
case 2:
brick.showImage(images.eyesTear);
// draw rect...
break;
case 3:
brick.showImage(images.eyesHurt);
break;
case 4:
brick.showImage(images.eyesAngry);
break;
case 5:
brick.showImage(images.eyesTiredMiddle);
break;
case 6:
brick.showImage(images.eyesTiredRight);
break;
case 7:
brick.showImage(images.eyesTiredLeft);
break;
case 8:
brick.showImage(images.eyesLove);
break;
}
}
function UP() {
if (motors.largeA.angle() > -50) {
control.runInBackground(function () {
motors.largeD.clearCounts()
motors.largeD.setSpeed(-35);
pauseUntil(() => motors.largeD.angle() < -25);
motors.largeD.stop();
motors.largeD.setRegulated(false)
motors.largeD.setSpeed(-15)
pauseUntil(() => motors.largeD.angle() < -65);
motors.largeD.stop();
})
motors.largeA.clearCounts()
motors.largeA.setSpeed(-35);
pauseUntil(() => motors.largeA.angle() < -25);
motors.largeA.stop();
motors.largeA.setRegulated(false)
motors.largeA.setSpeed(-15)
pauseUntil(() => motors.largeA.angle() < -65);
motors.largeA.stop();
loops.pause(500);
}
}
function RST() {
P_T = Math.randomRange(3, 6);
F_T = Math.randomRange(2, 4);
P_C = 1;
F_C = 1;
control.timer1.reset();
control.timer2.reset();
control.timer3.reset();
CS(0);
}
function CS(db: number) {
if (DB_S != db) {
DB_S = db;
NS = true;
}
}
function MON() {
if (control.timer2.seconds() > 10) {
control.timer2.reset();
P_C--;
if (P_C < 0) {
P_C = 0;
}
}
if (control.timer1.seconds() > 20) {
control.timer1.reset()
F_C--;
if (F_C < 0) {
F_C = 0;
}
}
if (control.timer3.seconds() > 30) {
control.timer3.reset();
CS(1);
}
}
function UIS() {
if (control.timer5.seconds() > IBP) {
control.timer5.reset();
if (ISS == 1) {
ISS = 6;
IBP = Math.randomRange(1, 5);
} else {
ISS = 1;
IBP = 0.25;
}
IS(ISS);
}
if (control.timer6.seconds() > IAP) {
if (ISS != 1) {
control.timer6.reset();
IAP = Math.randomRange(1, 10)
if (ISS != 7) {
ISS = 7
} else {
ISS = 6;
}
IS(ISS);
}
}
}
function UPDB() {
if ((P_T == P_C) && (F_T == F_C)) {
CS(6);
}
if ((P_T > P_C) && (F_T < F_C)) {
CS(3);
}
if ((P_T < P_C) && (F_T > F_C)) {
CS(5);
}
if ((P_C == 0) && (F_C > 0)) {
CS(2)
}
if (F_C == 0) {
CS(4)
}
}
function PTC() {
C = false;
OTC = TC;
TC = sensors.touch1.isPressed()
if (TC != OTC && TC) {
P_C++;
control.timer3.reset();
if (DB_S != 4) {
IS(2);
music.playSoundEffect(sounds.animalsDogSniff);
C = true;
}
}
return C;
}
function FDC() {
OCOL = COL;
COL = sensors.color4.color();
_C = false;
if ((COL != 0) && (OCOL != COL)) {
F_C++;
_C = true;
control.timer3.reset();
IS(2);
music.playSoundEffect(sounds.expressionsCrunching)
}
return _C;
}
function IDL() {
if (NS) {
NS = false;
UP();
}
UIS();
UPDB();
PTC();
FDC();
}
function MHT(Pos: number) {
let _R = Pos - motors.mediumC.angle();
if (_R >= 0) {
motors.mediumC.setSpeed(100, _R, MoveUnit.Degrees);
} else {
motors.mediumC.setSpeed(-100, Math.abs(_R), MoveUnit.Degrees);
}
}
function SLP() {
if (NS) {
NS = false;
IS(5)
DN()
MHT(3000)
IS(1)
music.playSoundEffect(sounds.expressionsSnoring)
}
if (sensors.touch1.isPressed() || brick.buttonEnter.isPressed()) {
music.stopAllSounds();
control.timer3.reset();
CS(7);
}
}
function PLF() {
if (NS) {
NS = false
IS(0)
UP()
music.playSoundEffect(sounds.animalsDogBark2)
control.timer4.reset()
GTO = Math.randomRange(4, 8);
}
if(PTC()) {
CS(0);
}
if (control.timer4.seconds() > GTO) {
music.playSoundEffect(sounds.animalsDogBark2)
control.timer4.reset();
GTO = Math.randomRange(4, 8);
}
}
function NGR() {
NS = false
IS(4)
music.playSoundEffect(sounds.animalsDogGrowl);
UP();
loops.pause(1500);
music.stopAllSounds()
music.playSoundEffect(sounds.animalsDogBark1)
P_C--;
CS(0);
}
function HNG() {
if (NS) {
NS = false;
IS(3)
DN();
music.playSoundEffect(sounds.animalsDogWhine);
}
if(FDC()) {
CS(0)
}
if (PTC()) {
CS(3);
}
}
function PPP() {
// TODO
}
function HPY() {
// TODO
}
function WKU() {
}
DN();
MNRH();
// compare button state???
IS(1);
UP();
RST();
loops.forever(function () {
MON();
switch (DB_S) {
case 0:
IDL();
break;
case 1:
SLP();
break;
case 2:
PLF();
break;
case 3:
NGR();
break;
case 4:
HNG();
break;
case 5:
PPP();
break;
case 6:
HPY();
break;
case 7:
WKU();
break;
}
})
```

View File

@ -0,0 +1,51 @@
# Robot Arm
```typescript
function INI() {
motors.largeB.setBrake(true)
motors.largeC.setBrake(true)
motors.mediumA.setBrake(true)
motors.largeB.setSpeed(-50)
pauseUntil(() => sensors.color3.light(LightIntensityMode.Reflected) > 25);
motors.largeB.stop();
motors.mediumA.setSpeed(30, 1, MoveUnit.Seconds);
motors.mediumA.setSpeed(-50, 90, MoveUnit.Degrees);
motors.largeC.setSpeed(50)
sensors.touch1.pauseUntil(TouchSensorEvent.Pressed);
motors.largeC.setSpeed(-50, 0.86, MoveUnit.Rotations);
}
INI()
let down = false;
loops.forever(function () {
brick.showImage(images.informationQuestionMark)
brick.setLight(BrickLight.OrangePulse);
pauseUntil(() => (down = brick.buttonDown.wasPressed()) || brick.buttonUp.wasPressed())
brick.setLight(BrickLight.Off)
music.playSoundEffect(sounds.mechanicalAirRelease)
brick.showImage(images.informationAccept)
if (down) {
brick.showImage(images.informationForward)
motors.largeC.setSpeed(65, 0.85, MoveUnit.Rotations);
} else {
brick.showImage(images.informationBackward)
motors.largeC.setSpeed(-65, 0.85, MoveUnit.Rotations);
}
motors.largeB.setSpeed(20, 275, MoveUnit.Degrees)
motors.mediumA.setSpeed(30, 1, MoveUnit.Seconds)
motors.largeB.setSpeed(-55)
pauseUntil(() => sensors.color3.light(LightIntensityMode.Reflected) > 25);
motors.largeB.stop();
if (down) {
motors.largeC.setSpeed(-65, 0.86, MoveUnit.Rotations);
} else {
motors.largeC.setSpeed(65, 0.85, MoveUnit.Rotations);
}
motors.largeB.setSpeed(20, 275, MoveUnit.Degrees);
motors.mediumA.setSpeed(-30, 90, MoveUnit.Degrees);
motors.largeB.setSpeed(-55)
pauseUntil(() => sensors.color3.light(LightIntensityMode.Reflected) > 25);
motors.largeB.stop()
})
```