CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Robo "crazy" with repetitions

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Dave_25152



Joined: 11 Dec 2010
Posts: 60

View user's profile Send private message

Robo "crazy" with repetitions
PostPosted: Sun Jul 17, 2011 7:12 am     Reply with quote

Hello everyone!

Well, I'll be brief in describing the problem, do not want to bore you too much.

The code is what you can see below.

This code causes a "robot" first walk in front, then going to read an IR sensor and when it finds the end of a table, pulls back and turns.

So far so good ... if I follow the code below, it does everything well. The problem arises when, within the program main () I put:

Code:
while(true)
{
frente();
sensor();
para_recua_para();
vira_para();
}


He starts to walk out of control and always "fall ".... should be generated an error somewhere, but I've had enough of walking round to it and I fail to see what it is.
I wanted to enter a contest today with this game, but I can not. Succeeded in making me crazy and apparently .... he also went insane.

What can happen? I've thought it was due to the engines ... since the initiation but put a 47uF capacitor.

It is also the information that I control two motors with an L293 with 8 diodes (two H-bridges).

Code:
#include <16f88.h>
#device adc=10
#fuses NOWDT, NOMCLR, NOBROWNOUT, NOLVP, NOCPD, INTRC_IO,
#use delay (clock=1M)


void frente()
{
   
   output_low(PIN_B5);
   output_high(PIN_B4);//roda esquerda
   output_low(PIN_B3);
   output_high(PIN_B2);//roda direita
}

void sensor()
{
long int s;
   
SET_ADC_CHANNEL(2);

delay_us(50);

   while(s>=450)
   {
   s=read_adc();
   }
}

void para_recua_para()
{
   
output_high(PIN_B3);
output_high(PIN_B5);//para

delay_ms(1000);
output_low(PIN_A1);

output_low(PIN_B4);
output_low(PIN_B2);//recua

delay_ms(900);

output_high(PIN_B2);
output_high(PIN_B4);//para
}

void vira_para()
{
   
output_low(PIN_B4);//roda esquerda
delay_ms(1000);
output_high(PIN_B4);//para
}

void main()
{
set_tris_A(0B00000100);
set_tris_B(0B00000000);

   setup_oscillator(OSC_1MHZ|OSC_INTRC);
   SETUP_ADC(ADC_CLOCK_internal);
   SETUP_ADC_PORTS(sAN2|VSS_VDD);

frente();
sensor();
para_recua_para();
vira_para();
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jul 17, 2011 1:08 pm     Reply with quote

I think your program is too difficult to understand. You should modify
it so the design of the program is easy to see.

For example, you should replace all "PIN_xx" statements with a defined
constant (in upper case), so you can easily see what the pin does.
When you do this, it also helps to make the code be self-documenting.
You don't need to put comments on the line. You can just look at the
line of code see what it does. (I don't know what pins B3 and B5 do,
so I didn't create #define statements for them).
Code:

#define RODA_ESQUERDA  PIN_B2  // Left wheel (in English)
#define RODA_DIREITA   PIN_B4  // Right wheel (in English)

void frente()
{
output_low(PIN_B5);
output_high(RODA_ESQUERDA);
output_low(PIN_B3);
output_high(RODA_DIREITA);
}


That's one improvement. You can also take to the next step, by defining
macro functions for controlling the wheels. This will make it easier to
see what your code is doing, and make it easier for you to see the
problems in the program.
Code:

#define RODA_ESQUERDA  PIN_B2 
#define RODA_DIREITA   PIN_B4   

#define roda_direita_ativo() output_high(RODA_DIREITA)
#define roda_direita_parar() output_low(RODA_DIREITA)

#define roda_esquerda_ativo() output_high(RODA_ESQUERDA)
#define roda_esquerda_parar() output_low(RODA_ESQUERDA)

void frente()
{
roda_direita_ativo();  // right_wheel_on() in English
roda_esquerda_ativo(); // left_wheel_on() in English

// Put other code here.
}


I used the Google Translator to choose the Portuguese words.
I used 'ativo' for active, and 'parar' for stop.
Dave_25152



Joined: 11 Dec 2010
Posts: 60

View user's profile Send private message

PostPosted: Sun Jul 17, 2011 5:24 pm     Reply with quote

OK, my friend!

I'll do as you say.

Thank you very much the time you lost.

With best regards,
David Martins
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group