Pages: [1] 2   Go Down
Author Topic: need help in codes!  (Read 2269 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 40
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

what went wrong in this sketch?
please enlighten me


#include "math.h"
void setup()
{
  Serial.begin(9600);           // set up Serial library at 9600 bps
   double x , y, a ;
    x = -5;
    y = 5;
    a=atan2(y,x);
    Serial.print("arctan2 = ");
    Serial.println(a);
  
  
}

void loop()
{

}
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 40
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i know due to Serial.println() doesn't support floating point arguments...
can someone tell me how to solve it?
Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6250
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The following thread comes up with a google search for: arduino serial floating point
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1207226548

Have fun!
« Last Edit: October 05, 2008, 01:42:29 am by mem » Logged

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 124
Posts: 6651
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void printfloat(double f)
/*
 * Print a floating point number to two decimal places
 * beware output range: int part should be less than 32768...
 */
{
  int intpart, fracpart;
  
  intpart = (int)f;
  fracpart = ((int)(100.0*f + 0.5)) - 100 * intpart;  // grab two decimal places.
  Serial.print(intpart);
  Serial.print('.', BYTE);
  if (fracpart < 10) { // check if we need to explicitly handle leading zero
     Serial.print('0', BYTE);
  }
  Serial.println(fracpart);
}
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 40
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset



some questions regarding C again.

i'm using arduino to control this servo ,the protocol required 4-6bytes of data send to the servo and it will response back 2 bytes(Data1,Data2).


1)i called a BoundSet() in the void setup() that will send 6bytes to the servo,so must the data type  be able to hold 6 bytes?

i.e  
Code:
byte a;      // do i need a bigger capacity data type?
      a=BoundSet();

these codes are from the servo manual.
Code:
/*------------------ Function relating to serial communication ----------------*/
void SendByte(char data); // Send 1 Byte to serial port
void GetByte(char timeout); // Receive 1 Byte from serial port



/******************************************************************************/
/* Function that sends Set Command Packet(6 Byte) to wCK module */
/* Input : Data1, Data2, Data3, Data4 */
/* Output : None */
/******************************************************************************/
void SendSetCommand(char Data1, char Data2, char Data3, char Data4)
{
char CheckSum;
CheckSum = (Data1^Data2^Data3^Data4)&0x7f;
SendByte(HEADER);
SendByte(Data1);
SendByte(Data2);
SendByte(Data3);
SendByte(Data4);
SendByte(CheckSum);
}

i replaced SendByte() by ie. Serial.print(data1,BYTE) and it works well


Code:
/***********************************************************************/
/* Function that sends Boundary Set Command to wCK module */
/* Input : ServoID, *NewLBound, *NewUBound */
/* Return : 1 if succeed, 0 if fail */
/**********************************************************************/
char BoundSet(char ServoID, char *NewLBound, char *NewUBound)
{
char Data1,Data2;
SendSetCommand((7<<5)|ServoID, 0x11, *NewLBound, *NewUBound);
Data1 = GetByte(TIME_OUT2);
Data2 = GetByte(TIME_OUT2);
if((Data1==*NewLBound) && (Data2==*NewUBound)) return 1;
return 0;
}

2)can i replaced GetByte(TIME_OUT1) and GetByte(TIME_OUT2) with Serial.read()?

3)*NewLBound, *NewUBound...where should i call this 2 pointers?

4)since there is timeout1 and timeout2.Does this mean Data1 and Data2?

5)what do you think the code in void GetByte(char timeout)should be like? is it equilvalent to Serial.read()?
  but Serial.read() doesnt seem to read the response from the servo or i need to write another function?


Code:
/*************************************************************************************************/
/* Function that sends Position Move Command to wCK module */
/* Input : ServoID, SpeedLevel, Position */
/* Output : Current */
/*************************************************************************************************/
char PosSend(char ServoID, char SpeedLevel, char Position)
{
char Current;
SendOperCommand((SpeedLevel<<5)|ServoID, Position);
GetByte(TIME_OUT1);
Current = GetByte(TIME_OUT1);
return Current;
}

6)since the servo response with 2bytes.does this function GetByte(TIME_OUT1) reads 1 or 2 bytes?

7)why do we need to return current since we are only interested in sending the speedlevel,servoid and position?
Logged

Austin, TX USA
Offline Offline
God Member
*****
Karma: 4
Posts: 997
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey gr33nhorn--

Your code seems to be coming along well.  You don't need to change BoundSet's return type, since it simply reporting whether the process succeeded (1) or failed (0) and not really returning data.

It seems to me that the code example expects GetByte to wait a certain amount of time (TIME_OUT1 or TIME_OUT2) for a byte to arrive on the serial port.  If this is true, and assuming TIME_OUT1 and TIME_OUT2 measure time in milliseconds, you could write your own GetByte like this:

Code:
char GetByte(unsigned long timeout)
{
  unsigned long start = millis();
  do
    if (Serial.available())
      return Serial.read();
  while (millis() - start <= timeout);
  return 0;
}

GetByte() should return 1 byte; that's why you call it twice to get 2.

I don't know why BoundSet uses pointers, frankly, but you'd call it like this:
Code:
char lbound = 100; // I'm guessing it's an angle?
char ubound = 180; //?
if (BoundSet(myservoid, &lbound, &ubound))
  Serial.print("It worked!!");
else
  Serial.print("I guess it didn't work.");

The return value from PosSend is probably an indication that SendOperCommand succeeded.

Good luck!

Mikal
Logged

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 124
Posts: 6651
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I dunno about having the same postings in two forums (identical questions have been posted on ladyada's forums), but in case people are following here instead of both places...

1) no, the "return code" from the function doesn't need to be related in size to the amount of data sent.

