Author Topic: Multiple channel  (Read 3090 times)

0 Members and 1 Guest are viewing this topic.

Offline MohdAsyrofTopic starter

  • Beginner
  • *
  • Posts: 4
  • Helpful? 0
Multiple channel
« on: December 19, 2009, 03:33:15 PM »
Hello friends I'm new here. I need your help. Thanks in advance.
Okay my problem is that I am developing a mobile robot that solves a maze by using ultrasonic, infrared sensor, etc. The problem is I know how to use the multiple channel but I failed to manipulate the data to get the motor function according to the logic.
Here is the idea:

There are 2 sensors, so meaning that it have 4 logic:
Code:

sensor 1     sensor2       motor1       motor2
   0           0             0            0
   0           1             0            1
   1           0             1            0
   1           1             1            1

I know that we can do it directly from sensor to motor but in my case I don't actually use 2 sensors, but 4 sensors and 2 output, so in order to make this function I have to use logic control.

Here is the coding that I developed. It seems not to be functioning properly. It doesn't go to the subroutine maybe????
Code:

#include <16F877A.H>
#device adc=10
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=20000000) //20MHz
#define 1 PIN_C0
#define 2 PIN_C1
#byte PORTB=6
#byte PORTC=7

void fwd()
{
(output_high(pin_b1) && output_high(pin_b1));
delay_ms(20);
(output_low(pin_b0) && output_low(pin_b1));
}

void stp()
{
delay_ms(20);
output_low(pin_b0) && output_low(pin_b1);
}

void rgt()
{
output_high(pin_b1);
delay_ms(20);
output_low(pin_b0) && output_low(pin_b1);
}

void lft()
{
output_high(pin_b0);
delay_ms(20);
output_low(pin_b0) && output_low(pin_b1);
}

   void machai()
   {
   if (output_low(pin_C0) && output_high(pin_C1))
   {
   fwd();
   }
   
   else if (output_low(pin_C0) && output_high(pin_C1))
   {
   rgt();
   }
   
   else if (output_high(pin_C0) && output_low(pin_C1))
   {
   lft();
   }
   
   else if (output_high(pin_C0) && output_high(pin_C1))
   {
   stp();
   }
   }

void main()
{
   
int adc_value0,adc_value1;
int i;


setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_DIV_32);
set_tris_b(0b00000000);
set_tris_c(0b00000011);
set_tris_a(0b00000011);
portc=0;
portb=0;

while(1)
  {

for(i=0;i<2;i++)
   {
   
   set_adc_channel(0); //set to read channel a0 next
   delay_us(20);
   adc_value0 = read_adc(); //get 10 bit readig for channel 0
   if (adc_value0<128) output_low(pin_C0);
   if (adc_value0>128) output_high(pin_C0);
   delay_ms(500);
   
   set_adc_channel(1); //set to read channel a1 next
   delay_us(20);
   adc_value1 = read_adc(); //get 10 bit readig for channel 1
   if (adc_value1<128) output_low(pin_C1);
   if (adc_value1>128) output_high(pin_C1);
   delay_ms(500);
   
   machai();
   }
  }
}

Thanks a million.

Offline Razor Concepts

  • Supreme Robot
  • *****
  • Posts: 1,856
  • Helpful? 53
    • RazorConcepts
Re: Multiple channel
« Reply #1 on: December 19, 2009, 03:49:28 PM »
You have a 500ms delay in there - that doesn't really sound right to me.

paulstreats

  • Guest
Re: Multiple channel
« Reply #2 on: December 19, 2009, 06:46:16 PM »
You havent said how its not working...

does the robot just not move at all, or ias it just not doing as you expected? and if so what were you expecting and what is it actually doing?

A quick observation..


Code: [Select]
void machai()
   {
   if (output_low(pin_C0) && output_high(pin_C1)) ************
   {
   fwd();
   }
   
   else if (output_low(pin_C0) && output_high(pin_C1)) ***********
   {
   rgt();
   }
   
   else if (output_high(pin_C0) && output_low(pin_C1)) xxxxxxxxxxxxxxxxxx
   {
   lft();
   }
   
   else if (output_high(pin_C0) && output_high(pin_C1)) xxxxxxxxxxxxxxxxx
   {
   stp();
   }
   }



Its not working because the conditional statements marked with * 's are the same and the conditional statements marked with x's are the same.(not only that, but by your code they are setting the ports as outputs states rather thasn checking what their input is?)


Take the x's. the robot cannot go forward and go right at the same time, the robot would just end up going right because thats the last If with the same conditions.

If you want the robot to go forwards and right at the same time then you must write a new function for that capability.

Offline hopslink

  • Robot Overlord
  • ****
  • Posts: 202
  • Helpful? 14
Re: Multiple channel
« Reply #3 on: December 19, 2009, 07:01:24 PM »
...
void fwd()
{
(output_high(pin_b1) && output_high(pin_b1));
...
}
Here it looks like you set the same pin (b1) twice. Suggest maybe you want b0 and b1.

Quote
...
   void machai()
   {
   if (output_low(pin_C0) && output_high(pin_C1))
   {
   fwd();
   }
   
   else if (output_low(pin_C0) && output_high(pin_C1))
   {
   rgt();
   }
   
The logic expression for your if statement seems identical in both these cases ? Suggest fwd should be called on C0 low and C1 low.

The for loop you use seems redundant as you never actually use the value of i and it is nested in a constant while loop. Not a logic error, just me being picky :P

Also as pointed out you spend a second reading the adc values and only 20ms driving your motors.
 

Offline MohdAsyrofTopic starter

  • Beginner
  • *
  • Posts: 4
  • Helpful? 0
Re: Multiple channel
« Reply #4 on: December 20, 2009, 04:10:20 AM »
it looks like there's a confusion in the questons...
lets me make it clear...

the output of port C acts like a logic to the program
Hence:

pin C0           pin C1              motor1            motor2
low               low                  on                  on
low               high                 off                  on
high               low                 on                  off
high              high                 off                  off

note that motor output is using port b

it is possible to use the port output and manipulate it to run the motor....or we need to do it with a different method? ;D


Offline hopslink

  • Robot Overlord
  • ****
  • Posts: 202
  • Helpful? 14
Re: Multiple channel
« Reply #5 on: December 20, 2009, 05:30:36 AM »
All of the answers given above still apply.

You can use an output port to control the motor (how else would you do it?  :P) as long as you are not trying to power the motor from the port.

Using portC output registers for logic in your code is possible but you need to be careful. It makes it easier to for errors to creep in. As paulstreats points out  the functions output_low() and output_high() write to bits in the port output registers, you are using this function to try and read bits. As you replace this with code to read bits make sure you read the port output register and not the port pins.

paulstreats

  • Guest
Re: Multiple channel
« Reply #6 on: December 20, 2009, 01:58:04 PM »
Can you tell us more about how its not working?

eg.

does the code compile at all?

if the code compiles and runs on your microcontroller, how is it acting? does the robot move or does it move but not how you expected.

We understand what youre trying to do as far as the logic goes but without actually knowing whats going wrong its hard to help.

 

SMF spam blocked by CleanTalk