Show Posts
Pages: 1 ... 50 51 [52] 53
766  Forum 2005-2010 (read only) / Syntax & Programs / Re: Urgent - Split Long into 2 Int using BitShift on: January 20, 2011, 03:22:11 pm
The following should work to convert the two ints back into a long:

newlong = ((long)H << 16) | L;

But it will depend on the platform and programming environment you are working in to conver it back.  This assumes that H and L are two 16 bit variables, and that (long) is a 32 bit variable.  On most modern PCs and programming languages, ints are 32 bit variables and longs are 64 bit variables.  H and L may need to be declared as shorts and your 32 bit variable just declared as an int. (and the cast would be (int) instead of (long) as well)
767  Forum 2005-2010 (read only) / Syntax & Programs / Re: Urgent - Split Long into 2 Int using BitShift on: January 20, 2011, 01:15:03 pm
I'm not quite sure what your expectations are here.  The intermediate int decimal values will in no way correlate to the original long decimal value.  If they are only being used as a necessary step to transfer the entire data, then that doesn't matter.

What matters is that the bit representation is maintained.
Say you have a long value of (just for example) 1,582,081.
It's binary representation would be:
00000000 00011000 00100100 00000001

If you then assign that value to two ints with bitwise shifting, you would end up with
high int 00000000 00011000
low  int 00100100 00000001

If you then properly recompose that back into a new long, that new long will have the same decimal value as the previous one (assuming the binary representation of that decimal value is the same across any platform transitions).

You could use unsigned ints for your intermediate variables and it wouldn't make a difference.  You could even use 4 bytes or chars instead.

Quote
With -1 as ValueLong I read on serial monitor:

L = 11111111111111111111111111111111
H = 11111111111111111111111111111111

That looks completely correct.  Signed ints are stored in a twos complement format.  Google it for more info on how twos complement works, and why it's an ideal format to use for signed ints.
768  Forum 2005-2010 (read only) / Syntax & Programs / Re: Urgent - Split Long into 2 Int using BitShift on: January 20, 2011, 11:44:22 am
Bitwise manipulation in general doesn't care one bit (no pun intended) about the sign or format of the variable they are operating on.

If you decompose a 4 byte variable into two 2 byte variables, and then later recompose them back into a 4 byte variable, the bit patterns will remain identical.

That's not to say there won't be any sign issues, but they will not be related in any way to the bitwise operations (for example, there may be endian problems if you are transferring the data between platforms with different endian'ness').
769  Forum 2005-2010 (read only) / Syntax & Programs / Re: Urgent - Split Long into 2 Int using BitShift on: January 20, 2011, 11:28:21 am
Just use:

HighINT = ValueLong >> 16;
LowINT  = ValueLong;

And be done with it.  The masking is unnecessary.  The results won't match your other method, but that is because, as AWOL has correctly pointed out, your other method is incorrect.
770  Forum 2005-2010 (read only) / Syntax & Programs / Re: -output value to +output value? on: January 20, 2011, 04:16:56 pm
What you have is almost what you need.  Just needs two tweaks...

change +output to -output in your second analogWrite.

One of your if statements should handle the case where outputValue = 0.  Either use >= in the first line, or <= in the second line.
771  Forum 2005-2010 (read only) / Syntax & Programs / Re: Troubles with 4 servos and 2 dc motors on: January 11, 2011, 10:41:46 am
You shouldn't be using separate serial reads in your code.  Add your motor control code into the first serial read switch/case.

Code:
if ( Serial.available()) {
    char ch = Serial.read();

    switch(ch) {
        case '0'...'9':
           switch(readState) {
             case STATE_NONE:
               break;
             case STATE_PINNUMBER:
               pinNumber = pinNumber * 10 + (ch -'0');
               break;
             case STATE_POSITION:
               position = position * 10 + (ch -'0');
               break;
           }
           break;
        case 'g':
           servos[pinNumber].write(position);
           pinNumber = 0;
           position = 0;
           readState = STATE_NONE;
           Serial.print("p");
           Serial.print(pinNumber);
           Serial.print("m");
           Serial.print(position, DEC);
           Serial.print("OK");
           break;
        case 'p':
           readState = STATE_PINNUMBER;
           break;
        case 'm':
           readState = STATE_POSITION;
           break;
        case 's':
            motor3.run(FORWARD);
            motor4.run(FORWARD);
            delay(17);
            motor3.run(RELEASE);
            motor4.run(RELEASE);
            break;
        case 'w':
            //motor control code
            break;
        case 'a':
            //motor control code
            break;
        case 'd':
            //motor control code
            break;
    }
 }
772  Forum 2005-2010 (read only) / Syntax & Programs / Re: Calculating distance between two points accurately on: January 07, 2011, 01:03:48 pm
Quote
If I have a latitude (as a string) of say 12345678, I can convert this to a float or double such as 123.45678.

