def main(): start = timeit.default_timer() print "Looking for brick" b = nxt.find_one_brick() print "found it" l = nxt.Motor(b, nxt.PORT_B) r = nxt.Motor(b, nxt.PORT_C) m = nxt.SynchronizedMotors(l, r, 0) #move forward for i in range(3): print "image:", i m.turn(100, 14400) #take_img() time.sleep(2) for i in range(3): print "image:", i m.turn(-100, 14400) #take_img() time.sleep(2) stop = timeit.default_timer() print "time taken:", stop - start
def __init__(self, turn_ratio=0, power_up=10, power_down=-10, power_left=20, tacho_left=30, power_right=20, tacho_right=30, bluetooth=False): if bluetooth: self.sock, self.brick = nxt_bluetooth.connectCar( ) # noqa PKSM '00:16:53:17:B4:04' else: self.brick = nxt.locator.find_one_brick() self.leftMotor = nxt.Motor(self.brick, nxt.PORT_B) self.rightMotor = nxt.Motor(self.brick, nxt.PORT_A) self.both = nxt.SynchronizedMotors(self.leftMotor, self.rightMotor, turn_ratio) self.power_up = power_up self.power_down = power_down self.power_left = power_left self.tacho_left = tacho_left self.power_right = power_right self.tacho_right = tacho_right self.btCon = bluetooth
def initBrick(): global b global right global left b = nxt.find_one_brick() right = nxt.Motor(b, nxt.PORT_C) left = nxt.Motor(b, nxt.PORT_B)
def setUp(cls): ID = '00:16:53:17:EF:0A' # MAC address NXT11 cls.sock = BlueSock(ID) brick = cls.sock.connect() cls.leftMotor = nxt.Motor(brick, nxt.PORT_B) cls.rightMotor = nxt.Motor(brick, nxt.PORT_A) cls.leftMotor.reset_position(False) cls.rightMotor.reset_position(False)
def __init__(self, node_name="nxt_motion_control"): self.node_name = node_name rospy.init_node(self.node_name) rospy.Subscriber('/cmd', Command, self.new_cmd_callback) self.current_cmd = Command(0, 0) self.brick = nxt.find_one_brick() self.motor_a = nxt.Motor(self.brick, nxt.PORT_A) self.motor_b = nxt.Motor(self.brick, nxt.PORT_B)
def __init__(self): threading.Thread.__init__(self) self.stopping = False self.powerLeft = 0.0 self.powerRight = 0.0 self.miniTick = 0 self.motorPlan = [] self.brick = nxt.find_one_brick(method=nxt.locator.Method(bluetooth=False)) self.leftMotor=nxt.Motor(self.brick, nxt.PORT_C) self.rightMotor=nxt.Motor(self.brick, nxt.PORT_A)
def callback(cmd): l = nxt.Motor(b, nxt.PORT_A) r = nxt.Motor(b, nxt.PORT_B) if cmd.linear.x != 0.0: m = nxt.SynchronizedMotors(l, r, 0) #the last arg is turn ratio m.turn(90, (cmd.linear.x * 10)) #forward m.idle() elif cmd.angular.z > 0.0: m = nxt.SynchronizedMotors(r, l, 127) m.turn(90, (cmd.angular.z * 10)) m.idle() elif cmd.angular.z < 0.0: m = nxt.SynchronizedMotors(l, r, 127) m.turn(90, abs(cmd.angular.z * 10)) m.idle()
def light_callback(msg): #def light_callback(): l = nxt.Motor(b, nxt.PORT_C) i = 0 intensity = 0 global direction if direction: try: while i <= 8: l.turn(50, 20) time.sleep(.15) sample = light.get_sample() if sample > intensity: intensity = sample i = i + 1 except: print "Blocked" l.idle() direction = False else: try: while i <= 8: l.turn(-50, 20) time.sleep(.15) sample = light.get_sample() if sample > intensity: intensity = sample i = i + 1 except: print "Blocked" l.idle() direction = True pub_intensity.publish(intensity)
def move_to(self, port, power, tachocount, speedreg=1, smoothstart=0, brake=0): ''' Same as cmd(), except that the tachocount is subtracted from the motor's current position and that value is used to turn the motor. Power is -100-100, but the sign is rewritten as needed.''' tacho = nxt.Motor(self.brick, port).get_tacho().block_tacho_count tacho = tachocount-tacho tsign = int(tacho >= 0) * 2 - 1 tacho = abs(tacho) power = abs(power)*tsign self.cmd(port, power, tacho, speedreg, smoothstart, brake)
def __init__(self): self.brick = nxt.find_one_brick() #set up sensors for p in range(4): try: s = self.brick.get_sensor(p) if isinstance(s, nxt.sensor.hitechnic.Compass): self.compass = s elif isinstance(s, nxt.sensor.hitechnic.Accelerometer): self.accel = s elif isinstance(s, nxt.sensor.generic.Ultrasonic): self.us = s except: # we assume the only other thing connected is a color sensor self.color = nxt.sensor.Color20(self.brick, p) #set up motors self.m_left = nxt.Motor(self.brick, nxt.PORT_C) self.m_right = nxt.Motor(self.brick, nxt.PORT_B)
def moveCar(bk): ''' Remote control function to NXT robot with keyboard inputs. :param bk: brick :type bk: brick object ''' leftMotor = nxt.Motor(bk, nxt.PORT_B) rightMotor = nxt.Motor(bk, nxt.PORT_A) # both = nxt.SynchronizedMotors(leftMotor, rightMotor, 0) # rightboth = nxt.SynchronizedMotors(leftMotor, rightMotor, 100) # leftboth = nxt.SynchronizedMotors(rightMotor, leftMotor, 100) while True: try: if key.is_pressed('q'): print('Exiting...') break elif key.is_pressed('up'): rightMotor.weak_turn(20, 100) leftMotor.weak_turn(20, 100) elif key.is_pressed('down'): rightMotor.weak_turn(-20, 30) leftMotor.weak_turn(-20, 30) elif key.is_pressed('left'): rightMotor.weak_turn(20, 30) leftMotor.weak_turn(-20, 30) elif key.is_pressed('right'): rightMotor.weak_turn(-20, 30) leftMotor.weak_turn(20, 30) elif key.is_pressed('space'): leftMotor.idle() rightMotor.idle() leftMotor.brake() rightMotor.brake() else: pass except: break
def main(): if len(sys.argv) < 2: print "usage: python rotate2.py foldername" exit() foldername = sys.argv[1] start = timeit.default_timer() print "Looking for brick" b = nxt.find_one_brick() print "found it" l = nxt.Motor(b, nxt.PORT_B) r = nxt.Motor(b, nxt.PORT_C) m = nxt.SynchronizedMotors(l, r, 0) for i in range(100): print "image:",i m.turn(80,75) time.sleep(2) take_img() stop = timeit.default_timer() print "time taken:", stop - start execute_command("mkdir " + foldername) execute_command("mv *.jpg " + foldername)
def __enter__(self): self.brick = nxt.find_one_brick(method=nxt.locator.Method(bluetooth=False)) self.leftMotor=nxt.Motor(self.brick, nxt.PORT_C) self.rightMotor=nxt.Motor(self.brick, nxt.PORT_A) return self
# this find_one_brick method is unreliable #b = nxt.find_one_brick(host=ID) from nxt.bluesock import BlueSock # connection to first brick "JAWS" ID1 = '00:16:53:17:52:EE' # MAC address sock1 = BlueSock(ID1) b1 = sock1.connect() # connection to 2nd brick "pie" #ID2 = '00:16:53:0A:4B:2B' # MAC address #sock2 = BlueSock(ID2) #b2 = sock2.connect() mx = nxt.Motor(b1, nxt.PORT_A) # left-side my = nxt.Motor(b1, nxt.PORT_B) # right-side d = drive.Drive(mx,my) marm = nxt.Motor(b1, nxt.PORT_C) # arm motor a = arm.Arm(marm) marm.turn(power=-100,tacho_units=200,brake=True) marm.turn(power=100,tacho_units=200,brake=True) # this waved the arm around turn_arm = lambda x: marm.turn(power=-x,tacho_units=180,brake=True) [ turn_arm(_x) for _x in power ]
import time import nxt FORWARD_SPEED = 50 TURN_SPEED = 50 LEFT = -1 RIGHT = 1 brick = nxt.find_one_brick() motors = [nxt.Motor(brick, nxt.PORT_B), nxt.Motor(brick, nxt.PORT_C)] def stop(secs=0.2): for motor in motors: motor.idle() if secs: time.sleep(secs) def forward(secs=0.2): for motor in motors: motor.run(FORWARD_SPEED, regulated=True) if secs: time.sleep(secs) stop(0)
#!/usr/bin/env python3 #Script to control a NXT 2-axis CNC "Pancake maker" #Illustrates controlling more than one motor at the same time without trying to #sync them. Uses the thread module. #Written 2/3/11 by Marcus Wanner # #For more info and warnings see: #http://groups.google.com/group/nxt-python/browse_thread/thread/f6ef0865ae768ef import nxt, _thread, time b = nxt.find_one_brick() mx = nxt.Motor(b, nxt.PORT_A) my = nxt.Motor(b, nxt.PORT_B) motors = [mx, my] def turnmotor(m, power, degrees): m.turn(power, degrees) #here are the instructions... #the first value is the time to start the instruction #the second is the axis (0 for x, 1 for y) #the third is the power #the fourth is the degrees #it's probably not a good idea to run simultaneous turn #functions on a single motor, so be careful with this instructions = ( [0, 0, 80, 180], [0, 1, -40, 1080],
# checkEdge() logFile = open('robot.log', 'a+') log('') log('starting...') log('NXT brick acquiring') #nxt.locator.make_config() b = nxt.locator.find_one_brick(host=myBrickHost, debug=True) #b = nxt.locator.find_one_brick() left_motor = nxt.Motor(b, nxt.PORT_A) right_motor = nxt.Motor(b, nxt.PORT_B) left_lock = thread.allocate_lock() right_lock = thread.allocate_lock() log('NXT brick acquired') log('sensors acquiring') leftEdgeSensor = nxt.Ultrasonic(b, nxt.PORT_4, False) rightEdgeSensor = nxt.Ultrasonic(b, nxt.PORT_3, False) endSensor = nxt.Touch(b, nxt.PORT_1) log('sensors acquired')
def init_drive_motors(brick): ma = nxt.Motor(brick, nxt.PORT_A) mb = nxt.Motor(brick, nxt.PORT_B) bm = nxt.SynchronizedMotors(ma, mb, 0) return ma, mb, bm
import json import requests from flask import Flask, jsonify, abort, request, make_response from threading import Thread, Lock import logging import nxt app = Flask(__name__, static_url_path="") discoveryURL = 'http://192.168.1.80:8070/ngsi9' brokerURL = '' profile = {} subscriptionID = '' b = nxt.find_one_brick() mxA = nxt.Motor(b, nxt.PORT_A) mxB = nxt.Motor(b, nxt.PORT_B) @app.route('/notifyContext', methods=['POST']) def notifyContext(): if not request.json: abort(400) objs = readContextElements(request.json) handleNotify(objs) return jsonify({'responseCode': 200}) def readContextElements(data):
import nxt.bluesock import nxt.usbsock global motorX, motorY FPS = 5 #setting up the nxt and the cascade classifiers try: face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml') print("Attempting to connect to NXT...") b = nxt.bluesock.BlueSock('00:16:53:11:58:AB').connect() #b = nxt.usbsock.find_bricks(name="Dan's NXT") print("Connected!") print("Initializing motors...") motorX = nxt.Motor(b, nxt.PORT_A) motorY = nxt.Motor(b, nxt.PORT_B) print("Testing motors...") #motorX.run(50) #nunber from -100 to 100 #motorY.run(50) motorX.brake() motorY.brake() except: print("Something went wrong") quit() class VideoStreamWidget(object): wScreen = 0 hScreen = 0 p1 = []
# # key press info here: # https://stackoverflow.com/questions/16044229/how-to-get-keyboard-input-in-pygame from __future__ import print_function import nxt, thread, time import numpy as np import drive from nxt.bluesock import BlueSock # configure NXT brick for drive ID1 = '00:16:53:17:52:EE' # MAC address sock1 = BlueSock(ID1) b1 = sock1.connect() mx = nxt.Motor(b1, nxt.PORT_A) # left-side my = nxt.Motor(b1, nxt.PORT_B) # right-side d = drive.Drive(mx, my) import pygame pygame.init() window_w = 800 window_h = 600 white = (255, 255, 255) black = (0, 0, 0) FPS = 120
import nxt import sys import time #b = nxt.locator.find_one_brick() b = nxt.find_one_brick() mx = nxt.Motor(b, nxt.PORT_A) mx.run(int(sys.argv[1])) time.sleep(int(sys.argv[2])) mx.brake()
import nxt, thread, time b = nxt.find_one_brick() mA = nxt.Motor(b, nxt.PORT_B) mB = nxt.Motor(b, nxt.PORT_C) lux = nxt.sensor.Color20(b, nxt.PORT_4) #mA.run(100) ; mB.run(100) ; time.sleep(1); mA.brake(); mB.brake() #mA.weak_turn(100,90) while 1: intensite = lux.get_reflected_light(nxt.sensor.Type.COLORNONE) if intensite < 6: print str(intensite) + " noir" mA.run(100) mB.run(100) time.sleep(1) mA.brake() mB.brake() angle = 0 else: print str(intensite) + " blanc" angle = 10 mA.turn(100, angle) if intensite < 6: mA.run(100) mB.run(100) time.sleep(1) mA.brake() mB.brake() else: mA.turn(100, -2 * angle)
def __init__(self,Brick,PortOfDrive): threading.Thread.__init__(self) # создаем, но не запускаем поток self.brick=Brick self.PofDrv=PortOfDrive self.motor=nxt.Motor(self.brick,self.PofDrv) self.Start=False
#!/usr/bin/env python # import nxt import sys import tty, termios import nxt.locator from nxt.sensor import * from nxt.motor import * brick = nxt.locator.find_one_brick() left = nxt.Motor(brick, PORT_B) right = nxt.Motor(brick, PORT_C) both = nxt.SynchronizedMotors(left, right, 0) camera = nxt.Motor(brick, PORT_A) leftboth = nxt.SynchronizedMotors(left, right, 100) rightboth = nxt.SynchronizedMotors(right, left, 100) guideLight = nxt.Color20(brick, PORT_4) backSense = nxt.Ultrasonic(brick, PORT_1) sens = 360 sensTurn = 90 curSens = 5 power = 100 blue = Type.COLORBLUE green = Type.COLORGREEN red = Type.COLORRED off = Type.COLORNONE curCol = off guideLight.set_light_color(curCol) curCol = green camDeg = 0