def __init__(self): # method that inializes placeholder variables self._bus = Bus() self._MOTOR_ADDRESS = self._bus.get_motor_address() self._timer = Timer() self._distance = Distance() self._list = [] self._servo = Servo(50) # parameter in hertz
class Distance: _timer = None _bus = None def __init__(self): self._bus = Bus() self._timer = Timer() def get_distance(self): self._bus.get_bus().write_byte_data(self._bus.get_distance_address(), 0, 81) distance = self._bus.get_bus().read_word_data( self._bus.get_distance_address(), 2) / 255 return distance
def bus_phase(SourceBus): # get bus_dict from external (bus_dict, N_bus, threePbus) = Bus.Bus_gen() bus_phase_dict = dict() bus_phase_dict[SourceBus] = [1,2,3] # iterate over lines line_data = pd.read_csv("data/line data.csv", header=None) N_lines = len(line_data.index) for l in range(N_lines): to_bus = str(line_data.iloc[l, 1]) phase = [] to_PhaseA = str(to_bus) + '.1' to_PhaseB = str(to_bus) + '.2' to_PhaseC = str(to_bus) + '.3' # init the no. of phases phase = [] if to_PhaseA in bus_dict: phase.append(1) if to_PhaseB in bus_dict: phase.append(2) if to_PhaseC in bus_dict: phase.append(3) bus_phase_dict[to_bus] = phase return bus_phase_dict
def __init__(self, Vbase, SourceBus, reg_bus, reg_phase): # The Voltage base self.Vbase = Vbase # V self.SourceBus = SourceBus # Voltage regulator nodes self.reg_bus = reg_bus self.reg_Phase = reg_phase # (1) get the bus dict (self.bus_dict, self.N_bus, self.threePbus) = Bus.Bus_gen() # (2) get the Z and C bus (self.Zbus, self.Cbus) = ZandC.gen_ZC(self.Vbase) # (3) the bus -> phase dict self.bus_phase_dict = dict() self.bus_phase_dict[SourceBus] = [1, 2, 3] # (4) the bus -> downstream bus dictionary self.downstream_dict = dict() # (5) transistion bus, having both abc and 012 variables self.transistion_bus = set() # (6) line data, read from file self.line_data = pd.read_csv("data/line data.csv", header=None) self.N_lines = len(self.line_data.index) # open the file self.fd = open('ieee123_yalmip.m', 'w') # first line, MATLAB function declare self.fd.write('clear;\n') self.fd.write('clc;\n') self.fd.write('close all;\n') self.fd.write('\n')
def getAllBuses(): buses = [] rs = Bus.query.all() for i in rs: bus = Bus(i.busName, i.busStatus) buses.append(bus) return buses
def event_stream(): #TODO: Get these inputs from client: city = "IUB" #Buses whose coordinates listColloquialBusNumbers = ['3 College Mall / Bradford Place','6 Campus Shuttle - Fri','6 Limited', '9 IU Campus / College Mall / Campus Corner', 'A Route', 'B Route', 'C Route', 'D Route'] #target location coordinates targetCoordinates = (39.17159402400064, -86.51633620262146) #10th St Bloomington, IN 47408 alertDistance = 0.3 #miles #Create constans object to fetch all constant values constantsObject = Constants() constantsObject.load_constants("constants.json") #Create bus operations object busOperationsObject = BusOperations(constantsObject, city) #Create route object routeObject = Route(constantsObject, city) #Create a map of actual to colloquial bus numbers map_actual_bus_numbers_to_colloquial_bus_numbers = routeObject.get_colloquial_bus_numbers_from_actual_bus_numbers() #Make a list of bus objects listOfActualBusNumbers = routeObject.get_actual_bus_numbers(listColloquialBusNumbers) #Create bus objects listOfBusObjects = [] #Stores list of all bus objects for actualNumber in listOfActualBusNumbers: busObject = Bus(constantsObject) busObject.set_actual_number(actualNumber) listOfBusObjects.append(busObject) flag = True while flag: #gevent.sleep(2) #sleep for 2 second before updating status of each bus to avoid overloading servers with requests listOfBusObjects = busOperationsObject.updateBusStatus(listOfBusObjects) #check which buses are approaching, then track them or show them or whatever for bus in listOfBusObjects: status = bus.getBusMovementAgainstTarget(targetCoordinates) if status == constantsObject.APPROACHING: status = "APPROACHING" elif status == constantsObject.LEAVING: status = "LEAVING" else: status = "STOPPED" data = map_actual_bus_numbers_to_colloquial_bus_numbers[bus.get_actual_number()]," :",status, " is at distance: ",str(bus.getBusDistanceFromTarget(targetCoordinates))," miles" printableData = " ".join(data) gevent.sleep(2) #sleep for 2 second before updating status of each bus to avoid overloading servers with requests if status == "APPROACHING" and bus.getBusDistanceFromTarget(targetCoordinates) <= alertDistance: printableData = "ALERT! bus: "+str(map_actual_bus_numbers_to_colloquial_bus_numbers[bus.get_actual_number()])+" is near" #print type(printableData) #print printableData yield 'data: %s\n\n' % printableData
def BusCrea(): b = Bus.Bus(len(listBus)) listBus.insert(len(listBus), 1) BBus = Button(root, text="Bus " + str(b.getNumBus()), command=lambda: View(b)) Label(root, text="Bus " + str(b.getNumBus())) BBus.grid(row=1, column=int(b.getNumBus()) + 3)
def __init__(self, memory_dict): #______ Buses ______ self.Main_bus = Bus.Bus(4) self.Memory_address_bus = Bus.Bus(4) self.ALU_op_bus = Bus.Bus(1) self.Register_address_bus = Bus.Bus(1) self.Output_address_bus = Bus.Bus self.Registers = Registers.Register_bank(self.Main_bus, self.Register_address_bus) self.Memory_address_register = Registers.Memory_address_register( self.Main_bus, self.Memory_address_bus, 4) self.Memory = Memory.Memory(self.Memory_address_bus, self.Main_bus, memory_dict) self.ALU = ALU.ALU(self.Main_bus, self.Main_bus, self.ALU_op_bus) self.Output = Output.IO(self.Main_bus, self.Output_address_bus) self.halt = 0 self.instruction_count = 0
def in_cars(line, cars): cd = [] if line[0] == 0: cd = Truck() cd = in_truck(cd, line, cars) elif line[0] == 1: cd = Bus() cd = in_bus(cd, line, cars) elif line[0] == 2: cd = LightCar() cd = in_light_car(cd, line, cars)
def testAddingMessage(self): baseMessage1 = Message.Message('baseMessage1', 'odd') baseMessage1.setLabel('255') baseMessage2 = Message.Message('baseMessage2', 'odd') baseMessage2.setLabel('355') testBus = Bus.Bus('testBus') testBus.addMessage(baseMessage1) testBus.addMessage(baseMessage2) expectedDico = {(0255, None): baseMessage1, (0355, None): baseMessage2} actualDico = testBus._messages self.assertDictEqual(expectedDico, actualDico, 'Bus construct not working')
def testNoDuplicate(self): ''' Verify an exception is raised when attempting to add a message in a bus that has he same SDI/Label combinaison than a message already existing ''' baseMessage1 = Message.Message('baseMessage1', 'odd') baseMessage1.setLabel('255') baseMessage2 = Message.Message('baseMessage2', 'odd') baseMessage2.setLabel('255') testBus = Bus.Bus('testBus') testBus.addMessage(baseMessage1) self.assertRaises(Exception.A429MsgStructureError, testBus.addMessage, baseMessage2)
def pollDistance(self): #get the actual bus objects from the user specified name list pprint(self.busList) print '-'*20 listOfActualBusNumbers = self.routeObject.get_actual_bus_numbers(self.busList) print '-'*20 pprint(listOfActualBusNumbers) #Create bus objects listOfBusObjects = [] #Stores list of all bus objects for actualNumber in listOfActualBusNumbers: busObject = Bus(self.constantsObject) busObject.set_actual_number(actualNumber) listOfBusObjects.append(busObject) #Create a map of actual to colloquial bus numbers map_actual_bus_numbers_to_colloquial_bus_numbers = self.routeObject.get_colloquial_bus_numbers_from_actual_bus_numbers() while True: time.sleep(2) #sleep for 2 second before updating status of each bus listOfBusObjects = self.busOperationsObject.updateBusStatus(listOfBusObjects) #check which buses are approaching, then track them or show them or whatever for bus in listOfBusObjects: status = bus.getBusMovementAgainstTarget(self.targetCoordinates) if status == self.constantsObject.APPROACHING: status = "APPROACHING" elif status == self.constantsObject.LEAVING: status = "LEAVING" else: status = "STOPPED" currentDist = bus.getBusDistanceFromTarget(self.targetCoordinates) if currentDist <= 0.4: #send notification & remove it from the list #TODO sending notification to the client listOfBusObjects.remove(bus) yield bus.get_colloquial_number() print map_actual_bus_numbers_to_colloquial_bus_numbers[bus.get_actual_number()]," :",status, \ " is at distance: ",bus.getBusDistanceFromTarget(self.targetCoordinates)," miles"
def MATLABoutput(self): self.fd.write('vPF=zeros(' + str(self.N_bus) + ', 1);\n') busnames = Bus.getBus() for bus in busnames: ph_list = self.bus_phase_dict[bus] for i in range(len(ph_list)): node = bus + '.' + str(ph_list[i]) self.fd.write('vPF(' + str(self.bus_dict[node]) + ') = v' + bus + '(' + str(i + 1) + ',' + str(i + 1) + ');\n') self.fd.write('\n') # output the voltage profile self.fd.write('\n') self.fd.write('disp(diag(S150R149) / 1000)')
def __init__(self,program): self.bMC = Bus.Bus(1) self.bMAD = Bus.Bus(4) self.bAC = Bus.Bus(1) self.bAD = Bus.Bus(4) self.bRC = Bus.Bus(2) self.bRD = Bus.Bus(4) self.Memory = Memory.Memory(program,self.bMC,self.bMAD) self.Registers = Register.Register_bank(self.bRC,self,bRD) self.ALU = ALU.ALU(self.bAC,self.bAD)
def setBusController(self, bc=None): if not isinstance(bc, busC.BusController) or bc == None: raise ValueError( "Satellite.setBusController: bc is not a BusController instance" ) # based on this value the method returns a true or a false oldVal = self.busController self.busController = bc self.bus = b.Bus() # sets remote terminals in the satellite bc.setRemoteTerminals(self.getRemoteTerminal()) # associates bus to bus controller bc.setBus(self.bus) if oldVal == None: return True else: return False
def test(self): testBus = Bus.Bus('testBus') testMessage1 = Message.Message('baseMessage', 'odd') testMessage1.setLabel('257') testMessage1.addField( DiscreteBit.Field(10, 'testBit', 'test bit is happy', 'test bit is not happy')) testMessage1.setFieldValueByName('testBit', True) testMessage1.addField( DiscreteBit.Field(15, 'bobo', 'test bit is happy', 'test bit is not happy')) testBus.addMessage(testMessage1) with open("/home/nicolas/Desktop/test.xml", 'w') as XMLFile: XMLSerializer.serialize(XMLFile, testBus, True)
def gen_Y(Vbase): # call bus_gen function # get the bus dict , N_bus and 3-phase bus (bus_dict, N_bus, threePbus) = Bus.Bus_gen() ## init the Zbus and Cbus matrix Zbus = np.zeros((N_bus, N_bus), dtype=complex) Cbus = np.zeros((N_bus, N_bus), dtype=complex) # add line data to Zbus and Cbus (Zbus, Cbus) = ZC_line(bus_dict, Vbase, threePbus, Zbus, Cbus) # add transformer data to Zbus and Cbus (Zbus, Cbus) = ZC_transformer(bus_dict, Vbase, threePbus, Zbus, Cbus) # add capacitor data to Zbus and Cbus (Zbus, Cbus) = ZC_capacitor(bus_dict, Vbase, threePbus, Zbus, Cbus) # convert to Ybus from Zbus and Cbus Ybus = np.linalg.inv(Zbus) + Cbus sio.savemat('data/Ybus.mat', {"Ybus": Ybus}) return (Ybus)
def add_bus(self, name, seats): new_bus = Bus.Bus() new_bus.name = name new_bus.seats = seats Bus.Bus().add_bus(new_bus)
def edit_bus(self, id, name, seats): Bus.Bus().edit_bus(id, name, seats)
def get_bus(self, id): return Bus.Bus().get_bus(id)
def get_buses(self): return Bus.Bus().get_buses()
def delete_bus(self, id): Bus.Bus().remove_bus(id)
from Bus import * busParser = Bus("3d9bf006" , "9975bb88bdfb3578c3cd3e1448a9d621") stopInformation = busParser.getStopInformation("490015209D") for i in stopInformation["departures"]: print i
def v_DER_sensivity(der_list, f): # downstream and upstream dict (downstream_dict, upstream_dict) = bus_trace(SourceBus) # get bus_dict busnames = Bus.getBus() (bus_dict, N_bus, threePbus) = Bus.Bus_gen() print(bus_dict) # impedance between bus and DER for bus in busnames: # check if any DER is downstream of the bus hasDER = False for der in der_list: if bus in upstream_dict[der]: hasDER = True # hasDER is true, the bus has downstream DER if hasDER: for der in der_list: if bus in upstream_dict[der]: f.write('Ze' + bus + der + ' = ') length = len(upstream_dict[der]) for i in range(length-1): bus_f = upstream_dict[der][i] bus_t = upstream_dict[der][i+1] f.write('Ze' + bus_f + bus_t + ' + ') # last line segment, connected to DER bus_f = upstream_dict[der][-1] f.write('Ze' + bus_f + der + ' + ') f.write('0;\n') # delta V nodeA = bus + '.1' nodeB = bus + '.2' nodeC = bus + '.3' if nodeA in bus_dict.keys(): f.write('Cons = [Cons, DeltaV(' + str(bus_dict[nodeA]) + ') == ') for der in der_list: if bus in upstream_dict[der]: f.write('DER' + der + '(1) * Ze' + bus + der + "(1, 1)' + Ze" + bus + der + '(1, 1) * DER' + der + "(1)' + ") f.write('0];\n') if nodeB in bus_dict.keys(): f.write('Cons = [Cons, DeltaV(' + str(bus_dict[nodeB]) + ') == ') for der in der_list: if bus in upstream_dict[der]: f.write('DER' + der + '(2) * Ze' + bus + der + "(2, 2)' + Ze" + bus + der + '(2, 2) * DER' + der + "(2)' + ") f.write('0];\n') if nodeC in bus_dict.keys(): f.write('Cons = [Cons, DeltaV(' + str(bus_dict[nodeC]) + ') ==') for der in der_list: if bus in upstream_dict[der]: f.write('DER' + der + '(3) * Ze' + bus + der + "(3, 3)' + Ze" + bus + der + '(3, 3) * DER' + der + "(3)' + ") f.write('0];\n') f.write('\n') # finally the voltage constraint f.write('Cons = [Cons, v_lb <= vPF + DeltaV <= v_ub];\n')
def __init__(self): self._bus = Bus() self._timer = Timer()
def spawn_bus(self, location): """ Create a new bus, spawning it at the specified location :param location: the location where to spawn the new bus """ self.buses.append(B.Bus(location, self.p_bus_advancement))
def __init__(self, memory_dict): #______ Buses ______ self.Main_bus = Bus.Bus(4) self.Memory_address_bus = Bus.Bus(4) self.ALU_op_bus = Bus.Bus(1) self.Register_address_bus = Bus.Bus(1) self.Output_address_bus = Bus.Bus self.Registers = Registers.Register_bank(self.Main_bus, self.Register_address_bus) self.Memory_address_register = Registers.Memory_address_register( self.Main_bus, self.Memory_address_bus, 4) self.Memory = Memory.Memory(self.Memory_address_bus, self.Main_bus, memory_dict) self.ALU = ALU.ALU(self.Main_bus, self.Main_bus, self.ALU_op_bus) self.Output = Output.IO(self.Main_bus, self.Output_address_bus) self.halt = 0 self.instruction_count = 0 self.instr_dict = { 0: self.Halt, 1: self.Noop, 2: self.Move, 3: self.Load, 4: self.Store, 5: self.Compare_reg, 6: self.Compare_addr, 7: self.Out_reg, 8: self.Out_addr, 9: self.Outd_reg, 10: self.Outd_addr, 11: self.Load_byte, 12: self.Store_byte, 13: self.Load_word, 14: self.Store_word, 16: self.ALU_reg, 17: self.ALU_reg, 18: self.ALU_reg, 19: self.ALU_reg, 20: self.ALU_reg, 21: self.ALU_reg, 22: self.ALU_reg, 23: self.ALU_reg, 24: self.ALU_reg, 25: self.ALU_reg, 26: self.ALU_reg, 27: self.ALU_reg, 28: self.ALU_reg, 29: self.ALU_reg, 30: self.ALU_reg, 31: self.ALU_reg, 32: self.ALU_addr, 33: self.ALU_addr, 34: self.ALU_addr, 35: self.ALU_addr, 36: self.ALU_addr, 37: self.ALU_addr, 38: self.ALU_addr, 39: self.ALU_addr, 40: self.ALU_addr, 41: self.ALU_addr, 42: self.ALU_addr, 43: self.ALU_addr, 44: self.ALU_addr, 45: self.ALU_addr, 46: self.ALU_addr, 47: self.ALU_addr, 48: self.In_reg, 49: self.In_addr }
def loadApproximate(): # Voltage base Vbase = 4160 / np.sqrt(3) # get bus dict (bus_dict, Nbus, threePbus) = Bus.Bus_gen() # init the constant Z loads Yloads = np.zeros((Nbus, Nbus), dtype=complex) # init the constant PQ loads PQloads = np.zeros((Nbus), dtype=complex) # read load data from file loads = pd.read_csv('data/loads data.csv') N_loads = len(loads.index) # delta to wye alpha = np.cos(np.pi / 6) + 1j * np.sin(np.pi / 6) beta = np.cos(-np.pi / 6) + 1j * np.sin(-np.pi / 6) M = np.array([[beta, 0, alpha], [alpha, beta, 0], [0, alpha, beta] ]) / np.sqrt(3) # iterate over loads for i in range(N_loads): bus = str(loads.iloc[i, 0]) load_type = str(loads.iloc[i, 1]) # three phase load in W (convert from kW) load_A = 1000 * (float(loads.iloc[i, 2]) + 1j * float(loads.iloc[i, 3])) load_B = 1000 * (float(loads.iloc[i, 4]) + 1j * float(loads.iloc[i, 5])) load_C = 1000 * (float(loads.iloc[i, 6]) + 1j * float(loads.iloc[i, 7])) # three phase node node_A = bus + '.1' node_B = bus + '.2' node_C = bus + '.3' # Wye connection # constant PQ if load_type == 'Y-PQ': if node_A in bus_dict: PQloads[bus_dict[node_A] - 1] = load_A if node_B in bus_dict: PQloads[bus_dict[node_B] - 1] = load_B if node_C in bus_dict: PQloads[bus_dict[node_C] - 1] = load_C # constant I elif load_type == 'Y-I': # split half to PQ and other half to Z if node_A in bus_dict: PQloads[bus_dict[node_A] - 1] = load_A / 2 if node_B in bus_dict: PQloads[bus_dict[node_B] - 1] = load_B / 2 if node_C in bus_dict: PQloads[bus_dict[node_C] - 1] = load_C / 2 Y_A = load_A / (Vbase * Vbase * 2) Y_B = load_B / (Vbase * Vbase * 2) Y_C = load_C / (Vbase * Vbase * 2) if node_A in bus_dict: Yloads[bus_dict[node_A] - 1, bus_dict[node_A] - 1] = Y_A if node_B in bus_dict: Yloads[bus_dict[node_B] - 1, bus_dict[node_B] - 1] = Y_B if node_C in bus_dict: Yloads[bus_dict[node_C] - 1, bus_dict[node_C] - 1] = Y_C # constant Z elif load_type == 'Y-Z': Y_A = load_A / (Vbase * Vbase) Y_B = load_B / (Vbase * Vbase) Y_C = load_C / (Vbase * Vbase) if node_A in bus_dict: Yloads[bus_dict[node_A] - 1, bus_dict[node_A] - 1] = Y_A if node_B in bus_dict: Yloads[bus_dict[node_B] - 1, bus_dict[node_B] - 1] = Y_B if node_C in bus_dict: Yloads[bus_dict[node_C] - 1, bus_dict[node_C] - 1] = Y_C # Delta connection else: delta_load = np.array([load_A, load_B, load_C]) delta_load = np.dot(M, delta_load) # constant PQ if load_type == 'D-PQ': if node_A in bus_dict: PQloads[bus_dict[node_A] - 1] = delta_load[0] if node_B in bus_dict: PQloads[bus_dict[node_B] - 1] = delta_load[1] if node_C in bus_dict: PQloads[bus_dict[node_C] - 1] = delta_load[2] # constant Z elif load_type == 'D-Z': Y_A = delta_load[0] / (Vbase * Vbase) Y_B = delta_load[1] / (Vbase * Vbase) Y_C = delta_load[2] / (Vbase * Vbase) if node_A in bus_dict: Yloads[bus_dict[node_A] - 1, bus_dict[node_A] - 1] = Y_A if node_B in bus_dict: Yloads[bus_dict[node_B] - 1, bus_dict[node_B] - 1] = Y_B if node_C in bus_dict: Yloads[bus_dict[node_C] - 1, bus_dict[node_C] - 1] = Y_C # constant I elif load_type == 'D-I': # split half to PQ and other half to Z if node_A in bus_dict: PQloads[bus_dict[node_A] - 1] = delta_load[0] / 2 if node_B in bus_dict: PQloads[bus_dict[node_B] - 1] = delta_load[1] / 2 if node_C in bus_dict: PQloads[bus_dict[node_C] - 1] = delta_load[2] / 2 Y_A = delta_load[0] / (Vbase * Vbase * 2) Y_B = delta_load[1] / (Vbase * Vbase * 2) Y_C = delta_load[2] / (Vbase * Vbase * 2) if node_A in bus_dict: Yloads[bus_dict[node_A] - 1, bus_dict[node_A] - 1] = Y_A if node_B in bus_dict: Yloads[bus_dict[node_B] - 1, bus_dict[node_B] - 1] = Y_B if node_C in bus_dict: Yloads[bus_dict[node_C] - 1, bus_dict[node_C] - 1] = Y_C sio.savemat('data/Yloads.mat', {"Yloads": Yloads}) sio.savemat('data/PQloads.mat', {"PQloads": PQloads})
class MotorControl: _bus = None # placeholder for the Bus object, representing smbus _MOTOR_ADDRESS = None # Variable representing the address of the motor control board _MotorBD = [7, 3, 0xa5, 2, 3, 0xa5, 2] # Array representing the motors going backward _MotorL = [ 7, 3, 0xa5, 1, 3, 0xa5, 2 ] # Array representing the motors making a left turn: left (motor)wheels are going backward and right (motor)wheels backward _MotorR = [ 7, 3, 0xa5, 2, 3, 0xa5, 1 ] # Array representing the motors making a right turn: right (motor)wheels are going forward and left (motor)wheels backward _MotorFD = [7, 3, 0xa5, 1, 3, 0xa5, 1] # Array representing the motors going forward _MotorST = [7, 0, 0, 0, 0, 0, 0] # Array representing the motors stopping _Totalpower = [4, 220] # representing motor speed _Softstart = [0x91, 100, 0] # speed the motors slowly build up to _prevDirection = None # variable storing the last direction the rover went used for backtracking _timer = None # placeholder for the timer object _list = None # placeholder for an array _distance = None # placeholder for the distance object representing the distance sensor _servo = None # placeholder for the servo object representing the servomotor def __init__(self): # method that inializes placeholder variables self._bus = Bus() self._MOTOR_ADDRESS = self._bus.get_motor_address() self._timer = Timer() self._distance = Distance() self._list = [] self._servo = Servo(50) # parameter in hertz def set_up(self): gpio.setmode(gpio.BCM) # Sets the GPIO numbering mode to BCM gpio.setup( 17, gpio.IN, pull_up_down=gpio.PUD_DOWN ) # sets up pin 17 BCM to be an input in pull_up_down resistor mode self._bus.get_bus().write_i2c_block_data(self._MOTOR_ADDRESS, 0, self._Totalpower) self._bus.get_bus().write_i2c_block_data(self._MOTOR_ADDRESS, 0, self._Softstart) def forward(self): # Drive forward self._bus.get_bus().write_i2c_block_data(self._MOTOR_ADDRESS, 0, self._MotorFD) def backward(self): # Drive backward self._bus.get_bus().write_i2c_block_data(self._MOTOR_ADDRESS, 0, self._MotorBD) def left(self): # Drive left-forward self._bus.get_bus().write_i2c_block_data(self._MOTOR_ADDRESS, 0, self._MotorL) def right(self): # Drive right forward self._bus.get_bus().write_i2c_block_data(self._MOTOR_ADDRESS, 0, self._MotorR) def stop(self): # Stop driving self._bus.get_bus().write_i2c_block_data(self._MOTOR_ADDRESS, 0, self._MotorST) def set_speed(self, speed): # Set motor speed self._Totalpower[1] = speed self._bus.get_bus().write_i2c_block_data(self._MOTOR_ADDRESS, 0, self._Totalpower) self._bus.get_bus().write_i2c_block_data(self._MOTOR_ADDRESS, 0, self._Softstart) def drive(self, direction): if self._timer._start is None: self._timer.start() elif direction == "stop": self._list.append({ "direction": self._prevDirection, "time": self._timer.stop() }) print(self._prevDirection) elif self._prevDirection is not None and direction != self._prevDirection: self._list.append({ "direction": self._prevDirection, "time": self._timer.stop() }) print(self._prevDirection) self._timer.start() if direction == "forward": self.forward() if direction == "right": self.right() if direction == "left": self.left() if direction == "backward": self.backward() if direction == "stop": self.stop() self._prevDirection = direction # the method used for backtracking, used by reverse_drive() def reverse(self): for i in reversed(self._list): if i['direction'] == "forward": self.backward() if i['direction'] == "right": self.left() if i['direction'] == "left": self.right() if i['direction'] == "backward": self.forward() if i['direction'] == "stop": self.stop() # checks if interrupt is neccesary (neccesary when obstacle is detected), it stops the rover then self._timer.conditional_pause(i['time'], 20, self._distance.get_distance(), [20, 40]) self.stop() self._timer.pause(1) #self.stop() # empties the route that the rover took for a new backtracking after the backtracking button is clicked. self._list.clear() print(self._list) # method called when the backtracking button is clicked # check if list has any values and then performs backtracking using threads def reverse_drive(self): if self._list: self._servo.prepare() self._timer.pause(0.5) thread = Thread(target=self.reverse) thread1 = Thread(target=self._servo.start) thread2 = Thread(target=self._servo.stop) thread.start() thread1.start() thread.join() thread2.start()
routeObject = Route(constantsObject, city) #Create a map of actual to colloquial bus numbers map_actual_bus_numbers_to_colloquial_bus_numbers = routeObject.get_colloquial_bus_numbers_from_actual_bus_numbers() print "-"*50 print map_actual_bus_numbers_to_colloquial_bus_numbers print '-'*50 #Make a list of bus objects listColloquialBusNumbers = ['3 College Mall / Bradford Place','6 Limited','9 Limited'] listOfActualBusNumbers = routeObject.get_actual_bus_numbers(listColloquialBusNumbers) print "Colloquial no:",listColloquialBusNumbers print "Actual nos:",listOfActualBusNumbers #Create bus objects listOfBusObjects = [] #Stores list of all bus objects for actualNumber in listOfActualBusNumbers: busObject = Bus(constantsObject) busObject.set_actual_number(actualNumber) listOfBusObjects.append(busObject) while True: time.sleep(2) #sleep for 2 second before updating status of each bus listOfBusObjects = busOperationsObject.updateBusStatus(listOfBusObjects) #check which buses are approaching, then track them or show them or whatever for bus in listOfBusObjects: status = bus.getBusMovementAgainstTarget(targetCoordinates) if status == constantsObject.APPROACHING: status = "APPROACHING" elif status == constantsObject.LEAVING: status = "LEAVING" else: status = "STOPPED"