# SimEx08.py # One light sensor, event driven from ch.aplu.nxt import NxtRobot, Gear, UltrasonicSensor, SensorPort, NxtContext, TouchSensor, Motor, MotorPort, Tools from random import randint robot = NxtRobot() jaw = Motor(MotorPort.A) us = UltrasonicSensor(SensorPort.S4) motor1 = Motor(MotorPort.B) motor2 = Motor(MotorPort.C) ts1 = TouchSensor(SensorPort.S1) ts2 = TouchSensor(SensorPort.S2) robot.addPart(jaw) robot.addPart(us) robot.addPart(ts1) robot.addPart(ts2) robot.addPart(motor1) robot.addPart(motor2) def motor_forward(motor, t): motor.stop() motor.forward() Tools.delay(t) motor.stop() def motor_backward(motor, t): motor.stop() motor.backward() Tools.delay(t) motor.stop()
# Robot basically spins around while scanning with the ultrasonic sensor, pausing to shoot # at anything it "sees" from ch.aplu.nxt import NxtRobot, Motor, MotorPort, UltrasonicSensor, SensorPort, Tools robot = NxtRobot() gunmotor = Motor(MotorPort.A) motor1 = Motor(MotorPort.B) motor2 = Motor(MotorPort.C) us = UltrasonicSensor(SensorPort.S4) robot.addPart(gunmotor) robot.addPart(motor1) robot.addPart(motor2) robot.addPart(us) def start_turn(m1, m2): m1.stop() m2.stop() m1.backward() m2.forward() def stop(m1, m2, t): m1.stop() m2.stop() Tools.delay(t) start_turn(motor1, motor2) while true: if (us.getDistance() < 80): stop(motor1, motor2, 500) gunmotor.forward()
# A simple maze-traversing two-wheeled robot # that does the additional aggressive thing of # open firing on anything that appears before it # suddenly! from ch.aplu.nxt import NxtRobot, Motor, MotorPort, UltrasonicSensor, TouchSensor, SensorPort, Tools, LightSensor robot = NxtRobot() gunmotor = Motor(MotorPort.A) motor1 = Motor(MotorPort.B) motor2 = Motor(MotorPort.C) ts1 = TouchSensor(SensorPort.S2) ts2 = TouchSensor(SensorPort.S3) us = UltrasonicSensor(SensorPort.S4) robot.addPart(gunmotor) robot.addPart(motor1) robot.addPart(motor2) robot.addPart(ts1) robot.addPart(ts2) robot.addPart(us) def move_backward(m1, m2, t): m1.stop() m2.stop() m1.backward m2.backward() Tools.delay(t) m1.stop() m2.stop()
# A cautious robot that stays still until approached from front # backs away each time it is approached from front 5 times # if it senses it is backed against the wall it starts shooting briefly # after backing away a full 5 times goes berserk and shoots all ammo left from ch.aplu.nxt import NxtRobot, Motor, MotorPort, UltrasonicSensor, TouchSensor, SensorPort, Tools, LightSensor robot = NxtRobot() gunmotor = Motor(MotorPort.A) motor1 = Motor(MotorPort.B) motor2 = Motor(MotorPort.C) ts1 = TouchSensor(SensorPort.S2) ts2 = TouchSensor(SensorPort.S3) us = UltrasonicSensor(SensorPort.S4) robot.addPart(gunmotor) robot.addPart(motor1) robot.addPart(motor2) robot.addPart(ts1) robot.addPart(ts2) robot.addPart(us) def back_away(m1, m2, t): m1.stop() m2.stop() m1.backward m2.backward() Tools.delay(t) m1.stop() m2.stop() def shoot(m, t):
# Robot basically spins around while scanning with the ultrasonic sensor, looks for the # direction with the least obstacles, and moves there from ch.aplu.nxt import NxtRobot, Motor, MotorPort, UltrasonicSensor, SensorPort, Tools robot = NxtRobot() gunmotor = Motor(MotorPort.A) motor1 = Motor(MotorPort.B) motor2 = Motor(MotorPort.C) us = UltrasonicSensor(SensorPort.S4) robot.addPart(gunmotor) robot.addPart(motor1) robot.addPart(motor2) robot.addPart(us) def start_turn(m1, m2): m1.stop() m2.stop() m1.backward() m2.forward() def stop(m1, m2, t): m1.stop() m2.stop() Tools.delay(t) def move_forward(m1, m2, t): m1.stop() m2.stop() m1.forward() m2.forward()