hello,

I'm writing a program to collect the data from a sonar of a robot. the robot is running Fedora 6. I want to collect the data in an external file as there is a lot of information to be reviewed. the program is fine when I direct the sonar data to the monitor with printf. however, when I try to use the 'fputs' I get the following warning message and nothing is deposited in the file.

"passing arg 1 of 'fputs' makes pointer from integer without a cast"

the data that I'm collecting is declared as integer. perhaps there is another function, instead of 'fputs' that I should use to redirect the incoming data. I appreciate any sort of help.

================================================

#include <stdio.h>
#include <stdlib.h>
#include "Nclient.h"

/* turn on the sonar sensors */

void turn_sonar_on(void)
{
  int sn_order[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
  
  /* turn on all sonars (sn_order) and set them to fire in certain order (15*4 miliseconds = 60 miliseconds) */
  conf_sn(15, sn_order);
  
  /* configure time out of the robot (in seconds) */
  conf_tm(15); 
}


/*
 * main body of the program designated to collect the sensor data
 */
main(void)
{

  /* create a file to deposit the data to */
   FILE * pFile;
   pFile = fopen ("mylog.txt","a");
  
  /* connect to the robot */
  connect_robot(1)

  /* alternate connection to the robot */
//  connect_robot(1, MODEL_SCOUT2, "/dev/ttyS0", 38400);

  /* turn on the sonar sensors */
  turn_sonar_on();
  

  /* collect the data from the robot sensors and motion and deposit it in external file via STATE variable */
  gs();
  while(1)
  {

    /* deposit sonar data */
    short i = 0;
    do
    {
      fputs ("Sonar data:\n",pFile);
      fputs (State[STATE_SONAR_0+i],pFile);
      fputs ("end of Sonar data:\n",pFile);

      i++;
    } while (i<16);    

  }

  return(0);
}

I replaced the fputs() with fprintf() and I'm still getting the same error:

warning: passing arg 2 of 'fprintf' makes pointer from integer without a cast
fprintf (pFile,State[STATE_SONAR_0+i]);

any idea what else I could try?

your STATE variable (it seems) is an array of integers, correct?

fputs expects a pointer to a character string, and you're using fprintf wrong.

look up the definition of predefined functions in any code reference, when they dont behave like you think they should

http://www.cplusplus.com/reference/clibrary/cstdio/fprintf.html

not knowing anything about your STATE variable, or how it's scoped, i'd suggest you try something like this: fprintf(pFile, "State[%d] = %d\n",STATE_SONAR_0+i,State[STATE_SONAR_0+i]); .

thanks a million, that works exactly how I need it to.

by now I have another issue that I hope to get some answer to: the data that I'm depositing using the fprintf(), inside the do loop, is generating a tremendous amount of information. is there a way to slow down the execution execution of the loop to, say, once per a second? thanks

while(1)
  {

    /* deposit sonar data */
    short i = 0;
    do
    {
      fputs ("Sonar data:\n",pFile);
      fputs (State[STATE_SONAR_0+i],pFile);
      fputs ("end of Sonar data:\n",pFile);

      i++;
    } while (i<16);    

  }

this is a simple and imprecise and not even remotely thread-safe, but it should do for you for now.

void delay(int seconds)
{        
    time_t start_time, cur_time;

    time(&start_time);
    cur_time = start_time;

    // delay will rarely ever be the full amount specified.
    // specifically:    (seconds-1) < delay <= (seconds)

    while((cur_time - start_time) < seconds);
        time(&cur_time);      
}

usage: #include <time.h> ... delay(3); // the average delay tends towards 2.5 seconds.

when I included the call to delay() inside the function main() with the fprintf(), my "mylog.txt" did not get any information sent to it (when I re-wrote the function main() with the printf() instead of the fprintf(), I got a single run of the while(1) loop). it looks as if the delay() function does not want to return the execution of the program once it starts to execute. any idea of how I can fix that?

thanks.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "Nclient.h"

void turn_front_sonar_on(void)
{
  int sn_order[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
  
  conf_sn(500, sn_order);
  
  conf_tm(5); 
}

void delay(int seconds)
{
 time_t start_time, cur_time;
 time(&start_time);
 cur_time = start_time;

  while((cur_time - start_time) < seconds);
   time(&cur_time);
}

/*
 * main
 */
main(void)
{
  FILE *pFile;
  int i;
  connect_robot(1, MODEL_SCOUT2, "/dev/ttyS0", 38400);

  pFile = fopen ("mylog.txt","a");  

  /* turn on the front sonars */
  turn_front_sonar_on();

  /* the loop */
  gs();
 
  while(1)
  {
    i = 0;

    do
    {
      fprintf(pFile,"Sonar data :");
      fprintf(pFile,"i[%d] = ",i);
      fprintf(pFile,"State[%d]\n",State[STATE_SONAR_0+i]);
      i++;
    } while (i < 16);
  delay(3);
  }
  disconnect_robot(1);
  return(0);
}

ack.

i put a semicolon after the "while" statement in line #11 of my function.

sorry, i dont know how it got there. but it put you in an infinite loop. remove it and it will work as expected.

it will also be technically more correct to declare the argument to the "delay" function as time_t void delay(time_t seconds) although it will work just fine as an INT... unless your delay time requirements exceed 68 years.

This article has been dead for over six months. Start a new discussion instead.