Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Begginer help: comparing bits


  • Please log in to reply
5 replies to this topic

#1 capn

capn

    Member

  • Members
  • PipPip
  • 14 posts
  • LocationNC

Posted 12 February 2011 - 03:53 AM

Hey everyone!

I am very new to micro-processors and programming in general, but I am pretty good with discrete electronics but I am by no means an expert.

I want to start some threads that will help very base level beginners like myself understand some basic concepts.

My question for today is, how do I compare bits for say, direction control of motors?

I understand that if I had an analog input (0-3.3V) it gets converted to a 10 bit integer, 0-1023, assuming that I haven't changed the range.

Can you more experienced people help me fill in my pseudo code?

public static void Main()
{
InputPort Xaxis = new InputPort(pins.analogPinA1);       // Defining the analog input port as my joystick axis 
OutputPort Direction1 = new OutputPort(pins.outputpin1); // Defining the output port to a motor driver direction 1 enable
OutputPort Direction2 = new OutputPort(pins.outputpin2); // Defining the output port to a motor driver direction 2 enable

//I have made the range for directions 0-1023, with a 128bit "dead zone"

For(Xaxis > 576bits)
   {
   Direction1.value = true;               //this gives me direction forward
   }

For(Xaxis < 448bits)
   {
   Direction2.value = true;                //this gives me direction reverse
   }

}

Be patient with me, I'm new to this.


#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 12 February 2011 - 04:46 AM

Hi capn,

[Quick note: you'll want to say "Xaxis > value of 576" and "Xaxis < value of 448" (i.e. value rather than bits). I totally know what you mean, but a lot of people will get confused or might ignore your post due to the accidental misnaming. Odd but true.]

For Xaxis, instead of creating an InputPort you'll want to create an AnalogInput. And actually, I'd do something like:
AnalogInput xAxisInput = new AnalogInput(Pins.GPIO_PIN_A1);
int xAxisValue= xAxisInput.Read();

if (xAxisValue > 576)
{
    Direction1.Value = true;
}
else if (xAxisValue <448)
{
    Direction2.Value = true;
}


#3 capn

capn

    Member

  • Members
  • PipPip
  • 14 posts
  • LocationNC

Posted 12 February 2011 - 05:00 AM

Thanks Chris! I know I wasn't right with the nomenclature, I just wanted to make the code decipherable.

This might be a rudimentary question, but why use an "IF" statement instead of a "FOR" loop?

Also would it be more right to have the direction outputs as:

if (xAxisValue > 576)
            {
                Direction1.Write(true);     //gives the motor 1 direction 1 a true reading, this is forward
            }
            else if(xAxisValue < 448)
            {
                Direction2.Write(true);     //gives the motor 1 direction 2 a true reading, this is reverse
            }

Be patient with me, I'm new to this.


#4 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 12 February 2011 - 08:46 AM

This might be a rudimentary question, but why use an "IF" statement instead of a "FOR" loop?


A for loop executes a section of code one or more times (depending on the parameters that you give to 'for'). An if block executes only if something is/is not true. In this case, you'll probably want to read the value over and over again--either on a timer or in an infinite loop ("while(true)"). There are a lot of good C# primers out there which describe the various operations like for and if...would it bef halpful if we found a good one to recomend to you?

Also would it be more right to have the direction outputs as:

if (xAxisValue > 576)
{
    Direction1.Write(true);     //gives the motor 1 direction 1 a true reading, this is forward
}
else if(xAxisValue < 448)
{
    Direction2.Write(true);     //gives the motor 1 direction 2 a true reading, this is reverse
}


Yes, actually the code I wrote above (Direction1.Value) isn't correct. Sorry about that! But if you have two motors controlling "forward" and "back", you'll want to go one step further--turn _off_ the one you're not using! Otherwise they'll both try to run.

if (xAxisValue > 576)
{
    Direction1.Write(true);     //gives the motor 1 direction 1 a true reading, this is forward
    Direction2.Write(false);
}
else if(xAxisValue < 448)
{
    Direction2.Write(true);     //gives the motor 1 direction 2 a true reading, this is reverse
    Direction1.Write(false);
}

Also, if I might make a recommendation: rename Direction1 and Direction2 to something like "forwardMotorOutput" and "reverseMotorOutput"... It'll make the code easier to understand and maintain.

Chris

#5 capn

capn

    Member

  • Members
  • PipPip
  • 14 posts
  • LocationNC

Posted 12 February 2011 - 04:51 PM

A for loop executes a section of code one or more times (depending on the parameters that you give to 'for'). An if block executes only if something is/is not true. In this case, you'll probably want to read the value over and over again--either on a timer or in an infinite loop ("while(true)"). There are a lot of good C# primers out there which describe the various operations like for and if...would it bef halpful if we found a good one to recomend to you?


Of course! I am always looking for more information. And yes, I will want to read this value over and over again, that's why I had initially put the code within a for loop.

Yes, actually the code I wrote above (Direction1.Value) isn't correct. Sorry about that! But if you have two motors controlling "forward" and "back", you'll want to go one step further--turn _off_ the one you're not using! Otherwise they'll both try to run.

if (xAxisValue > 576)
{
    Direction1.Write(true);     //gives the motor 1 direction 1 a true reading, this is forward
    Direction2.Write(false);
}
else if(xAxisValue < 448)
{
    Direction2.Write(true);     //gives the motor 1 direction 2 a true reading, this is reverse
    Direction1.Write(false);
}

Also, if I might make a recommendation: rename Direction1 and Direction2 to something like "forwardMotorOutput" and "reverseMotorOutput"... It'll make the code easier to understand and maintain.

Chris



In this example I am only controlling one motor which has the possibility to run in forward or reverse. And in my realm of hardware, I need two outputs for each motor as each motor will have its own respective H-bridge; like so:

Posted Image

So for my robot, it would essentially have an If...ElseIf statement for each motor?

Be patient with me, I'm new to this.


#6 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 13 February 2011 - 08:31 AM

So for my robot, it would essentially have an If...ElseIf statement for each motor?


I believe so. If motor control is all you're doing, you can put it all in one big "while(true)" loop.

while(true)
{
    // process analog input for motor 1
    [code goes here]

    // process analog input for motor 2
    [code goes here]
}





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.