Pages: [1]   Go Down
Author Topic: newbie trying to shift some bits  (Read 956 times)
0 Members and 1 Guest are viewing this topic.
Adelaide, South Australia
Offline Offline
Newbie
*
Karma: 0
Posts: 20
Arduino? I like it!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello, I am trying to send serial data to the arduino and I am wanting to send large whole numbers in the fastest possible way.

I'm using some python and pyserial as my data input for now. Essentially, here is what I would like to be able to type as the python input command:

Code:
motor.move(2543)

I have started to look at bit sifting as a way to reconstruct things to 2 or more bytes. Example:
Code:
value = (userInput[0] << 8) + userInput[1];
 

Wondering if anyone has a working example of some Arduino code for this sort of thing?  

Also when I run:
Code:
Serial.println(1,BIN);
I get 110001

I thought a byte was 8 numbers. This is only 6? It seems to leave out the zeros at the start if it's not needed. Some ascii tables such as this one http://www.neurophys.wisc.edu/comp/docs/ascii/ list the char 1 as Binary 00110001?

Is this just a serial.print abbreviation or is it actually discarding the first zeros?

Not even sure if this is the right approach for what I am doing? Any suggestions would be most helpful.

Thanks in advance,

Dan.
Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Code:
value = (userInput[0] << 8) + userInput[1];
 

That seems to be correct. You could use the WordCast

Quote
Code:
Serial.println(1,BIN);
I get 110001
...
Is this just a serial.print abbreviation or is it actually discarding the first zeros?
It simply does not print the leading zeros.
Logged

Adelaide, South Australia
Offline Offline
Newbie
*
Karma: 0
Posts: 20
Arduino? I like it!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for clarifying AlphaBeta. So.. Before I try to understand word cast..

I feel I need to be clearer about my problem. Lets assume I know very little about all this.

This is my final goal:
I want to be able to write an arbitrary number to serial using pySerial that can be read 1 for 1 in the Arduino.

I have managed to get this to work with this kind of python command:
Code:
ser.write(chr(255))

Apparently this (chr) wraps any decimal value from 0-255, and you get back its ASCII equivalent. Example (chr65) you get back 'A' which the Arduino sees as 65 in Decimal value when you do Serial.read()

This is all great but it only lets me go so high. What is the best approach to take a very long value say, 12345678 over serial to the Arduino so I can use the exact same value in the Aduino code.

Sorry for being so obvious. But I feel I must to cover all bases.

I'm sure this is a very common task. But I am struggling to find any entry level info on this. So far I've found these links:
http://principialabs.com/arduino-python-4-axis-servo-control/ I found this really usefull if not going over 255
http://antipastohw.blogspot.com/2008/12/passing-large-values-between-arduino-to.html This kinda made sense but was not fully explaining the syntax.
http://www.arduino.cc/playground/Code/BitMath Interesting but don't know if this applies to my problem.

I know there are things like firmata and simple message system. But I would really like to learn the leanest approach to this.

Even if that means sending raw binary and reconstructing it(I don't know how). Can somebody please help me? Suggestions, Links, keywords even! All welcome

Thanks.  smiley
Logged

Connecticut, US
Offline Offline
Edison Member
*
Karma: 2
Posts: 1036
Whatduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Serial.println([glow]'[/glow]1[glow]'[/glow],BIN); should print 110001, while Serial.println(1,BIN); should print 1.

When you say "very large values", how large is the largest value?  Are they always whole numbers?  Positive?  Up to fifteen or fifty digits?
« Last Edit: March 25, 2009, 06:50:52 am by halley » Logged

Adelaide, South Australia
Offline Offline
Newbie
*
Karma: 0
Posts: 20
Arduino? I like it!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It's for driving stepper motors and they could be gear reduced by a up to 60:1. So what if I said, 7 or 8 digits?...

The Serial.print command has some cool ways of translating the data into different data types for display, but not sure how this can help me get over 255 hump?
Logged

Copenhagen / Denmark
Offline Offline
Edison Member
*
Karma: 6
Posts: 2360
Do it !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If you send the number as a string then you can use the atoi() function to convert the string to an integer.

I can't remember if there is a size limit to the integer you can get this way.

atoi() is a standard c function that is not documented in the Arduino reference. Google it to see the syntax.

atoi() will add quite some bytes to the size of your sketch though.

Edit: check this thread: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1176289764
« Last Edit: March 26, 2009, 11:31:53 am by MikMo » Logged

Adelaide, South Australia
Offline Offline
Newbie
*
Karma: 0
Posts: 20
Arduino? I like it!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
If you send the number as a string then you can use the atoi() function to convert the string to an integer.

I can't remember if there is a size limit to the integer you can get this way.

atoi() is a standard c function that is not documented in the Arduino reference. Google it to see the syntax.

atoi() will add quite some bytes to the size of your sketch though.

Edit: check this thread: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1176289764

Thanks for this but I'm not sure why I would need to convert a string to and integer in this instance since I am not sending a string(that i am aware of) from pyserial. I am sending ASCII(which is represented in characters) but it seems that when it is read into the arduino it comes in as the same number I sent.

Example:
pySerial command in a python shell for sending
Code:
ser.write(chr(255))
Arduino command for receiving Data
Code:
startbyte = Serial.read();

The startbyte variable in the Arduino code, behaves as though it's holding the value I sent from python (255). So the relationship is 1 for 1 (or in this case 255 for 255).

This is good and what I want to happen. Only sending the values as ASCII means I can only send a number no higher than 255.

This is the limit for ASCII which is also the maximum number you can reconstruct from one byte(apparently).


Here is where it all gets a bit blurry for me...
Say I wanted to send a value from python which is 2555. I'm sure there are all sorts of ways to do this with the current ASCII encoded approach.
 
Like sending the ASCII Character for 2 then 5 then 5 then 5.
Or, send 25 then 55. Then rebuild these numbers back to the whole number in the Arduino code. But this feels like a cumbersome approach correct?

I'm thinking that there must be some way to get the whole binary number for 2555, then maybe split that into the minimum amount of bytes needed to send it over serial (one byte at a time)  then rebuilt the bytes back into one whole binary number, then convert that into the original 2555 number that was sent from pySerial.

Can anyone tell me if I'm on the right track? Sorry for rambling, I am just thinking out aloud.  :-/
Logged

Copenhagen / Denmark
Offline Offline
Edison Member
*
Karma: 6
Posts: 2360
Do it !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

a group of ascii characters IS a string.  (more or less)

Edit: The problem is that Arduino really just recieves one byte at a time, and has no idea what that byte is a part of.
« Last Edit: March 27, 2009, 04:34:47 pm by MikMo » Logged

Adelaide, South Australia
Offline Offline
Newbie
*
Karma: 0
Posts: 20
Arduino? I like it!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Well the weekend is here. So I will try out some of those suggestions and let you know how it gos. Thanks everyone.
Logged

Adelaide, South Australia
Offline Offline
Newbie
*
Karma: 0
Posts: 20
Arduino? I like it!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Well it took a couple of weekends actually. But I have learned a lot in the process.

Here's a link to the video.

I didn't need to use atoi() in the end, but I'm sure it will come in handy so thanks. I will post the code when I get time to clean it up.

Thanks again for the help,

Dan.
Logged

Pages: [1]   Go Up
Jump to: