class MissionAll( StandardMission ):
	
	def __init__( self , name ):
		self.name = name 

		StandardMission.__init__( self , self.name , "/mission/all" , self.callback )

		self.vision_gate = VisionCollector( "gate" )
		self.vision_flare = VisionCollector( "flare" )
		self.vision_drum = VisionCollector( "drum" )

		self.state = False

		self.echo( self.name , "ALL MISSION SETUP FINISHED")
		
		self.time_start = time.time()

	def callback( self , request ):
		
		result = False

		self.state = request.data 
		if( not self.ok_state() ):
			return False

		self.echo( self.name , "START MISSION GATE" )

		self.reset_target( "yaw" )
		self.reset_target( "xy" )
		self.reset_velocity( "xy" )

		self.target_state()
		self.start_yaw = self.temp_state[5]

		self.fix_z( -0.8 )
		self.wait_state( "z" , 0.15 , 5 )

		self.find_gate( 4 , 30 , 2 , 15 , 1 ) # d_f , t_f , d_s , t_s

		self.echo( "CONNECTION" , "Now can go pass gate" )
		self.over_move( 10 , 70 , True )
		self.fix_z( -1.2 )
		self.velocity_xy( 0.2 , 0 )
		while( self.ok_state() ):
			self.sleep( 0.1 )
			if( self.over_move( 10 , 70 ) ):
				break
		self.reset_velocity( "xy")
		self.fix_z( -0.7 )

		play_pick = False
		if( self.ok_state() ): play_pick = self.find_drum() 
		if( self.ok_state() and play_pick ): self.move_out_in()

		self.find_flare( 6 , 50 , -1 )		

		self.reset_velocity( "xy" )
		self.hold_gripper()
		self.active_control( False )
		return True

	def over_move( self , distance , time , setup = False ):
		if( setup ):
			self.time_start = time.time()
			self.collect_state()
			return True
		else:
			if( self.distance() > distance ):
				return True
