need help in codes!

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() {

}

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

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!

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);
}

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

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

these codes are from the servo manual.

/*------------------ 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

/***********************************************************************/
/* 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?

/*************************************************************************************************/
/* 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?

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:

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:

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

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:

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;
}
  1. The way I read it (do you have a link to the servo controller manual?), GetByte returns one byte for each call.

  2. 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…

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

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

im back with new problem

#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 :

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

can somebody enlightened me?

thanks in advance

im back with new problem

#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 :

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

can somebody enlightened me?

thanks in advance

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

#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);
}

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

When you use

#define LastID 11

and then write

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

the compiler turns that into

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

Similarly with "Torque"

Mikal

oh

thanks brother!

[#include <math.h>
#define Header 0xff  


byte *PosPtr;
byte ZeroPosArray[]={/* ID 0 ,  1 ,  2 ,  3 ,  4 ,  5 ,  6 ,  7 ,  8 ,   9,  10 ,  11 */
                          10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 , 100, 111 , 120
                    };                                                                       //initial positions.
                    
byte WalkPosArray[5][12]={/* ID 0 ,  1 ,  2 ,  3 ,  4 ,  5 ,  6 ,  7 ,  8 ,   9,  10 ,  11 */
                              {10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 , 100, 110 , 120}, //Index:0 -ViaPoint_0
                              {20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 ,100 , 110, 120 , 130}, //Index:1 -ViaPoint_1
                              {30 , 40 , 50 , 60 , 70 , 80 , 90 ,100 ,110 , 120, 130 , 140}, //Index:0 -ViaPoint_2
                              {40 , 50 , 60 , 70 , 80 , 90 ,100 ,110 ,120 , 130, 140 , 150}, //Index:0 -ViaPoint_3
                              {50 , 60 , 70 , 80 , 90 ,100 ,110 ,120 ,130 , 140, 150 , 160}  //Index:4 -ViaPoint_0
                         }; 
void setup()
  {
    Serial.begin(115200);
    byte LastID, Torque;
    LastID=11; 
    Torque=4; //0~4
    PosPtr=ZeroPosArray;
    SyncPosSend(LastID,Torque,PosPtr);//initialise
    
    
      
  }
  
void loop()
{ 
}
void SyncPosSend(byte LastID, byte Torque ,byte *PosPtr)
{
  byte i, Checksum;
  i=0;
  Checksum=0;
  Serial.print(Header,BYTE);
  Serial.print((Torque<<5)|0x1f,BYTE);
  Serial.print(LastID+1,BYTE);
    for(i=0; i<=LastID; i++)
      {
        Serial.print(*(PosPtr+i),DEC);
        Checksum ^=*(PosPtr+i);
      }
      
  Checksum &=0x7f;
  Serial.print(Checksum,BYTE);
  
}

/code]

i  have added a 2dArray and is intended to access it after acessed zeroposarray[], so how am i going to access the multi array if i'm going to use the SyncPosSend() function to access it too?please enlighten me.

My question on C…say…i have a 2d array of [3][4]…how can i use the values in this array and dump into a function whereby it will calculate the average value in [0][1] and [0][2] and put back into a new 2d array?