Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- from ev3dev.ev3 import * #Ev3 crucial lib
- from time import sleep #Importing the sleep only from the library time
- import math #Import math lib for math functions
- gyro_sensor = GyroSensor() #Defining Gyro Sensor
- left_motor = LargeMotor("outB") #Defining the LargeMotor which is connected to the port B as left motor
- right_motor = LargeMotor("outC") #Defining the LargeMotor which is connected to the port C as right motor
- assert gyro_sensor.connected #Ensuring that
- assert left_motor.connected #everything is
- assert right_motor.connected #connected!
- gyro_sensor.mode = 'GYRO-ANG' #Setting the mode to gyro sense to measure angles
- numofAtts=int(input("Input the number of graphs you want to draw:"))
- while(numofAtts>0):
- k=float(input("Input k from y=k*x+n:")) #Input value for k
- print("\n") #New line
- n=float(input("Input n from y=k*x+n:")) #Input value for n
- if(n > 0): #If N is larger than 0
- left_motor.run_timed(time_sp=1000*n,speed_sp=100)
- right_motor.run_timed(time_sp=1000*n,speed_sp=100)
- if (n < 0): #If N is less than 0
- left_motor.run_timed(time_sp=1000*abs(n),speed_sp=-100)
- right_motor.run_timed(time_sp=1000*abs(n),speed_sp=-100)
- time.sleep(3)
- gyro_sensor.mode = 'GYRO-RATE'
- gyro_sensor.mode = 'GYRO-ANG'
- gcal=abs(gyro_sensor.value()) #Storing value from gyro sensor (-33,33)
- time.sleep(2) #Rest a bit with functioning
- angle=0 #At the start the angle that robot and y-axis take should be 0
- angleneeded=abs(math.degrees(math.atan(k))) #Computation of the angle we need to achieve (45)
- print(angleneeded)
- angle=gcal #33
- if k<0: #If k is less than 0 than the angle is > 90
- while abs(angle-gcal)<90-angleneeded: #While we don't achieve the needed complementary angle
- left_motor.run_forever(speed_sp=-10) #Move the robot to the
- right_motor.run_forever(speed_sp=10) #Right
- angle=abs(gyro_sensor.value()) #Constantly update the angle
- print(angle)
- print(gcal)
- elif k > 0: #If k is large than 0 than the angle is < 90
- gcal=abs(gyro_sensor.value())
- angle=abs(gyro_sensor.value())
- #if gyro_sensor.value()<0:
- # angleneeded+=abs(gyro_sensor.value())
- while angle-gcal<abs(90-angleneeded): #While we don't achieve the needed complementary angle
- left_motor.run_forever(speed_sp=10) #Move the robot to the
- right_motor.run_forever(speed_sp=-10) #Left
- angle=abs(gyro_sensor.value()) #While we don't achieve the needed complementary angle
- print(angle)
- print(gcal)
- elif k == 0: #If k is equal to 0 than the angle is 0 with x axis
- gcal=abs(gyro_sensor.value())
- angle=abs(gyro_sensor.value())
- #if gyro_sensor.value()<0:
- # angleneeded+=abs(gyro_sensor.value())
- while angle-gcal<abs(90-angleneeded): #While we don't achieve the needed complementary angle
- left_motor.run_forever(speed_sp=10) #Move the robot to the
- right_motor.run_forever(speed_sp=-10) #Left
- angle=abs(gyro_sensor.value()) #While we don't achieve the needed complementary angle
- print(angle)
- print(gcal)
- #Assuring that we didn't made motors to move @ all
- left_motor.stop() #When we get out from the loop
- right_motor.stop()# we make the robot to stop rotating since it achieved the desired angle
- left_motor.run_timed(time_sp=3250,speed_sp=100,stop_action="brake") #Moving the robot to some point backwards *Both motors
- right_motor.run_timed(time_sp=3250,speed_sp=100,stop_action="brake") #Moving the robot to some point backwards *Both motors
- time.sleep(8) #The robot is tired,and calculates ;)
- left_motor.run_timed(time_sp=6500,speed_sp=-100,stop_action="brake") #Moving the robot to some point backwards *Both motors
- right_motor.run_timed(time_sp=6500,speed_sp=-100,stop_action="brake") #Moving the robot to some point backwards *Both motors
- time.sleep(5)
- numofAtts-=1
- raise SystemExit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement