Society of Robots - Robot Forum

Software => Software => Topic started by: NERO on January 10, 2008, 07:05:43 AM

Title: Usart Problem with CMu cam
Post by: NERO on January 10, 2008, 07:05:43 AM
hallo guys...
i'm already able to communicate with cmu camera with my pic.....
but what is the problem is.... when streaming the data for a while...the communication was stucked...
how do i solve this problem...
tahnks
Title: Re: communication wih cmu
Post by: paulstreats on January 10, 2008, 07:30:36 AM
This happens quite a lot with me if i connect my PIC up to my pc and use keyboard commands to control my bot. If i hold the 'f' key down for it to keep going forwards, the PIC eventually sticks and needs resetting, If i just keep tapping a key so its not a constant stream its fine. Maybe the USART buffer on the PIC is being overloaded, im hoping somebody has resolved this and can let us know
Title: Re: communication wih cmu
Post by: Admin on January 11, 2008, 02:40:25 PM
hmmmm I used to use the CMUcam with a pic, but I never had that problem.

perhaps the baud rate has high error? tried a lower baud rate?

can you try to do software resets if data isn't properly received?
Title: Re: communication wih cmu
Post by: NERO on January 11, 2008, 03:28:38 PM
Quote
can you try to do software resets if data isn't properly received?

could u explain more detail?
Title: Re: communication wih cmu
Post by: Admin on January 11, 2008, 04:56:56 PM
well, first you need to figure out what data you do/don't receive when its stuck.

then have your microcontroller detect this state . . .

finally, re-initiate your PIC uart, then send initialization commands to the CMUcam.


how often does this problem occur? did it only start recently?
Title: Re: communication wih cmu
Post by: NERO on January 11, 2008, 08:14:27 PM
yes...i could not get back data when give ommand tw or tc....it seems the communication is not sychonised....
could you give example of your working code?
thanks
Title: Re: communication wih cmu
Post by: Admin on January 11, 2008, 10:24:46 PM
you haven't answered any of my questions . . .

Have you tried reducing baud yet? What PIC are you using?

my cmucam never got 'stucked', so I never needed to write any un-stuck code . . .
Title: Re: communication wih cmu
Post by: NERO on January 11, 2008, 11:46:56 PM
i have tried for 9600 baud rate and using 16f877a pic...  the main problem is to retrieve the data packet from cmu....
i could not receive the data packet, so i couldn't analyze the data....actually the data has received succesfully, but i couldn't store the information of data packet.....so it was make my cmu camera become stuck.....
Title: Re: communication wih cmu
Post by: Admin on January 12, 2008, 10:57:02 PM
oh . . . so you are receiving the data without a problem, but your code isn't storing the data?

attach your output to rs232 so that you can view it using hyperterminal (http://www.societyofrobots.com/programming_data_logging.shtml)

verify that its sending out the data you expect and let us know
Title: Re: communication wih cmu
Post by: NERO on January 13, 2008, 12:53:23 PM
Quote
so you are receiving the data without a problem, but your code isn't storing the data?

yes....
i have done what u have told...and the result i can see its retrieve the paket of data.....
but the main is my code to capture and storing the data continously....
what should i do?
Title: Re: communication wih cmu
Post by: Admin on January 13, 2008, 04:02:15 PM
Future note, you must tell us more details to get help . . .

It appears you have sent a command, it was properly received, and then the CMUcam is sending the proper data back.

So based on that conclusion, this means your code is improperly storing that received data.

Post your receive code if you want it debugged ;)
Title: Re: communication wih cmu
Post by: NERO on January 13, 2008, 04:47:10 PM
Code: [Select]
#include <system.h>

//Target PIC16F877 configuration word
#pragma DATA _CONFIG, _PWRTE_OFF & _BODEN_OFF & _WDT_OFF & _LVP_ON & _CPD_OFF & _DEBUG_OFF & _HS_OSC & _CP_OFF

//Set clock frequency
#pragma CLOCK_FREQ 20000000
volatile static  char conf ;
volatile static  char ctrx;
volatile static  char ctry;
volatile unsigned int data_rdy;
char ser_tmp;
char M_packet[10];
//**************************************FUNCTION PROTOTYPE DECLARATION***************
void posisi(void);
void set_port();
void ser_init(char spbrg_val);
void ser_tx(char c);
void ser_putstring(const char* text);
const char ser_rcv();
void get_ACK();
void autogainon();
void clamp();
void setupCam();
void get_packetM();
int wait_for_data(void);
int wait_for_data2(void);