This may be part of your accuracy problem (if it's not just a trivial example without proper conversion).

In your first post you say you're getting a string as  DDDMMmmm, which is degrees, minutes, and decimal fractions of a minute.  Based on this format, 12345678 does NOT equal 123.45678 degrees.

It is 123 degrees and 45.678 minutes.

45.678 minutes = 45.678/60 degrees, or 0.7613 degrees.

So 12345678 would then equal 123.7613 degrees.  If your actual calculations are indeed making the proper conversion, then ignore this post.
773  Forum 2005-2010 (read only) / Interfacing / Re: No Interrupt Tachometer on: January 23, 2011, 04:15:21 pm
Why do you believe an interrupt would greatly disrupt your other functions?  As long as the ISR is kept short and fast, there should be no discernible interruption to your other routines.
774  Forum 2005-2010 (read only) / Interfacing / Re: Satellite control? on: January 20, 2011, 04:22:42 pm
Possible:  Yes.
Practical for a hobby RC car:  Not even remotely.
775  Forum 2005-2010 (read only) / Interfacing / Re: What do I need to do a Robot with wheels, ... on: January 20, 2011, 04:08:24 pm
One easy avenue available is to get 4 continuous rotation servos and some wheels that can be mounted to them.  For example, four of these:
http://www.robotshop.ca/roboblock-continuous-rotation-servo-wheel.html
They can be driven (though not powered) directly from an Arduino, and they can usually be powered from the same power source as the Arduino.

If you go with 4 separate motors for drive power, then you are also going to have to get a motor controller that is capable of driving those 4 motors.  It'll be more expensive and more complicated to set up.  something like:
http://www.robotshop.ca/solutions-cubed-ezr-5.html
http://www.robotshop.ca/solutions-cubed-easy-roller-robot-wheel-ezr2-1.html
could be used, but require a motor controller to drive them, something like:
http://www.robotshop.ca/Sabertooth-2x5-en-5.html

That latter setup would have a higher payload capacity and higher top speed than the servo setup, but also a higher cost and higher learning curve.  Being a novice, I'd recommend the servo route, and upgrade when you outgrow them (also, continuous rotation servos have other uses within hobby robotics, so you would likely be able to repurpose them if/when you outgrow them, as opposed to just tossing them aside)

As another option, either setup can also be reduced to a two-wheel arrangement with a caster wheel to reduce the costs by 30-50%.
776  Forum 2005-2010 (read only) / Interfacing / Re: What do I need to do a Robot with wheels, ... on: January 20, 2011, 03:33:49 pm
Do you want to piecemeal your own custom 4 wheel base?  If so, that's going to mean custom fabricating your own chassis from scratch.  If that's what you want, I can easily given you a list of required components to build a fully functional 4 wheel base (excluding the custom chassis to bolt everything to).

I'd personally recommend just buying a prefab kit.  Which kit is most suitable to you will depend on a variety of factors.  What kind of payload capacity are you looking for?  What kind of environments do you want it to operate in?  What is your budget?
777  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: Stepper motor using Arduino on: January 20, 2011, 04:29:36 pm
How much torque do you need?
How slow is slow?
How accurate does your slow winding need to be?
778  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: How fast does arduino process each line of code? on: January 20, 2011, 09:04:57 am
Quote
Also say you have the most simplest photo flash type code of all, no delays, just a simple "when input #1 is high, make output #1 high".  What sort of timeframe does it take for it to actually register the input and produce the output?  Is it within the range of mere nanoseconds, or microseconds, or what?

Microseconds.  You could probably get it down to the nanosecond range if you use an interrupt.  Not really sure exactly how quickly the avr responds to an interrupt.  Never had the need to time something anywhere near that fast.
779  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: How fast does arduino process each line of code? on: January 19, 2011, 03:11:28 pm
It can often be very beneficial to implement a state machine with a high response 'run' mode and separate isolated setup mode.

It doesn't have to be a toggle switch though.  Could be a serial command.  A momentary pushbutton that that toggles between 'run' and setup, or just automatically drops back into 'run' mode if there is some period of inactivity (user enters setup, but doesn't do anything for 10 second, the unit switches back to 'run' mode).  It could offer a window of time on initial bootup to enter setup (say 2-5 seconds), after which it enters runmode with no option to go to setup other than resetting the unit.  Different methods will be suited to different projects.  
780  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: How fast does arduino process each line of code? on: January 19, 2011, 02:47:34 pm
@AWOL:  You are entirely correct, and that was the primary reason I used it as an example of how the execution time for lines of code can and will vary so wildly.
Pages: 1 ... 50 51 [52] 53