Demo entry 6355132

robot sumo program

   

Submitted by anonymous on Apr 13, 2017 at 05:34
Language: Python 3. Code size: 2.7 kB.

#!/usr/bin/python3

# -----------------------------------------------------------------------------

# DESCRIPTION OF CODE

"""
This program aims to allow our robot to stay stationary for 3 secs before rotating,
on the spot and locating the opposing robot. Once the bot is detected our robot 
should head towards it and try to push it out of the ring.
"""

# Coded by: Zilu Dong, Derek Daquel, Timothy Ryan

# -----------------------------------------------------------------------------

# CONNECTION OF MOTORS/SENSORS

from time import sleep
import sys, os

from ev3dev.ev3 import *

#Connects motors
rightMotor = LargeMotor(OUTPUT_A)
leftMotor  = LargeMotor(OUTPUT_D)

#Connects touch sensors.
ts1 = TouchSensor(INPUT_1); assert ts1.connected
ts4 = TouchSensor(INPUT_4); assert ts4.connected
us  = UltrasonicSensor();   assert us.connected

# -----------------------------------------------------------------------------
# DEFINITION OF FUNCTIONS FOR SUMO BOT
def stop():
    # Stop both motors
    leftMotor.stop(stop_action='brake')  
    rightMotor.stop(stop_action='brake')

def charge(left,right):

    #Starts both motors
    
    rightMotor.run_direct(duty_cycle_sp = right)
    leftMotor.run_direct(duty_cycle_sp = left)

def start(left,right):

    # We want to turn the robot wheels in opposite directions
    
   
    """
    Start both motors at the given speeds.
    """
    leftMotor.run_direct(duty_cycle_sp=left)
    rightMotor.run_direct(duty_cycle_sp=right)
    
def defense(left,right):

    #rightMotor.run_direct(speed_sp=dir*-750)
    #leftMotor.run_direct(speed_sp=dir*750)
    leftMotor.run_direct(duty_cycle_sp=left)
    rightMotor.run_direct(duty_cycle_sp=right)
    
# -----------------------------------------------------------------------------

# MAIN PROGRAM

# Determines if user has pressed a button
btn = Button()

# Robotg must stay stationary for 3 secs before attempting to fight
sleep(3)

# Runs the robot until a button is pressed.
while not btn.any():
    #LEFT=-1
    #RIGHT=1
   
    # Ultrasonic sensor will measure distance to the closest object in front of it.
    distance = us.value();

    if distance > 1000:
        # Path is clear, rotate to detect the opponent.
        print("turningleft")
        start(100,-100)
        print("has turned")

    else:
        # opponent detected, run forward at full speed which is 100
        charge(-100,-100)

    # If robot is touched on the sides, a dfensive function is called
 
    if ts1.value():
        defense(-100,100)
    elif ts4.value():
        defense(100,-100)
# Stop the motors before exiting.
rightMotor.stop()
leftMotor.stop()

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).