• API
• FAQ
• Tools
• Archive
daily pastebin goal
88%
SHARE
TWEET

Kod so posebno za k

a guest Jan 18th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #!/usr/bin/env python3
2. from ev3dev.ev3 import * #Ev3 crucial lib
3. from time import sleep #Importing the sleep only from the library time
4. import math #Import math lib for math functions
5.
6. gyro_sensor = GyroSensor() #Defining Gyro Sensor
7. left_motor = LargeMotor("outB") #Defining the LargeMotor which is  connected to the port B as left motor
8. right_motor = LargeMotor("outC") #Defining the LargeMotor which is  connected to the port C as right motor
9.
10. assert gyro_sensor.connected #Ensuring that
11. assert left_motor.connected   #everything is
12. assert right_motor.connected   #connected!
13.
14. gyro_sensor.mode = 'GYRO-ANG' #Setting the mode to gyro sense to measure angles
15. numofAtts=int(input("Input the number of graphs you want to draw:"))
16. while(numofAtts>0):
17.  k=float(input("Input k from y=k*x+n:")) #Input value for k
18.
19.  print("\n") #New line
20.
21.  n=float(input("Input n from y=k*x+n:")) #Input value for n
22.
23.
24.  if(n > 0): #If N is larger than 0
25.    left_motor.run_timed(time_sp=1000*n,speed_sp=100)
26.    right_motor.run_timed(time_sp=1000*n,speed_sp=100)
27.  if (n < 0): #If N is less than 0
28.   left_motor.run_timed(time_sp=1000*abs(n),speed_sp=-100)
29.   right_motor.run_timed(time_sp=1000*abs(n),speed_sp=-100)
30.  time.sleep(3)
31.  gyro_sensor.mode = 'GYRO-RATE'
32.  gyro_sensor.mode = 'GYRO-ANG'
33.  gcal=abs(gyro_sensor.value()) #Storing value from gyro sensor (-33,33)
34.  time.sleep(2) #Rest a bit with functioning
35.  angle=0 #At the start the angle that robot and y-axis take should be 0
36.  angleneeded=abs(math.degrees(math.atan(k))) #Computation of the angle we need to achieve (45)
37.  print(angleneeded)
38.  angle=gcal #33
39.  if k<0: #If k is less than 0 than the angle is > 90
40.
41.    while abs(angle-gcal)<90-angleneeded: #While we don't achieve the needed complementary angle
42.
43.     left_motor.run_forever(speed_sp=-10) #Move the robot to the
44.     right_motor.run_forever(speed_sp=10) #Right
45.     angle=abs(gyro_sensor.value()) #Constantly update the angle
46.     print(angle)
47.     print(gcal)
48.
49.  elif k > 0: #If k is large than 0 than the angle is < 90
50.   gcal=abs(gyro_sensor.value())
51.   angle=abs(gyro_sensor.value())
52.   #if gyro_sensor.value()<0:
53.    # angleneeded+=abs(gyro_sensor.value())
54.   while angle-gcal<abs(90-angleneeded): #While we don't achieve the needed complementary angle
55.     left_motor.run_forever(speed_sp=10) #Move the robot to the
56.     right_motor.run_forever(speed_sp=-10) #Left
57.     angle=abs(gyro_sensor.value()) #While we don't achieve the needed complementary angle
58.     print(angle)
59.     print(gcal)
60.
61.  elif k == 0: #If k is equal to 0 than the angle is 0 with x axis
62.   gcal=abs(gyro_sensor.value())
63.   angle=abs(gyro_sensor.value())
64.   #if gyro_sensor.value()<0:
65.    # angleneeded+=abs(gyro_sensor.value())
66.   while angle-gcal<abs(90-angleneeded): #While we don't achieve the needed complementary angle
67.     left_motor.run_forever(speed_sp=10) #Move the robot to the
68.     right_motor.run_forever(speed_sp=-10) #Left
69.     angle=abs(gyro_sensor.value()) #While we don't achieve the needed complementary angle
70.     print(angle)
71.     print(gcal)
72.
73.   #Assuring that we didn't made motors to move @ all
74.   left_motor.stop() #When we get out from the loop
75.   right_motor.stop()# we make the robot to stop rotating since it achieved the desired angle
76.  left_motor.run_timed(time_sp=3250,speed_sp=100,stop_action="brake") #Moving the robot to some point backwards *Both motors
77.  right_motor.run_timed(time_sp=3250,speed_sp=100,stop_action="brake") #Moving the robot to some point backwards *Both motors
78.
79.  time.sleep(8) #The robot is tired,and calculates ;)
80.
81.  left_motor.run_timed(time_sp=6500,speed_sp=-100,stop_action="brake") #Moving the robot to some point backwards *Both motors
82.  right_motor.run_timed(time_sp=6500,speed_sp=-100,stop_action="brake") #Moving the robot to some point backwards *Both motors
83.
84.  time.sleep(5)
85.
86.  numofAtts-=1
87. raise SystemExit
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top