View unanswered posts | View active topics It is currently Tue Sep 02, 2014 2:51 pm






Reply to topic  [ 9 posts ] 
Attempting a work-around for Array Functions Issue 
Author Message
Rookie
User avatar

Joined: Sun Jan 02, 2011 1:57 am
Posts: 31
Post Attempting a work-around for Array Functions Issue
Hello all,

I have so far been attempting to create a function that will allow me to read values from 4 ultrasound sensors attached to a HiTechnic Sensor Multiplexer, place these values into a 4-item array, and then read this array. So far, I have run into the array in functions problem and I have attempted a few work arounds to this issue that havent quite worked out.

Here is the code:
Code:
#pragma config stuffs.... this takes up a lot of room so i deleted it but its not the issue

// HiTechnic Gyroscopic Sensor Driver
#include "../#include/HTGYRO-driver.h"

// LEGO Ultrasound Sensor driver for use with sensor multiplexer
#include "../#include/LEGOUS-driver.h"

void initializeRobot(){                                                           // Set value of motor encoder for linear motor to 0
  HTSMUXinit();                                                                   // Initialize driver for Sensor Multiplexer                                                         
  HTSMUXscanPorts(HTSMUX);                                               // Scan ports of Sensor Multiplexer
  return;                                                                             // Continue with program
}


//1st attempt at getting this to work, this gives me one value for distArray[0] but its actually the value for msensor_s3_4??? and everything else is 0 which makes no sense at all.

int *readDist(){
  int distArray[4];
  distArray[0] = USreadDist(msensor_S3_1);
  distArray[1] = USreadDist(msensor_S3_2);
  distArray[2] = USreadDist(msensor_S3_3);
  distArray[3] = USreadDist(msensor_S3_4);
  return distArray;
}

//2nd Attempt: this gives me valuese for all 4 sensors but the values are all for msensor_s3_1 except that the 3rd value is the dist +1 and the 4th value is the dist +2.... so if the distance is 7 you get {7,7,8,9}

void readDist2(int *distPtr){
  distPtr = USreadDist(msensor_S3_1);
  distPtr++;
  distPtr = USreadDist(msensor_S3_2);
  distPtr++;
  distPtr = USreadDist(msensor_S3_3);
  distPtr++;
  distPtr = USreadDist(msensor_S3_4);
}

#define MODE 2

task main () {

  int distArray[4];
  initializeRobot();
  while(true) {
    if(MODE == 2)
    {
// This method works fine....
      int distFront = USreadDist(msensor_S3_1);
      int distRight = USreadDist(msensor_S3_2);
      int distBack = USreadDist(msensor_S3_3);
      int distLeft = USreadDist(msensor_S3_4);
      nxtDisplayTextLine(3, "Dist Front:  %3d cm", distFront);
      nxtDisplayTextLine(4, "Dist Right:  %3d cm", distRight);
      nxtDisplayTextLine(5, "Dist Back:  %3d cm", distBack);
      nxtDisplayTextLine(6, "Dist Left:  %3d cm", distLeft);
    }else if(MODE == 1)
    {
 // This method give me the {7,7,8,9}
      int *distPtr=readDist();
      nxtDisplayTextLine(3, "Dist Front:  %3d cm", distPtr);
      nxtDisplayTextLine(4, "Dist Right:  %3d cm", distPtr++);
      nxtDisplayTextLine(5, "Dist Back:  %3d cm", distPtr++);
      nxtDisplayTextLine(6, "Dist Left:  %3d cm", distPtr++);
    }else if(MODE == 0)
    {
//and this method gives me only 1 value
      readDist2(distArray);
      nxtDisplayTextLine(3, "Dist Front:  %3d cm", distArray[0]);
      nxtDisplayTextLine(4, "Dist Right:  %3d cm", distArray[1]);
      nxtDisplayTextLine(5, "Dist Back:  %3d cm", distArray[2]);
      nxtDisplayTextLine(6, "Dist Left:  %3d cm", distArray[3]);
    }
  }
}


now i know that the first attempt is getting the right values and writing them to the array correctly but I seem to be reading them wrong or something idk. Any thoughts?

