element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
STEM Projects
  • Learn
  • Learning Center
  • STEM Academy
  • STEM Projects
  • More
  • Cancel
STEM Projects
Blog Virtual Eye System -- Post #8 -- Code optimisation and Error correction
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join STEM Projects to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: nikhil22
  • Date Created: 8 Nov 2015 7:17 PM Date Created
  • Views 497 views
  • Likes 0 likes
  • Comments 0 comments
  • object_tracking
  • rasp_berry
  • tilt/pan
  • teachers_pet
  • virtual_eye_system
  • teachers pet robotics design challenge
  • rasp_pi
  • picam
Related
Recommended

Virtual Eye System -- Post #8 -- Code optimisation and Error correction

nikhil22
nikhil22
8 Nov 2015

Hi guys!!!

Although the Teacher's Pet Students' Robotics Challenge has concluded, I found that the code mentioned in my previous blog post has some bugs and I have optimised it in this post and have added a video showing the same. The response is faster and better than the previous one.

MATLAB code

clear rpi
clear cam
rpi=raspi();
myserialdevice = serialdev(rpi,'/dev/ttyAMA0',9600);
cam = cameraboard(rpi, 'Resolution', '640x480');
cam.Rotation=180;
edit('track.m');thresh=50;
figure(1);
for i = 1:500
    %% Extract RGB color components
    img=snapshot(cam);
r = img(:,:,1);
g = img(:,:,2);
b = img(:,:,3);

%% Calculate green
justGreen =g - b/2 - r/2;
%% Threshold the image
bw = justGreen > thresh;


%% Find center
[x, y] = find(bw);
if ~isempty(x) && ~isempty(y)
    xm = round(mean(x));
    ym = round(mean(y));
    fprintf('x-coordinate: %d\n', xm);    
    fprintf('y-coordinate: %d\n', ym);
    if xm<200 
        if ym<200
            write(myserialdevice,[1]);%inside ''...it is normal values w/o.....it takes ASCII
        elseif ym>500
            write(myserialdevice,[2]);
        end
    elseif xm>300
        if ym<200
            write(myserialdevice,[3]);%inside ''...it is normal values w/o.....it takes ASCII
        elseif ym>500
            write(myserialdevice,[4]);
        end
   end
    %% Creat a red dot
    xx = max(1, xm-5):min(xm+5, size(bw, 1));
    yy = max(1, ym-5):min(ym+5, size(bw, 2));
    bwbw = zeros(size(bw), 'uint8');
    bwbw(xx, yy) = 255;
    
    %% Create output image
    img(:,:,1) = uint8(r + bwbw);
    img(:,:,2) = uint8(g - bwbw);
    img(:,:,3) = uint8(b - bwbw);
end
    subplot(211);
    imagesc(img);
    subplot(212);
    imagesc(bw);
    drawnow;


end

Instead of sending signed integer, we are now sending unsigned integers for four possible cases of an object being in either of the four corners of the camera frame.

The corresponding arduino code for controlling the servo is as follows:

 

#include <Servo.h> 
//Orange Signal
//Red Positive
//Brown GND
Servo myservo1;  
Servo myservo2;

int x,y,i=0,j=0;   

void setup() 
{ 
  myservo1.attach(A0);  
  myservo2.attach(A1);
  myservo1.write(45);
  myservo2.write(120);
  Serial.begin(9600);
  } 

void loop() 
{   while(Serial.available()>0){
  x=Serial.read();
  
  i=myservo1.read();
  j=myservo2.read();
  Serial.println(x);
  if(i<=90 && i>=0 && j<=180 && j>=90){
  if(x==1){
    i=myservo1.read();
    myservo1.write(i+2);
    j=myservo2.read();
    myservo2.write(j+2);}
  else if(x==2){
    i=myservo1.read();
    myservo1.write(i-2);
    j=myservo2.read();
    myservo2.write(j+2);}
  else if(x==3){
    i=myservo1.read();
    myservo1.write(i+2);
    j=myservo2.read();
    myservo2.write(j-2);}
  else if(x==4){
  i=myservo1.read();
    myservo1.write(i-2);
    j=myservo2.read();
    myservo2.write(j-2);}
}
else {myservo1.write(45);
myservo2.write(120);}
}
}

As said, the four cases are handled accordingly by turning the servo by 2 degrees per reading received.

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

Happy Coding!!!

  • Sign in to reply
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube