Best Python code snippet using avocado_python
smart_robot_driver.py
Source:smart_robot_driver.py
1#!/usr/bin/env python2# coding=UTF-83# Copyright (c) 2011, Willow Garage, Inc.4# All rights reserved.5#6# Developer : Lin Wei-Chih , kjoelovelife@gmail.com 7#8# Redistribution and use in source and binary forms, with or without9# modification, are permitted provided that the following conditions are met:10#11# * Redistributions of source code must retain the above copyright12# notice, this list of conditions and the following disclaimer.13# * Redistributions in binary form must reproduce the above copyright14# notice, this list of conditions and the following disclaimer in the15# documentation and/or other materials provided with the distribution.16# * Neither the name of the Willow Garage, Inc. nor the names of its17# contributors may be used to endorse or promote products derived from18# this software without specific prior written permission.19#20# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"21# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE24# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR25# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF26# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS27# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN28# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)29# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE30# POSSIBILITY OF SUCH DAMAGE.31## import library32import numpy as np33from serial import Serial34class Smart_robot():35 def __init__(self,port,baud):36 37 ## setup connect parameter38 self.port = port39 self.baud = baud40 self.connected = False41 ## setup parameter42 ## initial43 ## ( vx , vy , w ) , and speed range is 0-255 , if value=127 is stop. 44 self.stop_speed = 12745 self.vx = self.stop_speed 46 self.vy = self.stop_speed47 self.w = self.stop_speed 48 self.angle = 360 ## angle of ominibot 49 self.length = 12 ## distance between center of ominibot and center of wheels 50 self.linear_speed = np.matrix( [ [self.vx] , [self.vy] , [self.w] ] ) 51 self.radian = np.pi/18052 53 ## setup speed parameter54 self.speed_parameter_1 = np.array([ -1 * np.cos(60 * self.radian) , -1 * np.sin(60 * self.radian) , self.length ])55 self.speed_parameter_2 = np.array([ 1 , 0 , self.length ])56 self.speed_parameter_3 = np.array([ -1 * np.cos(60 * self.radian) , np.sin(60 * self.radian) , self.length ])57 self.speed_parameter = np.matrix( [ self.speed_parameter_1 , self.speed_parameter_2 , self.speed_parameter_3 ] )58 ## speed formula59 self.motor_speed = np.dot(self.speed_parameter ,self.linear_speed )60 ## set motor speed61 self.motor_A = int ( round(self.motor_speed[0] , 0 ) )62 self.motor_B = int ( round(self.motor_speed[1] , 0 ) )63 self.motor_C = int ( round(self.motor_speed[2] , 0 ) )64 ## set send signal65 self.send_signal = "$AP0:" + str(self.vx) + "X" + str(self.vy) + "Y" + str(self.w) + "A" + "360B!"66 ## default move_speed[ key word ][ (vx) ,(vy) ,(w) ]67 self.move_speed = {68 'w':(0 , 50, 0 ) , # forward69 's':(0 , 0, 0 ) , # stop 70 'x':(0 ,-50, 0 ) , # back 71 'a':(-50, 0, 0 ) , # left 72 'd':(50 , 0, 0 ) , # right 73 'q':(0 , 0, 50 ) , # rotation_counterclockwise74 'e':(0 , 0, -50 ) , # rotation_clockwise 75 }76 ## setup judge_sendSignal77 self.judge_sendSignal = self.send_signal78 ## connect port79 def connect(self):80 try:81 self.microcontroller = Serial(self.port , self.baud)82 self.connected = True83 except SerialException:84 print(" Seems like you dont connect the microcontroller with USB. Please checkit and restart this program. ")85 self.microcontroller.close86 87 ## Ultrasonic 88 def ultrasonic(self):89 self.distance = self.microcontroller.readline() 90 return self.distance 91 ## set speed 92 def set_speed(self,vx_speed,vy_speed,w_speed):93 self.move_speed = {94 'w':(0 , vy_speed , 0 ) , # forward95 's':(0 , 0, 0 ) , # stop 96 'x':(0 , -1 * vy_speed, 0 ) , # back 97 'a':(-1 * vx_speed, 0 , 0 ) , # left 98 'd':(vx_speed , 0 , 0 ) , # right 99 'q':(0 , 0, w_speed ) , # rotation_counterclockwise100 'e':(0 , 0, -1 * w_speed ) , # rotation_clockwise 101 }102 103 ## free action104 def free_speed(self , vx_speed , vy_speed , w_speed):105 self.vx = vx_speed + self.stop_speed106 self.vy = vy_speed + self.stop_speed107 self.w = w_speed + self.stop_speed 108 self.send_signal = "$AP0:" + str(self.vx) + "X" + str(self.vy) + "Y" + str(self.w) + "A" + "360B!" 109 if self.judge_sendSignal != self.send_signal: 110 print(" smart robot will free moving. \nsend: {} ".format(self.send_signal) )111 self.judge_sendSignal = self.send_signal112 if self.connected == True:113 self.microcontroller.write( bytes( self.send_signal) )114 self.vx = 0115 self.vy = 0116 self.w = 0117 ## forward [speed]Y => speed > 127 118 def go(self): 119 self.vx = self.move_speed['w'][0] + self.stop_speed120 self.vy = self.move_speed['w'][1] + self.stop_speed 121 self.w = self.move_speed['w'][2] + self.stop_speed 122 self.send_signal = "$AP0:" + str(self.vx) + "X" + str(self.vy) + "Y" + str(self.w) + "A" + "360B!" 123 if self.judge_sendSignal != self.send_signal: 124 print(" smart robot will go foward. \nsend: {} ".format(self.send_signal) )125 self.judge_sendSignal = self.send_signal126 if self.connected == True:127 self.microcontroller.write( bytes( self.send_signal) )128 ## back [speed]Y => speed < 127129 def back(self): 130 self.vx = self.move_speed['x'][0] + self.stop_speed131 self.vy = self.move_speed['x'][1] + self.stop_speed 132 self.w = self.move_speed['x'][2] + self.stop_speed 133 self.send_signal = "$AP0:" + str(self.vx) + "X" + str(self.vy) + "Y" + str(self.w) + "A" + "360B!" 134 if self.judge_sendSignal != self.send_signal: 135 print(" smart robot will back. \nsend: {} ".format(self.send_signal) )136 self.judge_sendSignal = self.send_signal137 if self.connected == True:138 self.microcontroller.write( bytes( self.send_signal) )139 ## stop 140 def stop(self): 141 self.vx = self.move_speed['s'][0] + self.stop_speed142 self.vy = self.move_speed['s'][1] + self.stop_speed 143 self.w = self.move_speed['s'][2] + self.stop_speed 144 self.send_signal = "$AP0:" + str(self.vx) + "X" + str(self.vy) + "Y" + str(self.w) + "A" + "360B!" 145 if self.judge_sendSignal != self.send_signal: 146 print(" smart robot will stop. \nsend: {} ".format(self.send_signal) )147 self.judge_sendSignal = self.send_signal148 if self.connected == True:149 self.microcontroller.write( bytes( self.send_signal) )150 ## left [speed]X => speed < 127 151 def left(self): 152 self.vx = self.move_speed['a'][0] + self.stop_speed153 self.vy = self.move_speed['a'][1] + self.stop_speed 154 self.w = self.move_speed['a'][2] + self.stop_speed 155 self.send_signal = "$AP0:" + str(self.vx) + "X" + str(self.vy) + "Y" + str(self.w) + "A" + "360B!" 156 if self.judge_sendSignal != self.send_signal: 157 print(" smart robot will turn left. \nsend: {} ".format(self.send_signal) )158 self.judge_sendSignal = self.send_signal159 if self.connected == True:160 self.microcontroller.write( bytes( self.send_signal) )161 ## right [speed]X => speed > 127 162 def right(self): 163 self.vx = self.move_speed['d'][0] + self.stop_speed164 self.vy = self.move_speed['d'][1] + self.stop_speed 165 self.w = self.move_speed['d'][2] + self.stop_speed 166 self.send_signal = "$AP0:" + str(self.vx) + "X" + str(self.vy) + "Y" + str(self.w) + "A" + "360B!" 167 if self.judge_sendSignal != self.send_signal: 168 print(" smart robot will turn right. \nsend: {} ".format(self.send_signal) )169 self.judge_sendSignal = self.send_signal170 if self.connected == True:171 self.microcontroller.write( bytes( self.send_signal) )172 ## rotate clockwise [speed]A => speed > 127 173 def rotate_clockwise(self): 174 self.vx = self.move_speed['e'][0] + self.stop_speed175 self.vy = self.move_speed['e'][1] + self.stop_speed 176 self.w = self.move_speed['e'][2] + self.stop_speed 177 self.send_signal = "$AP0:" + str(self.vx) + "X" + str(self.vy) + "Y" + str(self.w) + "A" + "360B!" 178 if self.judge_sendSignal != self.send_signal: 179 print(" smart robot will rotate in the counterclockwise. \nsend: {} ".format(self.send_signal) )180 self.judge_sendSignal = self.send_signal181 if self.connected == True:182 self.microcontroller.write( bytes( self.send_signal) )183 ## rotate counterclockwise [speed]A => speed < 127 184 def rotate_counterclockwise(self): 185 self.vx = self.move_speed['q'][0] + self.stop_speed186 self.vy = self.move_speed['q'][1] + self.stop_speed 187 self.w = self.move_speed['q'][2] + self.stop_speed 188 self.send_signal = "$AP0:" + str(self.vx) + "X" + str(self.vy) + "Y" + str(self.w) + "A" + "360B!" 189 if self.judge_sendSignal != self.send_signal: 190 print(" smart robot will rotate in the clockwise. \nsend: {} ".format(self.send_signal) )191 self.judge_sendSignal = self.send_signal192 if self.connected == True:193 self.microcontroller.write( bytes( self.send_signal) )194 ## close port195 def disconnect(self):196 if self.connected == True:197 self.microcontroller.close...
main.py
Source:main.py
1class transistor:2 def __init__(self, _signal=False):3 self.state = bool(_signal)4 def send_signal(self, _signal):5 self.state = bool(_signal)6 return self.state7 def get_state(self):8 return self.state9class not_gate:10 def __init__(self, _signal=False):11 self.state = not bool(_signal)12 def send_signal(self, _signal):13 self.state = not bool(_signal)14 return self.state15 def get_state(self):16 return self.state17class and_gate:18 def __init__(self, _signal1=False, _signal2=False):19 self.t1 = transistor(_signal1)20 self.t2 = transistor(_signal2)21 self.state = True if self.t1.get_state() and self.t2.get_state() else False22 def send_signal(self, _signal1, _signal2):23 self.state = True if self.t1.send_signal(_signal1) and self.t2.send_signal(_signal2) else False24 return self.state25 def get_state(self):26 return self.state27class or_gate:28 def __init__(self, _signal1=False, _signal2=False):29 self.t1 = transistor(_signal1)30 self.t2 = transistor(_signal2)31 self.state = True if self.t1.get_state() or self.t2.get_state() else False32 def send_signal(self, _signal1, _signal2):33 self.state = True if self.t1.send_signal(_signal1) or self.t2.send_signal(_signal2) else False34 return self.state35 def get_state(self):36 return self.state37class xor_gate:38 def __init__(self, _signal1=False, _signal2=False):39 self.a1 = and_gate(_signal1, _signal2)40 self.o1 = or_gate(_signal1, _signal2)41 self.n1 = not_gate(self.a1.get_state())42 self.a2 = and_gate(self.n1.get_state(), self.o1.get_state())43 self.state = self.a2.get_state()44 def send_signal(self, _signal1, _signal2):45 self.state = self.a2.send_signal(self.n1.send_signal(self.a1.send_signal(_signal1, _signal2)), self.o1.send_signal(_signal1, _signal2))46 return self.state47 def get_state(self):48 return self.state49class half_adder:50 def __init__(self, _signal1=False, _signal2=False):51 self.x1 = xor_gate(_signal1, _signal2)52 self.a1 = and_gate(_signal1, _signal2)53 self.sum, self.carry = [int(self.x1.get_state()), int(self.a1.get_state())]54 def send_signal(self, _signal1=False, _signal2=False):55 self.sum, self.carry = [int(self.x1.send_signal(_signal1, _signal2)), int(self.a1.send_signal(_signal1, _signal2))]56 return [self.sum, self.carry]57 def get_state(self):58 return [self.sum, self.carry]59 def get_sum(self):60 return self.sum61 def get_carry(self):62 return self.carry63class full_adder:64 def __init__(self, _signal1 = 0, _signal2 = 0, _signal3 = 0):65 self.ha1 = half_adder(_signal1, _signal2)66 self.ha2 = half_adder(self.ha1.get_sum(), _signal3)67 self.o1 = or_gate(self.ha1.get_carry(), self.ha2.get_carry())68 self.sum, self.carry = [int(self.ha2.get_sum()), int(self.o1.get_state())]69 def send_signal(self, _signal1, _signal2, _signal3):70 self.ha1.send_signal(_signal1, _signal2)71 self.ha2.send_signal(self.ha1.get_sum(), _signal3)72 self.o1.send_signal(self.ha1.get_carry(), self.ha2.get_carry())73 self.sum, self.carry = [int(self.ha2.get_sum()), int(self.o1.get_state())]74 return [self.sum, self.carry]75 def get_state(self):76 return [self.sum, self.carry]77 def get_sum(self):78 return self.sum79 def get_carry(self):80 return self.carry81class RipCarryAdder_8:82 def __init__(self, _byte1 = [0,0,0,0,0,0,0,0], _byte2 = [0,0,0,0,0,0,0,0]):83 self.carry_flag = False84 if len(_byte1) == 8 and len(_byte2) == 8:85 self.ha1 = half_adder(int(_byte1[7]), int(_byte2[7]))86 self.fa1 = full_adder(self.ha1.get_carry(), int(_byte1[6]), int(_byte2[6]))87 self.fa2 = full_adder(self.fa1.get_carry(), int(_byte1[5]), int(_byte2[5]))88 self.fa3 = full_adder(self.fa2.get_carry(), int(_byte1[4]), int(_byte2[4]))89 self.fa4 = full_adder(self.fa3.get_carry(), int(_byte1[3]), int(_byte2[3]))90 self.fa5 = full_adder(self.fa4.get_carry(), int(_byte1[2]), int(_byte2[2]))91 self.fa6 = full_adder(self.fa5.get_carry(), int(_byte1[1]), int(_byte2[1]))92 self.fa7 = full_adder(self.fa6.get_carry(), int(_byte1[0]), int(_byte2[0]))93 self.output = [self.fa7.get_sum(), self.fa6.get_sum(), self.fa5.get_sum(), self.fa4.get_sum(), self.fa3.get_sum(), self.fa2.get_sum(), self.fa1.get_sum(), self.ha1.get_sum()]94 self.carry_flag = self.fa7.get_carry()95 if self.carry_flag:96 print("Overflow Warning!")97 else:98 print("Error: arguments must consist of eight binary bits each!")99 def add_bytes(self, _byte1, _byte2):100 if len(_byte1) == 8 and len(_byte2) == 8:101 self.ha1.send_signal(int(_byte1[7]), int(_byte2[7]))102 self.fa1.send_signal(self.ha1.get_carry(), int(_byte1[6]), int(_byte2[6]))103 self.fa2.send_signal(self.fa1.get_carry(), int(_byte1[5]), int(_byte2[5]))104 self.fa3.send_signal(self.fa2.get_carry(), int(_byte1[4]), int(_byte2[4]))105 self.fa4.send_signal(self.fa3.get_carry(), int(_byte1[3]), int(_byte2[3]))106 self.fa5.send_signal(self.fa4.get_carry(), int(_byte1[2]), int(_byte2[2]))107 self.fa6.send_signal(self.fa5.get_carry(), int(_byte1[1]), int(_byte2[1]))108 self.fa7.send_signal(self.fa6.get_carry(), int(_byte1[0]), int(_byte2[0]))109 self.output = [self.fa7.get_sum(), self.fa6.get_sum(), self.fa5.get_sum(), self.fa4.get_sum(), self.fa3.get_sum(), self.fa2.get_sum(), self.fa1.get_sum(), self.ha1.get_sum()]110 self.carry_flag = self.fa7.get_carry()111 if self.carry_flag:112 print("Overflow Warning!")113 return self.output114 else:115 print("Error: arguments must consist of eight binary bits each!")116 117 def get_state(self):118 return [self.output, self.carry_flag]119 def get_byte(self):120 return self.output121 def get_carry(self):122 return self.carry_flag123def byte_to_num_8(_byte):124 total = 0125 if len(_byte) == 8:126 for i in range(len(_byte)):127 if 0 <= int(_byte[i]) < 2:128 total += int(_byte[i])*(2**(7-i))129 else:130 print("Error: argument must be a 0 or a 1!")131 return total132 else:133 print("Error: argument must consist of eight binary bits!")134if __name__ == '__main__':135#Transistor test136 # print("transistor test:")137 # transistor1 = transistor()138 # print(f"default transistor = {transistor1.get_state()}")139 # print(f"updating transistor with signal [1]: {transistor1.send_signal(1)}")140 # print("")141#Not_gate test142 # print("not_gate test:")143 # notGate1 = not_gate()144 # print(f"default not_gate state = {notGate1.get_state()}")145 # print(f"updating not_gate with signal [1]: {notGate1.send_signal(1)}")146 # print("")147#and_gate test148 # print("and_gate test:")149 # andGate1 = and_gate()150 # print(f"default and_gate state = {andGate1.get_state()}")151 # print(f"updating and_gate with signal (1,1): {andGate1.send_signal(1,1)}")152 # print(f"updating and_gate with signal (0,1): {andGate1.send_signal(0,1)}")153 # print(f"updating and_gate with signal (1,0): {andGate1.send_signal(1,0)}")154 # print(f"updating and_gate with signal (0,0): {andGate1.send_signal(0,0)}")155 # print("")156#or_gate test157 # print("or_gate test:")158 # orGate1 = or_gate()159 # print(f"default or_gate state = {orGate1.get_state()}")160 # print(f"updating or_gate with signal (1,1): {orGate1.send_signal(1,1)}")161 # print(f"updating or_gate with signal (0,1): {orGate1.send_signal(0,1)}")162 # print(f"updating or_gate with signal (1,0): {orGate1.send_signal(1,0)}")163 # print(f"updating or_gate with signal (0,0): {orGate1.send_signal(0,0)}")164 # print("")165#xor_gate test166 # print("xor_gate test:")167 # xorGate1 = xor_gate()168 # print(f"default xor_gate state = {xorGate1.get_state()}")169 # print(f"updating xor_gate with signal (1,1): {xorGate1.send_signal(1,1)}")170 # print(f"updating xor_gate with signal (0,1): {xorGate1.send_signal(0,1)}")171 # print(f"updating xor_gate with signal (1,0): {xorGate1.send_signal(1,0)}")172 # print(f"updating xor_gate with signal (0,0): {xorGate1.send_signal(0,0)}")173 # print("")174#half_adder test175 # print("half_adder test:")176 # ha1 = half_adder()177 # print(f"default half_adder state = {ha1.get_state()}")178 # print(f"updating half_adder with signal (0,0): {ha1.send_signal(0,0)}")179 # print(f"updating half_adder with signal (0,1): {ha1.send_signal(0,1)}")180 # print(f"updating half_adder with signal (1,0): {ha1.send_signal(1,0)}")181 # print(f"updating half_adder with signal (1,1): {ha1.send_signal(1,1)}")182 # print(f"half_adder.sum = {ha1.get_sum()}, half_adder.carry = {ha1.get_carry()}")183 # print("")184#full_adder test185 # print("full_adder test:")186 # fa1 = full_adder()187 # print(f"default full_adder state = {fa1.get_state()}")188 # print(f"updating full_adder with signal (0,0,0): {fa1.send_signal(0,0,0)}")189 # print(f"updating full_adder with signal (0,0,1): {fa1.send_signal(0,0,1)}")190 # print(f"updating full_adder with signal (0,1,0): {fa1.send_signal(0,1,0)}")191 # print(f"updating full_adder with signal (1,0,0): {fa1.send_signal(1,0,0)}")192 # print(f"updating full_adder with signal (0,1,1): {fa1.send_signal(0,1,1)}")193 # print(f"updating full_adder with signal (1,1,0): {fa1.send_signal(1,1,0)}")194 # print(f"updating full_adder with signal (1,1,1): {fa1.send_signal(1,1,1)}")195 # print("")196#RipCarryAdder_8 test197 print("8 bit ripple carry adder test:")198 rca8_1 = RipCarryAdder_8()199 # print(f"default : byte = {rca8_1.get_byte()}, carry flag = {rca8_1.get_carry()}")200 byte1 = "00000001"201 byte2 = "00000110"202 rca8_1.add_bytes(byte1,byte2)203 print(f"adding {byte1} & {byte2} gives:\nbyte = {rca8_1.get_byte()}, carry flag = {rca8_1.get_carry()}")204 num1 = byte_to_num_8(byte1)205 num2 = byte_to_num_8(byte2)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!