Thanks in advance for your help!

_________________
Karan Hiremath
FTC Team 110- MFS Foxes
Co-Captain
Head Programmer
Builder
Electrical
Service Coordinator


Sun Jan 02, 2011 2:12 am
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Attempting a work-around for Array Functions Issue
Your first attempt won't work even in ANSI C because you declared the array inside a function and attempted to return a pointer of the local array in the function. As soon as the code exited the function, the local array is out of scope and would be destroyed. So it won't work.
For your 2nd attempt, RobotC does not support pointers. So you cannot use the "int *" syntax as the parameter to a function.
I am surprise both of your attempts even compiled without error! In any case, if you want to pass an array to a function you need to define it in a structure and pass the structure by reference to the function. Something like this:
Code:
typedef struct _USArray
{
    int USValues[4];
} US_ARRAY;

void ReadSensors(US_ARRAY &array)
{
    array.USValues[0] = USreadDist(msensor_S3_1);
    array.USValues[1] = USreadDist(msensor_S3_2);
    array.USValues[2] = USreadDist(msensor_S3_3);
    array.USValues[3] = USreadDIst(msensor_S3_4);
}

task main()
{
    US_ARRAY USSensors;

    ReadSensors(USSensors);
}


Sun Jan 02, 2011 5:00 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3210
Location: Rotterdam, The Netherlands
Post Re: Attempting a work-around for Array Functions Issue
Actually, there are some serious typos in the driver as well.

I was going to suggest the following code:
Code:
#pragma config(Sensor, S1,     HTSMUX,              sensorI2CCustom9V)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

#include "drivers/common.h"

// HiTechnic Gyroscopic Sensor Driver
#include "drivers/HTGYRO-driver.h"

// LEGO Ultrasound Sensor driver for use with sensor multiplexer
#include "drivers/LEGOUS-driver.h"

void initializeRobot(){                                                           // Set value of motor encoder for linear motor to 0
  HTSMUXinit();                                                                   // Initialize driver for Sensor Multiplexer
  HTSMUXscanPorts(HTSMUX);                                               // Scan ports of Sensor Multiplexer
  return;                                                                             // Continue with program
}

typedef int tDistArr[4];

//1st attempt at getting this to work, this gives me one value for distArray[0] but its actually the value for msensor_s3_4??? and everything else is 0 which makes no sense at all.

void readDist(tDistArr &distarr){
  distarr[0] = USreadDist(msensor_S3_1);
  distarr[1] = USreadDist(msensor_S3_2);
  distarr[2] = USreadDist(msensor_S3_3);
  distarr[3] = USreadDist(msensor_S3_4);
}

task main () {
  tDistArr distArray;
  initializeRobot();
  while(true) {
    readDist(distArray);
    nxtDisplayTextLine(3, "Dist Front:  %3d cm", distArray[0]);
    nxtDisplayTextLine(4, "Dist Right:  %3d cm", distArray[1]);
    nxtDisplayTextLine(5, "Dist Back:  %3d cm", distArray[2]);
    nxtDisplayTextLine(6, "Dist Left:  %3d cm", distArray[3]);
  }
}


I'll make a fixed driver available later today.

Next time when pasting your program, do not leave out the pragmas. Let the reviewer decide which parts of your code are relevant :)

- Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Sun Jan 02, 2011 5:17 am
Profile WWW
Rookie
User avatar

Joined: Sun Jan 02, 2011 1:57 am
Posts: 31
Post Re: Attempting a work-around for Array Functions Issue
Ok thanks to both of you I will try both

I found an earlier post by you mightor and I was working on a structure workaround but i didnt get very far so thanks for the help.

Sorry mightor I will remember to leave those in next time!

_________________
Karan Hiremath
FTC Team 110- MFS Foxes
Co-Captain
Head Programmer
Builder
Electrical
Service Coordinator


Sun Jan 02, 2011 7:09 pm
Profile
Rookie

Joined: Thu Sep 13, 2012 9:43 am
Posts: 47
Post Re: Attempting a work-around for Array Functions Issue
Thanks Xander, I was struggling with passing arrays to functions, not now!


