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

2 general questions about using a PIC processor
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
elasticman



Joined: 26 May 2009
Posts: 8

View user's profile Send private message

ok i need some help again
PostPosted: Thu Jun 25, 2009 7:19 am     Reply with quote

We are getting some erratic behavior with the robot.
We are using this driver (2 of them - each one can drive 2 motors and we have 3 motors) : [url] http://www.solarbotics.com/assets/datasheets/solarbotics_l298_compact_motor_driver_kit.pdf[/url]

and this motor (3 of them) : http://banebots.com/pc/MP-36XXX-540/MP-36025-540

A PIC18F4431 on a board made by our lab technician with a 4MHz clock onboard and a 5V regulator for the chip.

These are the 3 sensors we are using:
http://www.acroname.com/robotics/parts/gp2y0a02_e.pdf

We are using a battery with 12V 7Ah.

The robot shouldn't move too fast, about a normal human pace so the current shouldn't be too high.

When we test the system without the IR sensors and just make the motors turn (either 2 motors to move forward or 3 motors to turn to either side) everything works...
but when we connect the sensors back we get erratic behavior and we don't know whats wrong exactly.

This is the program:
Code:

#include <18F4431.H>

#device ADC=10

//#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#fuses HS, NOLVP
#use delay(clock = 4000000)

#define PWM_PIN0  PIN_B0 //for the driver board - enable always H for all motors
#define PWM_PIN1  PIN_B1 // motor1 - I1
#define PWM_PIN2  PIN_B2 // motor1 - I2
#define PWM_PIN3  PIN_B3 // motor2 - I3
#define PWM_PIN4  PIN_B4 // motor2 - I4
#define PWM_PIN5  PIN_D6 // motor3 - I1
#define PWM_PIN6  PIN_D7 // motor3 - I2

#define FRONT_SENSOR 6
#define RIGHT_SENSOR 7
#define LEFT_SENSOR 0

static float VOLTAGE_EQUATING_1_METER = 133;

static int first_run_flag = 0;
static float temp_data_from_front_sensor,temp_data_from_right_sensor,temp_data_from_left_sensor;
static float distance_from_front_sensor,distance_from_right_sensor,distance_from_left_sensor;

//-------------------------------
void initialize_adc(void);
void activate_motors_1_and_2_to_move_forward(void);
void deactivate_motors_1_and_2(void);
void activate_all_motors_to_rotate_right(void);
void activate_all_motors_to_rotate_left(void);
void deactivate_all_motors(void);
void decide_what_to_do();
void read_sensors(void);
//====================================

void initialize_adc(void)
{
         setup_adc_ports( ALL_ANALOG );   //defines all pins to be analog
         setup_adc (ADC_CLOCK_INTERNAL);  // the adc uses the same clock speed as the board
}

void activate_motors_1_and_2_to_move_forward(void)
{
         output_high(PWM_PIN1);
         output_low(PWM_PIN2);
         output_low(PWM_PIN3);
         output_high(PWM_PIN4);
}

void deactivate_motors_1_and_2(void)
{
         output_low(PWM_PIN1);
         output_low(PWM_PIN2);
         output_low(PWM_PIN3);
         output_low(PWM_PIN4);
}

void activate_all_motors_to_rotate_right(void)
{
         output_high(PWM_PIN1);
         output_low(PWM_PIN2);
         output_high(PWM_PIN3);
         output_low(PWM_PIN4);
         output_high(PWM_PIN5);
         output_low(PWM_PIN6);
}

void activate_all_motors_to_rotate_left(void)
{
         output_low(PWM_PIN1);
         output_high(PWM_PIN2);
         output_low(PWM_PIN3);
         output_high(PWM_PIN4);
         output_low(PWM_PIN5);
         output_high(PWM_PIN6);
}

void deactivate_all_motors(void)
{
         output_low(PWM_PIN1);
         output_low(PWM_PIN2);
         output_low(PWM_PIN3);
         output_low(PWM_PIN4);
         output_low(PWM_PIN5);
         output_low(PWM_PIN6);
}

void decide_what_to_do()
{
         if ( temp_data_from_front_sensor>=VOLTAGE_EQUATING_1_METER ) // this is
         {                                                          // for the initial operation
               first_run_flag = 1;
               deactivate_all_motors();                              // scan first
         }                                                          // to find the object
         while (first_run_flag == 0 )                               // then keep on
         {                                                          // following it
            activate_all_motors_to_rotate_right();                  //
            delay_ms(60);                                           //
            deactivate_all_motors();                                //
            delay_ms(60);                                           //
            read_sensors();

            if ( temp_data_from_front_sensor>=VOLTAGE_EQUATING_1_METER )//
            {                                                         //
               first_run_flag = 1;                                    //
            }                                                         //
         }
         
         if ( distance_from_front_sensor<VOLTAGE_EQUATING_1_METER ) //voltage is lower for farther object
         {
            while ( distance_from_front_sensor<VOLTAGE_EQUATING_1_METER )
               {
                  activate_motors_1_and_2_to_move_forward();
                  delay_ms(60);
                  deactivate_motors_1_and_2();
                  delay_ms(60);
                  read_sensors();
               }
         }
         if ( distance_from_right_sensor>distance_from_front_sensor )
         {
              while ( distance_from_right_sensor>distance_from_front_sensor )
              {
                activate_all_motors_to_rotate_right();
                delay_ms(60);
                deactivate_all_motors();
                delay_ms(60);
                read_sensors();
              }
         }
         if ( distance_from_left_sensor>distance_from_front_sensor )
         {
              while ( distance_from_left_sensor>distance_from_front_sensor )
              {
               activate_all_motors_to_rotate_left();
               delay_ms(60);
               deactivate_all_motors();
               delay_ms(60);
               read_sensors();
              }
         }
}

void read_sensors(void)
{
         set_adc_channel(front_sensor);
         delay_us(20);
         temp_data_from_front_sensor = READ_ADC( ADC_START_AND_READ );
         distance_from_front_sensor = temp_data_from_front_sensor;
         set_adc_channel(right_sensor);
         delay_us(20);
         temp_data_from_right_sensor = READ_ADC( ADC_START_AND_READ );
         distance_from_right_sensor = temp_data_from_right_sensor;
         set_adc_channel(left_sensor);
         delay_us(20);
         temp_data_from_left_sensor = READ_ADC( ADC_START_AND_READ );
         distance_from_left_sensor = temp_data_from_left_sensor;
}

void main()
{
   initialize_adc();
   output_high(PWM_PIN0); //for the driver board - always have the "enabled" pin set to H ,for all motors
   do
   {
      read_sensors();
      decide_what_to_do();
   }  while (TRUE);
}

//====================================
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jun 25, 2009 11:55 am     Reply with quote

Put some printf statements in your code and display the values read from
the sensors. Also put in some printf's to show the progress of the
program through the various if() statements. Then you can understand
why it's failing.

Also, some comments on your coding style:
Quote:
define FRONT_SENSOR 6
#define RIGHT_SENSOR 7
#define LEFT_SENSOR 0

In the code above, you are correctly putting constants in all upper case.
But then when you use them in your code, you switch to lower case:
Quote:
set_adc_channel(front_sensor);
set_adc_channel(right_sensor);
set_adc_channel(left_sensor);

This is very bad style. It causes confusion to the reader, because in C,
lower case (or mixed case) is normally used for variable names.
Also, unlike CCS, most compilers are case sensistive by default.
They would give an error if you did this.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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