//*******************************MAIN BODY************************************
void main( void )
{ //char inc = 'B';

set_port();
ser_init(129); // start up serial port handling

setupCam();
clamp(); 
//Enable interrupts (Timer0)
set_bit(intcon,PEIE); // start up interrupts- required for servo control
    set_bit(intcon, GIE);


// autogain on, etc.
//Endless loop
   

   
   
     
     // set_bit(portb,RB0);
     // delay_s(3);
     // ser_putstring("dm 20\r");
    // get_ACK();
  //ser_putstring("gm\r");
  //get_ACK();
 // clear_bit(portb,RB0);
 
 
   set_bit( pie1, RCIE );    //Enable RX interrupt
 // ser_putstring("tw\r");
  //get_ACK(); 
   
  ser_putstring("tw\r");
   //get_ACK();
 ` // get_packetM();

//if(M_packet[8]==0||M_packet[1]==0||M_packet[2]==0)
// goto point;
//else{
  conf = M_packet[8];
  ctrx = M_packet[1];
  ctry = M_packet[2];
 
  //ser_putstring("pm 1\r");
  //get_ACK();  //set_bit(pie1,5);
 
 
  clear_bit(portb,RB0);
 // clear_bit(intcon,TMR0IE);
 // set_bit(pie1,5);
 
 
  while(1){
//wait_for_data2();
  //ser_putstring("\r");
point2: 
ser_putstring("tc\r");
//get_packetM();
if(M_packet[8]==0&&M_packet[1]==0&&M_packet[2]==0)
goto point2;
delay_s(1);
clear_bit(portb,RB0);
//servo(120);
posisi();
  //get_packetM();
 
  }}
//*******************************function for get the packet of data********************* 
void get_packetM(void)
{
char bufp;
 // while (ser_rcv() != 255) ;
 

 for (bufp = 0; bufp <9; bufp++) {
    M_packet[bufp] = ser_rcv();
  }
  get_ACK();
 
 wait_for_data2();
 // set_bit(portb,RB0);
}

//*******************************function for get the packet of data************************************     
void get_packetM(void)
{
char bufp;
 // while (ser_rcv() != 255) ;
 

 for (bufp = 0; bufp <9; bufp++) {
    M_packet[bufp] = ser_rcv();
  }
  get_ACK();
 
 wait_for_data2();
 // set_bit(portb,RB0);
}




//*******************************function for waiting the packet data************************************
int wait_for_data(void)
{
  int i;

  // This loop below is a counter that gives just enough time to catch
  // an ACK from a normal command. 
  for(i=0; i<20000; i++ )
   
{ if( rcreg==0 )
return 1;}

  return 0;
}

int wait_for_data2(void)
{
  int i;

  // This loop below is a counter that gives just enough time to catch
  // an ACK from a normal command. 
  for(i=0; i<25000; i++ )     
{ if( ser_rcv()==':' )
  return 1;

    else        //( ser_rcv()!=':' )
    ser_putstring("\r");
}}
//*******************************function for setting the port************************************
void set_port(void)
{
//Configure port A
trisa = 0x00;
//Configure port B
trisb = 0x00;
//Configure port C
trisc = 0x00;
//Configure port D
trisd = 0x00;
//Configure port E
trise = 0x00;
//Configure A/D pins
adcon1 = 0x06;
//Initialize port A
porta = 0x00;
//Initialize port B
portb = 0x00;
//Initialize port C
portc = 0x00;
//Initialize port D
portd = 0x00;
//Initialize port E
porte = 0x00;
//Set Timer0 mode
}


//*******************************function for setting the serial communication************************************
void ser_init(char spbrg_val)
{

sspcon = 0;
pir1 = 0;  // this ensures also RCIF = TXIF = 0;
clear_bit(pie1,5); //RCIE = 0;
clear_bit(pie1,4); //TXIE = 0;
trisc =  0xc0; // setup TRISC for USART use
trisb =  0x00;
spbrg = spbrg_val;
    txsta = 00100100b; // txen, brgh
rcsta = 10010000b; // spen and cren, the rest all off
ser_tmp = rcreg; // flush the rx buffer
ser_tmp = rcreg;
ser_tmp = rcreg;
ser_putstring("rs\r");
    get_ACK();
}

//*******************************function for transmit communication************************************
void ser_tx(char c) {
//set_bit( pie1, TXIE );
    //wait for txif to go hi
while (!(pir1 & 16)) ; //while (!TXIF);
txreg = c;

//enable_interrupt(GIE);  //?
}

//*******************************function for write string to transmit************************************

void ser_putstring(const char* text) {
    char i = 0;
   // set_bit( pie1, TXIE );
    while( text[i] != 0 )
        ser_tx( text[i++] );
       // clear_bit(pie1,TXIE);
}

//*******************************function for receiving from cmu camera************************************


// nonblocking receive returns 0 for any error, including nothing to read
const char ser_rcv(void) {
//again:
while (!(pir1 & 32));
{wait_for_data();
//if(pir1 & 32)
// {
ser_tmp = rcreg;
//if(rcreg!=" ")
return ser_tmp;

}
}


//*******************************function for get acknowledgment************************************


void get_ACK(){
int i;
 for(i=0; i<20000; i++ )
   
{ if( ser_rcv() == ':' )
return 1;}
return 0;
 
//while (ser_rcv() != ':') ;
     }

//*******************************function for setting autogainon************************************     
void autogainon() {
char B;
    ser_putstring("cr 18 44 19 33\r"); //18=color mode,44=wb on,19-autoeposure,33=gain on
    get_ACK(); ; // read all reply characters
} // autogainon(); //


//*******************************function for clamp the camera************************************
void clamp() {
//char B;
    ser_putstring("cr 18 40 19 32\r");
    get_ACK();
delay_s(5);
set_bit(portb,RB0);
} // clamp() //


//*******************************function for setting up camera************************************
void setupCam() {
   // char inc;
     delay_ms(200);
     portb=0x00;
     // raw mode on //
     ser_putstring("rm 3\r");
     get_ACK();
     
     autogainon();
     //  noise filter on //
     ser_putstring("mm 1\r");
     get_ACK();
     ser_putstring("nf 1\r");
     get_ACK();
       //  poll mode off //
     ser_putstring("pm 1\r");
     get_ACK();
     ser_putstring("l1 1\r");
     get_ACK();
       // full window mode //
      //ser_putstring("sw 35 65 45 75\r");
      //get_ACK();
// ser_putstring("cr 17 9\r");
  // get_ACK();
     
   //  portb=0x01;
   delay_s(5);
    ser_putstring("l1 2\r");
    get_ACK();
}

This is my code.....hopefully, could solve my problem....
p/s  i clear the led indicator when the data have stored succesfully...
Title: Re: communication wih cmu
Post by: Admin on January 13, 2008, 05:07:01 PM
I think I found your problem (at least one of them)
Code: [Select]
for (bufp = 0; bufp <9; bufp++) {
    M_packet[bufp] = ser_rcv();
  }

There is nothing guaranteeing that you will receive just 9 bytes of info . . .

I'd do something like this:
Code: [Select]
while(1)//fills array up with goodies
{
characterFromCamera = ser_rcv();
if(characterFromCamera == '\r')
{
stringFromCamera[i]='\0';
break;
}
stringFromCamera[i] = characterFromCamera;
i++;
}

and then you have an array to process . . .

Code: [Select]
parseSpacket(stringFromCamera, rmean, gmean, bmean, rdev, gdev, bdev);//store S packet
for each variable then do something like this:
Code: [Select]
//parses S packets for GM command
void parseSpacket(char* inpString,  int &rmean,  int &gmean,  int &bmean,  int &rdev,  int &gdev,  int &bdev)
{
int i = 2;

rmean = atoi(&inpString[i]);

while(inpString[i]!=' ')
{
i++;
}
i++;

...
Title: Re: communication wih cmu
Post by: NERO on January 13, 2008, 06:13:34 PM
ok thanks...i will try implement the code....
could i use this code for tw and tw command?
and could explain your function parseSpacket it's quite confused......
Title: Re: communication wih cmu
Post by: Admin on January 13, 2008, 06:49:19 PM
Quote
could i use this code for tw and tw command?
with modification of variable names, yes


Quote
and could explain your function parseSpacket it's quite confused......
it converts the array into individual values, divided by the space character ' ', then stores that into individual variables such as rmean (red mean value).

by doing i++ it just goes to the next value in the array
Title: Re: communication wih cmu
Post by: SmAsH on January 13, 2008, 06:50:45 PM
ive only done that once and never had a prblem..your leaving me puzzled :-\
Title: Re: communication wih cmu
Post by: NERO on January 14, 2008, 04:09:47 PM
Quote
stores that into individual variables such as rmean (red mean value).

by doing i++ it just goes to the next value in the array

when this function will return?
i see this seem uncompleted yet....
Title: Re: communication wih cmu
Post by: airman00 on January 15, 2008, 06:43:17 AM
ive only done that once and never had a prblem..your leaving me puzzled :-\

yes but which microcontroller did you use?
Title: Re: communication wih cmu
Post by: Admin on January 15, 2008, 09:02:43 AM
Quote
when this function will return?
i see this seem uncompleted yet....
well you have to fill out the rest of the code, I only did one of the variables. just copy/paste what I did for rmean for the other variables.
Title: Re: communication wih cmu
Post by: NERO on January 15, 2008, 09:26:32 AM
how about the M data packet.....it return 8 variable of each data packet.....so could i just  replace parseSpacket fucntion parameter?
Title: Re: communication wih cmu
Post by: Admin on January 15, 2008, 09:33:40 AM
no . . .

lets say you get a packet like this:
128 23 67 9 34 145

if you just read the first 8, you get this:
[1, 2, 8,  , 2, 3,  , 6]

thats why your code doesn't work

if you do my parse code, you get this:
[128, 23, 67, 9, 34, 145]
Title: Re: communication wih cmu
Post by: NERO on January 15, 2008, 12:55:15 PM
Quote
lets say you get a packet like this:
128 23 67 9 34 145
i get your point....but what i,m confusing just now about m packet data which return 8 variable....
i want to make the cmu can track in its field of view... so your sparse function able move 6 variable parameter only...how about he rest of 2 variable....
i'm going to send tw and then tc command....so cmu need to identify m packet data, not s packet data...
am i right?
Title: Re: communication wih cmu
Post by: NERO on January 17, 2008, 08:53:42 PM
Code: [Select]
void get_packetM(void)
{
int i = 1;
while(1)//fills array up with goodies
{
characterFromCamera = ser_rcv();
if(characterFromCamera == ':')
{
stringFromCamera[i]='\0';
break;
}
stringFromCamera[i] = characterFromCamera;
i++;
}}



Code: [Select]
void parseSpacket(char* inpString,  int &rmean1,  int &gmean1,  int &bmean1,  int &rdev1,  int &gdev1,  int &bdev1)
{
int i = 2;


rmean1 = atoi(&inpString[i]);

while(inpString[i]!=' ')
{
i++;
}
i++;
bmean1 = atoi(&inpString[i]);
while(inpString[i]!=' ')
{
i++;
}
i++;
gmean1 = atoi(&inpString[i]);
while(inpString[i]!=' ')
{
i++;
}
i++;
rdev1 = atoi(&inpString[i]);
while(inpString[i]!=' ')
{
i++;
}
i++;
gdev1 = atoi(&inpString[i]);
while(inpString[i]!=' ')
{
i++;
}
i++;
bdev1 = atoi(&inpString[i]);
while(inpString[i]!=' ')
{
i++;
}

}

this two code for function parseSpacket and get_packet that i have modified....but i still have the same problem....my pic cannot do the processing for the received packet...

below code in my main code.....
Code: [Select]
ser_putstring("tw\r");
get_packetM();

parseSpacket(stringFromCamera, rmean, gmean, bmean, rdev, gdev, bdev);

conf = rmean;
ctrx = gmean;
ctry = bmean;
 
clear_bit(portb,RB0);
 
 
do{
set_bit(portb,RB0);
ser_putstring("tc\r");
get_packetM();
parseSpacket(stringFromCamera, rmean, gmean, bmean, rdev, gdev, bdev);
delay_s(1);
clear_bit(portb,RB0);

//posisi();
}while(1);

if the data accaepted the pic will blink the led.....but it not happen....
how could i fix this problem?
Title: Usart Problem with CMu cam
Post by: NERO on March 09, 2008, 03:18:49 AM
hello there...

I'm having problem with my usar module. I'm using PIC 18f452. I'm successfully build the communicaion between

CMU camera and PIC, but afer a while the communication geting stucked. I have check the voltage on usat pin

(Rx & Tx) but i has no reading.... what cause this problem? What should I do? someone please help me  :'( 
Title: Re: Usart Problem with CMu cam
Post by: Admin on March 11, 2008, 06:47:20 AM
Please do not double post!!! I just merged your last post to your old one.

Quote
I have check the voltage on usat pin (Rx & Tx) but i has no reading.
You checked it with an oscilloscope and not a multimeter, right? It won't work with a multimeter.

So are you saying commands from both your PIC and your CMUcam stop transmitting? Or does one just stop?