Mon Dec 17, 2012 7:30 am
Profile
Novice

Joined: Sun Oct 21, 2012 10:01 pm
Posts: 76
Post Re: Attempting a work-around for Array Functions Issue
MikeJMcFarlane wrote:
Thanks Xander, I was struggling with passing arrays to functions, not now!

You can also pass an array of ints to a function as a pointer to an int. The name of the array is actually a pointer to the first item of the array, so using the [ ] operators on that pointer is legal.

For example:

Code:
int array[5] = {0, 1, 2, 3, 4};

void myFunction(int * arrayParm)
{
   nxtDisplayTextLine(0, "%d", arrayParm[0]);
   nxtDisplayTextLine(1, "%d", arrayParm[1]);
   nxtDisplayTextLine(2, "%d", arrayParm[2]);
   nxtDisplayTextLine(3, "%d", arrayParm[3]);
   nxtDisplayTextLine(4, "%d", arrayParm[4]);
}

task main()
{
   myFunction(array);
   while (true);
}



If I'm not mistaken, the a[b] operation is equivalent to *(a + b). In other words, item b of array a is the same as dereferencing the pointer at a + b. Pointer arithmetic dictates that adding a number b to a pointer returns a new pointer that's b items "later."


Mon Dec 17, 2012 3:28 pm
Profile
Rookie

Joined: Thu Sep 13, 2012 9:43 am
Posts: 47
Post Re: Attempting a work-around for Array Functions Issue
amcerbu wrote:
MikeJMcFarlane wrote:
Thanks Xander, I was struggling with passing arrays to functions, not now!

You can also pass an array of ints to a function as a pointer to an int. The name of the array is actually a pointer to the first item of the array, so using the [ ] operators on that pointer is legal.

For example:

Code:
int array[5] = {0, 1, 2, 3, 4};

void myFunction(int * arrayParm)
{
   nxtDisplayTextLine(0, "%d", arrayParm[0]);
   nxtDisplayTextLine(1, "%d", arrayParm[1]);
   nxtDisplayTextLine(2, "%d", arrayParm[2]);
   nxtDisplayTextLine(3, "%d", arrayParm[3]);
   nxtDisplayTextLine(4, "%d", arrayParm[4]);
}

task main()
{
   myFunction(array);
   while (true);
}



If I'm not mistaken, the a[b] operation is equivalent to *(a + b). In other words, item b of array a is the same as dereferencing the pointer at a + b. Pointer arithmetic dictates that adding a number b to a pointer returns a new pointer that's b items "later."


Thanks amcerbu. Is it meant to be 'int * arrayParm' or 'int *arrayParm'? ie a space between the asterisk and the array name.

I think part of the problem was I had declared the array within main() so it was a local array (if that is the right term).


Tue Dec 18, 2012 4:53 am
Profile
Novice

Joined: Sun Oct 21, 2012 10:01 pm
Posts: 76
Post Re: Attempting a work-around for Array Functions Issue
In RobotC (and all C-based languages, as far as I know), whitespace doesn't matter. That's why a+b and a + b are equivalent. The space is purely cosmetic, although it does make the code more readable. You may see it either way.

You can still pass a local array to a function, that probably wasn't the problem.

Anyway, hope that helps! Pointers are really cool (especially void pointers).


Tue Dec 18, 2012 3:30 pm
Profile
Rookie

Joined: Thu Sep 13, 2012 9:43 am
Posts: 47
Post Re: Attempting a work-around for Array Functions Issue
amcerbu wrote:
In RobotC (and all C-based languages, as far as I know), whitespace doesn't matter. That's why a+b and a + b are equivalent. The space is purely cosmetic, although it does make the code more readable. You may see it either way.

You can still pass a local array to a function, that probably wasn't the problem.

Anyway, hope that helps! Pointers are really cool (especially void pointers).


OK, I didn't know that, useful thanks. I've only just started to get my head round the benefits of pointers, they are pretty cool for some things:-)


Wed Dec 19, 2012 4:52 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 9 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.