[URGENT] Python - Arduino problem

This is the place for queries that don't fit in any of the other categories.

[URGENT] Python - Arduino problem

Postby Rusoski » Mon Apr 07, 2014 3:36 am

Hardware Specifications:

CPU: Intel Core i7-3630QM 2.4GHz
Graphics Family: Intel HD Graphics 4000
GPU: Nvidia GT 650M 2GB
RAM: 4GB
HDD: 750MB / 7200 RpM
Arduino: Arduino Due (Programming Port)

Software Specifications:

OS: Windows 8.1 x64
Python: Python 2.7.6 Amd x64
PyGame: PyGame 1.9.2a0 Amd x64 for Python 2.7
PySerial: PySerial 2.7 Amd x64 for Python 2.7
Arduino: Arduino 1.5.6-r2 BETA

Program description:

This program should follow a specific color. There are three colors, it will randomly choose which color to follow, only 1 at a time, then indicate in which side of the screen it is displayed (middle, left or right), after that it should send a 'M' to arduino telling that the collor is in the middle at the screen, 'L' telling the arduino taht the color is at the left part of the screen, or 'R' if the color is at right part of the screen.

Problem:

It does that, in fact it activates the engine of the robot to search for the color, but after some seconds the camera get frozen. If you could help me plase, I will be very gratefull, this has to be done and be workind before this Tuesday 08/04/2014.In python I will only show you only 1 of the 3 colors.

Thank you! :)

--------------------------------------------------
PYTHON CODE
--------------------------------------------------

Code: Select all
#!/usr/bin/env python
import pygame
import pygame.camera
import serial
import msvcrt
import time
from time import sleep
from random import randint

serialport = serial.Serial(6, 9600)
serialport.setDTR (False)
sleep (0.022)
serialport.setDTR (True)

pygame.init ()
pygame.camera.init ()
Pantalla = pygame.display.set_mode ((640, 480), 0)
ListaCamaras = pygame.camera.list_cameras ()
camara = pygame.camera.Camera (ListaCamaras [0], (640, 480))
camara.start ()
imagen = camara.get_image ()
imagen2 = camara.get_image ()
imagen3 = camara.get_image ()
imagen4 = camara.get_image ()
ccolor = pygame.transform.average_color(imagen, (160, 120, 50, 50))
ccolor2 = pygame.transform.average_color(imagen2, (160, 120, 50, 50))
ccolor3 = pygame.transform.average_color(imagen3, (160, 120, 50, 50))
ccolor4 = pygame.transform.average_color(imagen4, (160, 120, 50, 50))
colorChoose = randint (1, 3)
width = 0;

while 1:
    serialMessage = serialport.readline()
    if serialMessage == 'Z':
        colorChoose = randit (1, 3)
    pygame.event.get ()
    mouse= pygame.mouse.get_pressed ()
    keys = pygame.key.get_pressed ()

    if mouse [1] == 1:
        x2, y2 = pygame.mouse.get_pos ()
        ccolor2 = pygame.transform.average_color (imagen2, (x2, y2, 50, 50))
    imagen2 = camara.get_image ()
    Pantalla.blit (imagen2, (0, 0))
    mask2 = pygame.mask.from_threshold (imagen2, ccolor2, (10, 10, 30))
    connected = mask2.connected_component()
    if (mask2.count() > 50 & colorChoose == 2):
        serialport.write ('B')
        coord2 = mask2.centroid ()
        pygame.draw.circle (Pantalla, (0,255,0), coord2, 10,5)
        pygame.display.flip ()
        if (coord2 > (0, 320)) & (coord2 <= (213, 480)): # A - Green Middle Top
            width = 1
        if (coord2 > (0, 160)) & (coord2 <= (213, 320)): # B - Green Middle Middle
            width = 1
        if (coord2 > (0, 0)) & (coord2 <= (213, 160)): # C - Green Middle Bottom
            width = 1
        if (coord2 > (213, 320)) & (coord2 <= (420, 480)): # D - Green left Top
            width = 2
        if (coord2 > (213, 160)) & (coord2 <= (420, 320)): # E - Green Left Middle
            width = 2
        if (coord2 > (213, 0)) & (coord2 <= (420, 160)): # F - Green Left Bottom
            width = 2   
        if (coord2 > (420, 320)) & (coord2 <= (640, 480)): # G - Green Right Top
            width = 3
        if (coord2 > (420, 160)) & (coord2 <= (640, 320)): # H - Green Right Middle
            width = 3
        if (coord2 > (420, 0)) & (coord2 <= (640, 160)): # I - Green Right Bottom
            width = 3
    pygame.display.update()

    if width == 1:
        serialport.write ('R')
        time.sleep (0.2)

    if width == 2:
        serialport.write ('M')
        time.sleep (0.2)

    if width == 3:
        serialport.write ('L')
        time.sleep (0.2)

    if msvcrt.kbhit():
        keypress = ord(msvcrt.getch())
        if keypress == 113:
            print ("Stopping the program...")
            break
print ("Program ended successfully!")


--------------------------------------------------
ARDUINO CODE
--------------------------------------------------

Code: Select all
// Ultrasonic sensors
const int trigPina = 7;
const int echoPina = 8;

const int trigPinb = 9;
const int echoPinb = 10;

const int trigPinc = 11;
const int echoPinc = 12;

float durationa, distancea;
float durationb, distanceb;
float durationc, distancec;