#			elif( ( time.time() - self.time_start ) > time ):
#				return True
			else:
				return False

	def play_flare( self ):
		self.hold_gripper()
		self.free_xy( True )
		self.fix_z( -1.6 )
		count_unfound = 0
		while( self.ok_state() ):
			self.sleep( 0.1 )
			self.vision_flare( "flare" , "near" , 5 )
			if( not self.vision_flare.have_object() ):
				count_unfound += 1
				if( count_unfound == 5 ):
					break
				continue
			count_unfound = 0
			if( abs( self.vision_flare.center_x() ) < 0.1 ):
				self.velocity( { 'x' : 0.2 } )
			else:
				self.velocity( { 'y' : math.copysign( 0.25 , self.vision_flare.center_x()*-1 ) })

		self.fix_z( -1.3 )	
		self.echo("NEAR" , "wait depth" )
		while( self.ok_state() ){
			self.sleep( 0.1 )
			self.velocity({ 'x':0.01 } )
			if( self.check_position( "z" , 0.15 ) ): break
		}
		self.free_xy( False )
		time_start = time.time()
		diff_time = 0
		self.velocity_xy( 0.2 )
		while( self.ok_state() and diff_time < 6 ):
			self.sleep( 0.1 )
			diff_time = time.time() - time_start
		return True


	def find_flare( self , distance_survey , time_survey , direction ):
		mode = 1 # 1 forward 2 survey
		setup_mode = True
		direction_survey = direction
		current_fix_velocity = False
		finish_flare = False
		self.fix_z( -1.4 )
		while( self.ok_state() ):
			self.sleep( 0.1 )
			self.vision_flare( "flare" , "near" , 5 )
			if( self.vision_flare.have_object() ):
				finish_flare = self.play_flare()
				if( finish_flare ):
					self.reset_velocity( "xy" )
					break
			self.vision_flare( "flare" , "far" , 5 )
			if( self.vision_flare.have_object() ):
				if( current_fix_velocity ):
					self.reset_velocity( "xy" )
					current_fix_velocity = False
				setup_mode = True
				if( abs( self.vision_flare.center_x() ) < 0.15 ):
					self.velocity( { 'x' : 0.1 } ) 
				else: self.velocity( {'y' : math.copysign( 0.12, self.vision_flare.center_x())} )
				continue
			if( setup_mode ):
				if( mode == 1 ):
					self.over_move( 1 , 15 , True )
				else:
					self.over_move( distance_survey ,time_survey , True )
				setup_mode = False
			if( not current_fix_velocity ):
				if( mode == 1 ):
					self.velocity_xy( 0.2 , 0 )
				else:
					self.velocity_xy( 0 , 0.2 * direction_survey )
				current_fix_velocity = True
			if( mode == 1 ):
				if( self.over_move( 1 , 15 ) ):
					self.echo( "FLARE" , "Change to mode survey" )
					mode = 2
					self.reset_velocity("xy")
					current_fix_velocity = False
					setup_mode = True
			else:
				if( self.over_move( distance_survey , time_survey ) ):
					self.echo( "FLARE" , "Change to mode forward" )
					distance_survey *= -1
					self.reset_velocity("xy")
					current_fix_velocity = False
					setup_mode = True
					mode = 1
			

	def move_out_in( self ):
		self.velocity_xy( -0.15 , 0 )
		self.find_drum
		count_unfound = 0
		while( self.ok_state() ):
			self.sleep( 0.06 )
			self.vision_drum.analysis_all( "mat" , "sevinar" , 5 )
			if( not self.vision_drum.have_object() ):
				count_unfound += 1
				if( count_unfound == 5 ):
					break
			else:
				count_unfound = 0
		self.reset_velocity( "xy" )
		return self.find_drum( True )

	def play_drum( self , drop_ball ):
		self.fire_gripper()
		self.echo( "DROP" , "Command to release gripper")
		self.free_xy( True )
		value_x = 0
		value_y = 0
		ok_y = True
		ok_x = False
		ever_fire_golf = False
		wait_z = False
		self.relative_z( -0.3 )
		start_time = 0
		count_unfound = 0
		if( drop_ball ): self.hold_golf()
		while( self.ok_state() ):
			self.sleep( 0.06 )
			self.vision_drum.analysis_all( "blue" , "left-right" , 5 )
			if( not self.vision_drum.have_object() ):
				count_unfound += 1
				if( count_unfound == 5 ):
					self.free_xy( False )
					return False
				continue
			count_unfound = 0
			self.vision_drum.result['cx_2'] -= 0.15
			if( abs(self.vision_drum.result['cx_2']) < 0.1 ): 
				value_y = -0.05
				ok_y = True
			else: 
				value_y = math.copysign( 0.25 , self.vision_drum.result['cx_2'] * -1 )
				ok_y = False
			if( abs(self.vision_drum.result['cy_2']) < 0.1 ):
				value_x = 0.02
				ok_x = True
			else: 
				value_x = math.copysign( 0.2 , self.vision_drum.result['cy_2'] )
				ok_x = False
			if( ok_y and ok_x and self.check_position( "z" , 0.15 ) 
					and self.check_position("yaw" , 0.2 ) and ( not ever_fire_golf ) ):
				self.target_state()
				self.echo( "DRUM" , "Now depth is " + str( self.temp_state[2] ) )
				if( self.temp_state[2] < -1.4 ):
					if( not drop_ball ): self.fire_golf()
					else: self.velocity_z( -0.15 )
					ever_fire_golf = True
					start_time = time.time()
				else:
					self.relative_z( -0.3 )
			self.velocity( { 'x' : value_x , 'y' : value_y } )
			if( wait_z ):
				if(self.check_position( "z" , 0.15  ) ):
					self.free_xy( False )
					return True
				else: continue
			if( ever_fire_golf ):
				if( ( time.time() - start_time ) > 8 ):
					self.reset_velocity( "z" )
					self.fix_z( -0.6 )
					wait_z = True

	def find_drum( self , direction , drop_ball = False ):
		self.velocity_xy( 0.2 )
		count_found = 0 
		while( self.ok_state() ):
			self.sleep( 0.06 )
			self.vision_drum.analysis_all( "mat" , "sevinar" , 5 )
			if( self.vision_drum.have_object() ):
				count_found += 1
				if( count_found == 3 ):
					break
			else:
				count_found = 0
		self.reset_velocity( "xy" )
		self.reset_target( "xy" )

		value_x = 0
		value_y = 0
		ever_change = False
		count_found = 0
		while( self.ok_state() ):
			self.sleep( 0.06 )
			self.vision_drum.analysis_all( "mat" , "sevinar" , 5 )
			if( not self.vision_drum.result['forward'] ): value_x = 0.1
			elif( not self.vision_drum.result['backward'] ): value_x = -0.1
			else: value_x = 0
			
			if( direction == 1 ): # direction == 1 is left
				if( not self.vision_drum.result['left'] and not ever_change):
					direction *= -1
					ever_change = True
				else:
					return False
			else:
				if( not self.vision_drum.result['right'] and not ever_change):
					direction *= -1
					ever_change = True
				else:
					return False
		
			self.vision_drum.analysis_all( "blue" , "center" , 5 )
			if( self.vision_drum.have_object() ):
				count_found += 1
				if( count_found == 3 ):
					return self.play_drum( drop_ball )
			else:
				count_found = 0
				self.velocity( { 'x' : value_x , 'y' : 0.1 * direction } )

	def find_gate( self, distance_forward, time_forward, distance_survey, time_survey, direct):
		self.velocity_xy( 0.2 )
		self.over_move( distance_forward , time_forward , True )
		finish_gate = False
		count_found = 0 
		while( self.ok_state() and not( finish_gate )):
			self.sleep( 0.06 )
			self.vision_gate.analysis_all( "gate" , "sevinar" , 5 )
			if( self.vision_gate.have_object() ):
				if( self.vision_gate.num_object() == 2 ):
					self.reset_velocity( "xy" )
					finish_gate = self.play_gate( direct )
				else:
					count_found += 1 
					if( count_found == 5 ):
						self.reset_velocity( "xy" )
						finish_gate = self.play_gate( direct )
			else:
				count_found = 0
			if( self.over_move( distance_forward , time_forward ) ):
				break
		self.reset_velocity( "xy" )
		self.wait_state( "xy" , 0.15 , 5 )
		count_found = 0
		self.over_move( distance_survey , time_survey , True )
		if( direct == 0 ):
			return True
		while( self.ok_state() and not( finish_gate ) ):
			self.sleep( 0.06 )
			self.vision_gate.analysis_all( "gate" , "sevinar" , 5 )
			if( self.vision_gate.have_object() ):
				if( self.vision_gate.num_object() == 2 ):
					self.reset_velocity( "xy" )
					finish_gate = self.play_gate( direct )
				else:
					count_found += 1 
					if( count_found == 5 ):
						self.reset_velocity( "xy" )
						finish_gate = self.play_gate( direct )
			else:
				count_found = 0
			if( self.over_move( distance_survey , distance_forward ) ):
				break 
		return finish_gate

	def play_gate( self , direction ): # direction = 1 is right an -1 is left
		self.vision_gate.analysis_all( "gate" , "sevinar" , 5 )
		current_fix_velocity = False
		count_finish = 0
		if( self.vision_gate.num_object() == 2 ):
			while( self.ok_state() ):
				self.sleep( 0.06 )
				self.vision_gate.analysis_all( "gate" , "sevinar" , 5 )
				if( self.vision_gate.num_object() == 2 ):
					count_finish = 0
					if( abs( self.vision_gate.center_x() ) < 0.2 ): 
						if( not current_fix_velocity ):
							self.velocity_xy( 0.1 , 0 )
							current_fix_velocity = True
					else:
						if( current_fix_velocity ):
							self.reset_velocity( "xy" )
							current_fix_velocity = False
					self.echo_vision( self.vision_gate.echo_special() )
					if( self.vision_gate.distance_x() > 1 ):
						if( current_fix_velocity ):
							self.reset_velocity( "xy" )
						return True
				else:
					count_finish += 1 
					if( count_finish == 3 ):
						if( current_fix_velocity ):
							self.reset_velocity( "xy" )
						return True
		else:
			while( self.ok_state() ):
				self.sleep( 0.06 )
				self.vision_gate.analysis_all( "gate" , "sevinar" , 5 )
				if( self.vision_gate.num_object() == 2 ):
					return self.play_gate()
				elif( self.vision_gate.num_object() == 1 ):
					count_finish = 0
					self.velocity( { 'y' : direction * 0.1 } )
				else:
					count_finish += 1
					if( count_finish == 4 ):
						return True
