SimpleIDE C coding problem

A brief overview, I am using a QuickStart board as a middleman between the Roomba robot and a Raspberry Pi. The problem that I am having is that when I use the CRvolts() for the first time, it shows the correct value. As soon as I use the CRcurrent_val(), followed by a CRvolts() again, now the CRvolts() is showing an incorrect value. Some how the usage of CRcurrent_val() is corrupting the CRvolts() value? On the Raspberry Pi side I am using a Python script program to talk to the QuickStart board.

I am using some static defines on the variables, but I am not sure if that is the correct approach. I hope one of the C experts will see the problem and correct my coding style.

Thanks
Ray
/*
  cr2_robot.c
  
  January 5, 2019

*/
#include "simpletools.h" 
#include "simpletext.h"
#include "fdserial.h"

#define reboot() __builtin_propeller_clkset(0x80)
serial *rpi;
serial *robot;

/** roomba control **/
unsigned int highbyte,lowbyte;


static float chrgval;
static signed char currval,temp_val;

void CRstart();
void CRsafe();
void CRfull();

int CRvolts();
void CRbcharge();
void CRcurrent_val();

int main()
{
  // Add startup code here.
  rpi = fdserial_open(0, 1, 0, 115200);
  pause(250);
  robot = fdserial_open(2, 3, 0, 115200);
  pause(250);
  CRstart();  // Put robot in start mode.
  char inbuf[30];
 
  while(1)
  {
    // Add main loop code here.
    readStr(rpi,inbuf,30);
    if(!strcmp(inbuf, "crstart"))
    {
      CRstart();
    }
    else if(!strcmp(inbuf, "crsafe"))
    {
      CRsafe();
    }
    else if(!strcmp(inbuf, "crfull"))
    {
      CRfull();
    }
    else if(!strcmp(inbuf, "qsboot")) reboot();
    else if(!strcmp(inbuf, "crvolts"))
    {
      float voltval = CRvolts();
      //CRvolts();
      writeFloatPrecision(rpi,voltval,2,2);
      writeStr(rpi,"\n");
    }    
    else if(!strcmp(inbuf, "crbcharge"))
    {
      CRbcharge();
      writeFloatPrecision(rpi,chrgval,2,1);
      writeStr(rpi,"\n");
    }
    else if(!strcmp(inbuf, "crcurr"))
    {
      CRcurrent_val();
      writeDec(rpi,currval);
      writeStr(rpi,"\n");
    }
    else
    {
      writeChar(rpi,'X');
    }        
  }  
}

void CRstart()
{
  fdserial_txChar(robot,128);
}

void CRsafe()
{
  fdserial_txChar(robot,131);
}

void CRfull()
{
  fdserial_txChar(robot,132);
}

int CRvolts()
{
  float vval2 = 0;
  float voltval = 0;
  
  fdserial_txChar(robot,142);
  fdserial_txChar(robot,22);
  highbyte = fdserial_rxChar(robot);
  lowbyte = fdserial_rxChar(robot);
  
  vval2 =(highbyte << 8) + lowbyte;
  vval2 = (vval2 / 1000);
  voltval = vval2;
  return voltval;
}

void CRbcharge()
{
  float vval2;

  fdserial_txChar(robot,142);
  fdserial_txChar(robot,25);
  highbyte = fdserial_rxChar(robot);
  lowbyte = fdserial_rxChar(robot);
  
  vval2 =(highbyte << 8) + lowbyte;
  vval2 = (vval2 / 1000);
  chrgval = vval2;  
}  

void CRcurrent_val()
{
  signed char tempval;
  
  fdserial_txChar(robot,142);
  fdserial_txChar(robot,23);
  tempval = fdserial_rxChar(robot);
  currval = tempval;
}

Comments

  • For CRvolts and CRbcharge you read back 2 bytes, but for CRcurrent_val you are only reading 1 byte. Could it be that you need to read 2 bytes for that as well?
  • Thank you Dave, that was a very good catch. I totally missed the most obvious.

    Now, I am thinking about the future P2, will I be able to use a P2 configuration to replace the QuickStart board and the Raspberry Pi. Boy, would that simplify things, use one board, one language to manipulate one robot. Of course the P2 configuration would need some sort of WiFi. Since I am now using C, I would be open to using some sort of Python configuration.

    Ray
Sign In or Register to comment.