// Engine
int leftEngineForward = 32;
int leftEngineBackwards = 33;
int rightEngineForward = 28;
int rightEngineBackwards = 29;

// Garbage variable
boolean garbage = false;

// Screen quadrants
char garbageQuadrant = 'NA';
char containerQuadrant = 'NA';

// LEDs
int led = 20;
int pyLedLeft = 4;
int pyLedRight = 5;

//----------------------------------------------------------| Set up | START
void setup () {
  UltrasonicSetup ();
  EngineSetup ();
  LED ();
  Serial.begin (9600);
  Serial.print ("\n\n\nThis bad ass machine is ready to rock!\n\n\n");
  delay (1000);
}
//----------------------------------------------------------| Set up | END

//----------------------------------------------------------| Main loop | START
void loop () {
  Serial.print ("\nChoosing state...\n");
  delay (50);
  if (garbage == false) {
    SearchForGarbage ();
  }
}
//----------------------------------------------------------| Main loop | END

//----------------------------------------------------------| Search for garbage | START
void SearchForGarbage () {
  garbageQuadrant = Serial.read();
  if (garbageQuadrant == 'M') {
    digitalWrite (leftEngineBackwards, LOW);
    digitalWrite (leftEngineForward, HIGH);
    digitalWrite (rightEngineBackwards, LOW);
    digitalWrite (rightEngineForward, HIGH);
    digitalWrite (pyLedLeft, HIGH);
    digitalWrite (pyLedRight, HIGH);
    ultrasonicGarbageSensor ();
    if (distancea > 0 && distancea <= 5) {
      digitalWrite (led, HIGH);
      delay (100);
      digitalWrite (led, LOW);
    }
  }
  if (garbageQuadrant == 'L') {
    digitalWrite (leftEngineBackwards, HIGH);
    digitalWrite (leftEngineForward, LOW);
    digitalWrite (rightEngineBackwards, LOW);
    digitalWrite (rightEngineForward, HIGH);
    digitalWrite (pyLedLeft, HIGH);
    digitalWrite (pyLedRight, LOW);
  }
  if (garbageQuadrant == 'R') {
    digitalWrite (leftEngineBackwards, LOW);
    digitalWrite (leftEngineForward, HIGH);
    digitalWrite (rightEngineBackwards, HIGH);
    digitalWrite (rightEngineForward, LOW);
    digitalWrite (pyLedLeft, LOW);
    digitalWrite (pyLedRight, HIGH);
  }
}
//----------------------------------------------------------| Search for garbage | END

//----------------------------------------------------------| Ultrasonic garbage sensor | START
void ultrasonicGarbageSensor () {
  digitalWrite (trigPina, HIGH);
  delay (10);
  digitalWrite (trigPina, LOW);
  durationa = pulseIn (echoPina, HIGH);
 
  distancea = (durationa/2) / 29.1;
 
  Serial.print ("Collector sensor = ");
  Serial.print (distancea);
  Serial.print (" cm      \n");
}
//----------------------------------------------------------| Ultrasonic garbage sensor | END

//----------------------------------------------------------| Ultrasonic container sensor | START
void ulctrasonicContainerSensor () {
 
  digitalWrite (trigPinb, HIGH);
  digitalWrite (trigPinb, LOW);
  durationb = pulseIn (echoPinb, HIGH);
 
  digitalWrite (trigPinc, HIGH);
  digitalWrite (trigPinc, LOW);
  durationc = pulseIn (echoPinc, HIGH);
 
  distanceb = (durationb/2) / 29.1;
  distancec = (durationc/2) / 29.1;
 
  Serial.print ("\nLeft Sensor = ");
  Serial.print (distanceb);
  Serial.print (" cm    ");
  Serial.print ("Right Sensor = ");
  Serial.print (distancec);
}
//----------------------------------------------------------| Ultrasonic container sensor | END

//----------------------------------------------------------| Ultrasonic setup | START
void UltrasonicSetup (){
  pinMode (trigPina, OUTPUT);
  pinMode (echoPina, INPUT);
  pinMode (trigPinb, OUTPUT);
  pinMode (echoPinb, INPUT);
  pinMode (trigPinc, OUTPUT);
  pinMode (echoPinc, INPUT);
}
//----------------------------------------------------------| Ultrasonic setup | END

//----------------------------------------------------------| Engine setup | START
void EngineSetup () {
  pinMode (leftEngineForward, OUTPUT);
  pinMode (leftEngineBackwards, OUTPUT);
  pinMode (rightEngineForward, OUTPUT);
  pinMode (rightEngineBackwards, OUTPUT);
}
//----------------------------------------------------------| Engine setup | END

//----------------------------------------------------------| Led setup | START
void LED () {
  pinMode (led, OUTPUT);
  pinMode (pyLedLeft, OUTPUT);
  pinMode (pyLedRight, OUTPUT);
}
//----------------------------------------------------------| Led setup | END
Last edited by Yoriz on Mon Apr 07, 2014 5:04 am, edited 1 time in total.
Reason: First post lock
Rusoski
 
Posts: 2
Joined: Mon Apr 07, 2014 3:08 am

Re: [URGENT] Python - Arduino problem

Postby Rusoski » Mon Apr 07, 2014 5:11 pm

PROBLEM SOLVED!

I had to delete every line which was sending info to the serial (Serial.print ();).
Rusoski
 
Posts: 2
Joined: Mon Apr 07, 2014 3:08 am


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 2 guests

cron