Exemple #2
0
class MissionPick(StandardMission):
    def __init__(self, name):
        self.name = name

        StandardMission.__init__(self, self.name, "/mission/pick",
                                 self.callback)

        self.vision = VisionCollector("drum")

        self.echo(self.name, "PICK GOLF SETUP FINISHED")

        self.state = False

    def callback(self, request):

        result = False

        self.state = request.data
        if (not self.ok_state()):
            return False

        self.fix_z(-0.5)
        self.reset_target("xy")
        self.wait_state("z", 0.1, 5)

        found_object = self.step_01()

        if (not found_object):
            self.echo(self.name, "Don't found object mission aborted")
            return False
        else:
            self.step_03()
            result = True

        return result

    def step_01(self):  # find golf in a drum
        found_golf = False
        mode_control = 1  # mode control is survey around drum
        # 1 is left  2 is right 3 is up 4 is down
        value_x = 0
        value_y = 0

        count_change_mode = 0
        count_found_golf = 0
        limit_mode = 10
        self.echo(self.name, "We command to hold for pick golf")
        self.hold_golf()
        self.echo(self.name, "We command finish")
        while (self.ok_state()):
            self.sleep(0.1)
            if (mode_control == 1):
                self.vision.analysis_all("blue", "left-right", 5)
                self.echo_vision(self.vision.echo_special())
                if (self.vision.result["cx_1"] < -0.5):
                    value_y = 0.1
                elif (self.vision.result["cx_1"] > 0.0):
                    value_y = -0.1
                else:
                    value_y = 0
                if (abs(self.vision.result["cy_1"]) < 0.5):
                    value_x = 0
                else:
                    value_x = math.copysign(0.05, self.vision.result["cy_1"])
            elif (mode_control == 2):
                self.vision.analysis_all("blue", "left-right", 5)
                self.echo_vision(self.vision.echo_special())
                if (self.vision.result["cx_2"] > 0.5):
                    value_y = -0.05
                elif (self.vision.result["cx_2"] < 0.0):
                    value_y = 0.05
                else:
                    value_y = 0
                if (abs(self.vision.result["cy_2"]) < 0.5):
                    value_x = 0
                else:
                    value_x = math.copysign(0.05, self.vision.result["cy_2"])
            self.echo(
                self.name + " Mode " + str(mode_control),
                "We command velocity x : y --> " + str(value_x) + " : " +
                str(value_y))
            self.velocity({'x': value_x, 'y': value_y})
            self.sleep(0.1)
            self.vision.analysis_all("golf", "sevinar", 5)
            self.echo_vision(self.vision.echo_data())
            # This part will discus about data from vision
            if (self.vision.have_object()):
                count_found_golf = 0
                self.echo(self.name, "Find golf move to center of golf")
                while (self.ok_state() and abs(count_found_golf) < 15):
                    self.sleep(0.1)
                    self.vision.analysis_all("golf", "sevinar", 5)
                    if (self.vision.have_object()):
                        count_found_golf += 1
                        self.velocity({
                            'x':
                            math.copysign(0.1, self.vision.center_y()),
                            'y':
                            math.copysign(0.1, -1 * self.vision.center_x())
                        })
                    else:
                        count_found_golf -= 0
                    self.echo(self.name,
                              "Found golf is  " + str(count_found_golf))
                if (count_found_golf == 15):
                    return True

            if (value_x == 0 and value_y == 0):
                count_change_mode += 1
                self.echo(self.name,
                          "count_change_mode " + str(count_change_mode))
                if (count_change_mode == limit_mode):
                    mode_control += 1
                    count_change_mode = 0
                    if (mode_control > 2):
                        return False
        print("Finish loop")

    def step_02(self):  # this part we will pick a ball
        self.echo(self.name, "We will start to pick ball")
        self.fix_z(-0.8)
        count_ok_depth = 0
        value_x = 0
        value_y = 0
        self.free_xy(True)
        while (self.ok_state()):
            self.vision.analysis_all("golf", "sevinar", 5)
            self.echo_vision(self.vision.echo_data())
            if (self.vision.center_x() < -0.5): value_y = -0.1
            elif (self.vision.center_x() > -0.1): value_y = 0.1
            else: value_y = 0
            if (self.vision.center_y() < 0.1): value_x = 0.1
            elif (self.vision.center_y() > 0.5): value_x = -0.1
            else: value_x = 0
            self.echo(self.name,
                      " vel x : y is" + str(value_x) + " : " + str(value_y))
            self.velocity({'x': value_x, 'y': value_y})

            if (self.check_position("z", 0.15)):
                count_ok_depth += 1
            else:
                count_ok_depth = 0
            if (value_x == 0 and value_y == 0):
                self.echo(self.name, "Target on lock")
                if (count_ok_depth > 5):
                    self.echo(self.name, "We will move down now ")
                    start_time = time.time()
                    diff_time = 0
                    self.velocity_z(-0.1)
                    while (self.ok_state() and diff_time < 10):
                        self.sleep(0.1)
                        self.reset_target("xy")
                        diff_time = time.time() - start_time
                        self.echo(self.name, "Now time is " + diff_time)
                    self.reset_velocity("z")
                    self.fix_z(-0.5)
                    self.free_xy(False)
                    break
        self.echo(self.name, "Finish pick golf it ok?")

    def step_03(self):
        self.echo(self.name, "We will start to pick ball")
        current_fix_velocity = False
        value_x = 0
        value_y = 0
        self.fix_z(-0.8)
        self.wait_state("z", 0.1, 5)
        self.free_xy(True)
        while (self.ok_state()):
            self.sleep(0.05)
            self.vision.analysis_all("golf", "sevinar", 5)
            self.echo_vision(self.vision.echo_data())
            if (self.vision.have_object()):
                if (self.vision.center_x() > -0.4): value_y = -0.2
                else: value_y = 0.1
                if (self.vision.center_y() < 0.3): value_x = -0.15
                else: value_x = 0.10
                self.echo(
                    self.name,
                    "Vel x : y are " + str(value_x) + " : " + str(value_y))
                self.velocity({'x': value_x, 'y': value_y})
            else:
                if (not current_fix_velocity):
                    self.velocity_z(-0.03)
                    current_fix_velocity = True
                value_y = 0.0
                value_x = 0.0
                self.velocity({'y': value_y, 'x': value_x})
                self.echo(
                    self.name,
                    "Vel x : y are " + str(value_x) + " : " + str(value_y))
                self.target_state()