2) Probably not.  Serial.read() always returns immediately (with -1 if no data exists); but it looks like they want getbyte() to wait for data for up to TIMEOUT (milliseconds?)

3) I don't think your newbound parameters need to be pointers.  I'm not sure what you're trying to do there.

4) I suspect TIME_OUT1 (which isn't used in the code pieces you posted, and TIME_OUT2 are values  that represent  the amount of time the servo controller might take to respond to a command, unrelated to Data1 and Data2.

5) GetByte(timeout) probably waits for up to timeout milliseconds for a serial character to be available.  It would look something like:
Code:
int GetByte(long timeout)
{
   unsigned long starttime = millis(); // when we started.
   while (millis() - starttime < timeout) {   // while we haven't timed out
      if (Serial.available() > 0) {  // See if there is data available
         return Serial.read();        // if so, return it to the caller.
      }
   }
   // We have exceeded the timeout without seeing any data.
   return -1;
}

6) The way I read it (do you have a link to the servo controller manual?), GetByte returns one byte for each call.

7) I dunno.  Perhaps if things work, current will equal the new position.  Perhaps the code is returned before the servo has finished moving, and indicates where the actual servo position is.  Remember that the servo moves in timeframes on the order of 0.1 second, while the Arduino code runs in timeframes on the order of 0.000001 seconds...
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 40
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I dunno about having the same postings in two forums (identical questions have been posted on ladyada's forums), but in case people are following here instead of both places...
;D..i just want to share my problems with more people..

here's the link for the manual http://robosavvy.com/RoboSavvyPages/Robobuilder/robobuilder-creator-users-manual.pdf

hope there will be more people want to control these servos with arduino.

hope to see more reply from you westfw and others
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 40
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks valuable codes guys..i have solved my problems ;D
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 40
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

im back with new problem
Code:
#include <math.h>
#define LastID 11
#define Header 0xff  
#define Torque 4

byte PosArray[]={/* ID 0, 1, 2, 3, 4, 5,  6,  7,  8,  9,  10, 11 */
                       5,20,40,60,80,100,120,140,160,180,200,220
                };

byte *PosPtr;

void setup()
  {
    Serial.begin(115200);
    PosPtr=PosArray;
    SyncPosSend(LastID,Torque,*PosPtr);
  }
  
void loop()
{
  
}

void SyncPosSend(byte LastID, byte Torque ,byte *PosPtr)
{
  byte i, Checksum;
  i=0;
  Checksum=0;
  Serial.print(Header,HEX);
  Serial.print((Torque<<5)|0x1f,HEX);
  Serial.print(LastID+1,HEX);
    for (i=0; i<=3; i++)
      {
        Serial.print(*(PosPtr+i),DEC);
        Checksum ^=*(PosPtr+i);
      }
      
  Checksum &=0x7f;
  Serial.print(Checksum,HEX);
  
}



error :
Code:
error: expected ',' or '...' before numeric constant In function 'void setup()':
 At global scope:

can somebody enlightened me?

thanks in advance
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 40
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

im back with new problem
Code:
#include <math.h>
#define LastID 11
#define Header 0xff  
#define Torque 4

byte PosArray[]={/* ID 0, 1, 2, 3, 4, 5,  6,  7,  8,  9,  10, 11 */
                       5,20,40,60,80,100,120,140,160,180,200,220
                };

byte *PosPtr;

void setup()
  {
    Serial.begin(115200);
    PosPtr=PosArray;
    SyncPosSend(LastID,Torque,*PosPtr);
  }
  
void loop()
{
  
}

void SyncPosSend(byte LastID, byte Torque ,byte *PosPtr)
{
  byte i, Checksum;
  i=0;
  Checksum=0;
  Serial.print(Header,HEX);
  Serial.print((Torque<<5)|0x1f,HEX);
  Serial.print(LastID+1,HEX);
    for (i=0; i<=3; i++)
      {
        Serial.print(*(PosPtr+i),DEC);
        Checksum ^=*(PosPtr+i);
      }
      
  Checksum &=0x7f;
  Serial.print(Checksum,HEX);
  
}



error :
Code:
error: expected ',' or '...' before numeric constant In function 'void setup()':
 At global scope:

can somebody enlightened me?

thanks in advance
Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6250
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your #defines were clashing with the names of the arguments to SyncPosSend.

Code:
#define LastID 11
#define Header 0xff  
#define Torque 4

byte PosArray[]={5,20,40,60,80,100,120,140,160,180,200,220 };

void setup()
{
  Serial.begin(115200);
  SyncPosSend(LastID,Torque,PosArray);
}

void loop()
{

}

void SyncPosSend(byte _LastID, byte _Torque ,byte *_PosPtr)
{
  byte i, Checksum;
  i=0;
  Checksum=0;
  Serial.print(Header,HEX);
  Serial.print((_Torque<<5)|0x1f,HEX);
  Serial.print(_LastID+1,HEX);
  for (i=0; i<=3; i++)
  {
    Serial.print(*(_PosPtr+i),DEC);
    Checksum ^=*(_PosPtr+i);
  }

  Checksum &=0x7f;
  Serial.print(Checksum,HEX);
}
« Last Edit: February 07, 2009, 01:48:28 pm by mem » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 40
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hmm..may i know which #define did i clash with?
Logged

Austin, TX USA
Offline Offline
God Member
*****
Karma: 4
Posts: 997
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

When you use

Code:
#define LastID 11

and then write

Code:
void SyncPosSend(byte LastID, byte Torque ,byte *PosPtr)

the compiler turns that into

Code:
void SyncPosSend(byte 11, byte Torque ,byte *PosPtr) // whoops!

Similarly with "Torque"

Mikal
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 40
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

oh

thanks brother!
Logged

Pages: [1] 2   Go Up
Jump to: