go away spammer

Author Topic: Where is the problem?  (Read 1792 times)

0 Members and 1 Guest are viewing this topic.

Offline edwardkok30Topic starter

  • Beginner
  • *
  • Posts: 1
  • Helpful? 0
Where is the problem?
« on: October 08, 2012, 04:58:31 AM »
The code below shows a program that tells a robot to move to a ball and kick it. This is motion will repeat in a loop which holds three important methods turn_to(...), move_to(...) and kickBall(). The values are passed in to the three methods are based on data which are received through a server which is connected to a camera which shows live detection of the robot and the ball in the method update_coordinates. The get_ballxy() and get_robotxya() returns the coordinates and angle of the robot and the ball. The question here is, there is no problem if i run this code without the loop, however if i want the motion to continue in a loop there seems to be a problem in passing down the values to the robot.It works fine for the first time in the loop where it successfully turn to the ball, move to it and kick it, however for the second time, it doesn't and until a few more turn it does it correctly again. Is there problem with the loop or the activate method? or did i pass the values too quickly or wrongly? because it take sometime for the ball to stop moving. I tried sleeping it for sometime, but that doesn't help much. Any opinion on how to solve this?

import socket
import os,sys
import time
from threading import Thread

HOST = '59.191.193.67'
PORT = 5555

coordinates = []

def connect():   
    globals()['client_socket'] = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client_socket.connect((HOST,PORT))

def update_coordinates():
    screen_width = 0
    screen_height = 0
    global last_coordinates
    last_coordinates = [0 for _ in range(19)]
    while True:
        try:
            client_socket.send("loc\n")
            data = client_socket.recv(8192)
        except:
            connect();
            continue;

        globals()['coordinates'] = data.split()
        if(not(coordinates[-1] == "eom" and coordinates[0] == "start")):
            continue

        if (screen_width != int(coordinates[2])):
        screen_width = int(coordinates[2])
                screen_height = int(coordinates[3])
        return

def get_ballxy():
    update_coordinates()
    ballx = int(coordinates[8])
    bally = int(coordinates[9])
    return ballx,bally

def get_robotxya():
    update_coordinates()
    robotx = int(coordinates[16])
    roboty = int(coordinates[17])
    angle = int(coordinates[18])
    return robotx,roboty,angle

def activate():

    if(ultrasonic.get_sample() < 10):
        both.turn(power=-70, tacho_units=400, brake=False)
        time.sleep(1)
        bx,by = get_ballxy()
        rx,ry,a = get_robotxya()
        turn_to(brick,bx,by,rx,ry,a)
        time.sleep(0.5)
        move_to(brick,bx,by,rx,ry)
        kickBall(brick,by)

    else:
        time.sleep(1)
        bx,by = get_ballxy()
        rx,ry,a = get_robotxya()
        turn_to(brick,bx,by,rx,ry,a)
        time.sleep(0.5)
        move_to(brick,bx,by,rx,ry)
        kickBall(brick,by,ry)

Thread(target=update_coordinates).start()
connect()
update_coordinates()
while True:
    activate()
    time.sleep(2)

NOTE: the coding part for move_to,turn_to and kickBall has been remove due to the length of the code
Using nxt-python

 


Get Your Ad Here