Exemple #3
0
class MissionDrum(StandardMission):
    def __init__(self, name):
        self.name = name

        StandardMission.__init__(self, self.name, "/mission/drop",
                                 self.callback)

        self.vision = VisionCollector("drum")

        self.state = False

        self.echo(self.name, "DROP BALL SEtuP FINISHER")

    def callback(self, request):

        result = False

        self.state = request.data
        if (not self.ok_state()):
            return False

        self.echo(self.name, "START MISSION DROP BALL")

        self.wait_state("yaw", 0.1, 5)

        self.step_01()

        return True

    def step_01(self):
        count_Time = False
        self.echo(self.name, "START AT STEP 01")
        value_x = 0
        value_y = 0
        start_time = 0
        ever_drop = False
        self.velocity_z(-0.03)
        self.wait_state("z", 0.1, 5)
        current_fix_z = False
        while (self.ok_state()):
            self.vision.analysis_all("blue", "left-right", 5)
            self.echo_vision(self.vision.echo_special())
            self.echo_vision(self.vision.echo_data())
            if (abs(self.vision.center_x()) < 0.15):
                value_y = 0
            else:
                value_y = math.copysign(0.1, -1 * self.vision.center_x())
            if (abs(self.vision.center_y()) < 0.15):
                value_x = 0
            else:
                value_x = math.copysign(0.1, self.vision.center_y())


#			if( not self.vision.result["forward"] ):
#				value_x = -0.2
#			elif( not self.vision.result["backward"] ):
#				value_x = 0.2
#			else:
#				value_x = 0
            self.echo(
                self.name, "I command velocity x : y are " + str(value_x) +
                " : " + str(value_y))
            if (not count_Time):
                self.target_state()
                self.echo(self.name,
                          "Node depth is " + str(self.temp_state[2]))
                if (self.temp_state[2] < -1.3):
                    count_Time = True
                    start_time = time.time()
                    self.free_xy(True)
            else:
                diff_time = time.time() - start_time
                self.echo(self.name, "Count time for drop " + str(diff_time))
                if (diff_time > 5):
                    break
                elif (diff_time > 2 and not ever_drop):
                    self.echo(self.name, "We have to command drop ball")
                    self.fire_golf()
                else:
                    continue
            self.velocity({'x': value_x, 'y': value_y})

        self.reset_velocity("z")
        self.fix_z(-0.5)
        self.wait_state("z", 0.1, 5)
        self.free_xy(False)
        self.reset_target("xy")