display functionality
parent
677a5dc01b
commit
a50f6a6f91
|
@ -46,6 +46,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
</td>
|
</td>
|
||||||
<td>Sensor values<span id="SensorLabel"></span></td>
|
<td>Sensor values<span id="SensorLabel"></span></td>
|
||||||
</tr>
|
</tr>
|
||||||
|
<tr><th colspan="2" align="center">Display</th></tr>
|
||||||
|
<tr><td colspan="2" id="bayDisplay"></td></tr>
|
||||||
</tbody></table>
|
</tbody></table>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -108,21 +108,29 @@ class Lemming extends Robot {
|
||||||
block = 'blue'
|
block = 'blue'
|
||||||
}
|
}
|
||||||
const {distL, distR, wallL, wallR} = vals
|
const {distL, distR, wallL, wallR} = vals
|
||||||
|
this.block = block
|
||||||
if (distL < wallL - 5 || distR < wallR - 5) {
|
if (distL < wallL - 5 || distR < wallR - 5) {
|
||||||
|
this.state = 'block'
|
||||||
// if it senses a block
|
// if it senses a block
|
||||||
if (!block) return this.drive(2e-4) // no block: drive towards block?
|
if (!block) return this.drive(2e-4) // no block: drive towards block?
|
||||||
if (block == 'blue') void 0; // if blue: ignore
|
if (block == 'blue') void 0; // if blue: ignore
|
||||||
if (block == 'red') return this.turnDeg(-90) // if red: leave block
|
if (block == 'red') return this.turnDeg(-90) // if red: leave block
|
||||||
} else if (wallL < Infinity || wallR < Infinity) {
|
} else if (wallL < Infinity || wallR < Infinity) {
|
||||||
|
this.state = 'wall'
|
||||||
// no block: turn left or right
|
// no block: turn left or right
|
||||||
if (!block) return this.turnDeg(90 * (Math.random() < 0.5 ? -1 : 1))
|
if (!block) return this.turnDeg(90 * (Math.random() < 0.5 ? -1 : 1))
|
||||||
if (block == 'blue') return this.turnDeg(-90) // blue: leave
|
if (block == 'blue') return this.turnDeg(-90) // blue: leave
|
||||||
if (block == 'red') return this.turnDeg(90) // red: keep
|
if (block == 'red') return this.turnDeg(90) // red: keep
|
||||||
|
} else {
|
||||||
|
this.state = 'nothing'
|
||||||
}
|
}
|
||||||
// by default: wander
|
// by default: wander
|
||||||
this.rotate(+0.002);
|
this.rotate(+0.001);
|
||||||
this.drive(0.0002);
|
this.drive(0.0002);
|
||||||
}
|
}
|
||||||
|
getDisplay() {
|
||||||
|
return "carrying " + this.block + "\nseeing " + this.state
|
||||||
|
}
|
||||||
}
|
}
|
||||||
var sim = null
|
var sim = null
|
||||||
class Simulation {
|
class Simulation {
|
||||||
|
@ -319,6 +327,7 @@ class Bay {
|
||||||
}).join('')
|
}).join('')
|
||||||
|
|
||||||
document.getElementById('SensorLabel').innerHTML = sensorString;
|
document.getElementById('SensorLabel').innerHTML = sensorString;
|
||||||
|
document.getElementById('bayDisplay').innerHTML = robot.getDisplay()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
transformMouse({x,y}) {
|
transformMouse({x,y}) {
|
||||||
|
@ -374,6 +383,9 @@ Object.assign(Robot.prototype, {
|
||||||
*/
|
*/
|
||||||
this.body.torque = torque;
|
this.body.torque = torque;
|
||||||
},
|
},
|
||||||
|
getDisplay() {
|
||||||
|
return ""
|
||||||
|
},
|
||||||
|
|
||||||
drive(force=0) {
|
drive(force=0) {
|
||||||
/* Apply a force to the robot to move it.
|
/* Apply a force to the robot to move it.
|
||||||
|
|
Loading…
Reference in New Issue