Welcome, Guest. Please Login.
Surveyor Corporation Surveyor SRV-1
Home Help Search Login

Surveyor Robotics Forum

Welcome to the user support forum for Surveyor SRV-1 robots, SRV-1 robot controllers and SVS stereo vision systems. To register for this forum, please send an email to support@surveyor.com which includes your desired forum user name, your registration email address, and a brief explanation of why you wish to join, and we will create a forum account for you.

Please note that there is a Search button in the forum toolbar for forum topics. Another effective search method for the entire surveyor.com site is to use Google, e.g. "xyz site://www.surveyor.com" where "xyz" is the search topic.



Pages: 1 2 3 
Send Topic Print
Using picoC (Read 26355 times)
admin
YaBB Administrator
*****




Posts: 3676
Using picoC
04/11/09 at 7:15am
 
We recently replaced our original "Small C" interpreter running in the SRV-1 firmware with a very powerful C interpreter called picoC - http://code.google.com/p/picoc/ - which supports a substantial subset of the full C language syntax.  
 
A few tips on using the C interpreter:
 
-  Make certain you are running the latest version of srv1.ldr firmware from  
    http://code.google.com/p/surveyor-srv1-firmware/source/checkout    or
    http://code.google.com/p/surveyor-srv1-firmware/downloads/list
 
-  A preliminary description of picoC commands is found at  
    http://www.surveyor.com/C.html  
 
-  picoC supports a "stored program" mode and an "interactive" mode.  'Q' launches the stored program mode.  '!' launches the interactive mode.  Note that when a stored program finishes running, picoC switches to interactive mode which allows you to continue accessing variables and functions that were defined by the stored program.
 
-  The stored program mode runs programs from the flash buffer located in SDRAM (addr 0x00100000).  This is the same flash buffer that's used for srv1.ldr firmware uploads.  It is possible to directly enter code into the flash buffer using the 'E' editor, but a more secure method is to use XMODEM file transfer (the same method that is used to transfer srv1.ldr firmware updates.  
 
-  After initiating an XMODEM session via the 'X' command, you need to transfer your C source code via XMODEM-CRC or XMODEM-1K protocol.  On Windows, the TeraTerm terminal program - http://www.ayera.com/teraterm/ - has very good support for XMODEM transfers.  On OS/X or Linux, we use the lrzsz transfer program - http://www.ohse.de/uwe/software/lrzsz.html .  With the rest of the SRV-1 source code, you'll find shell scripts to automate this transfer - take a look at 'send1' (for arbitrary file transfer) and 'load1' (for srv1.ldr transfer).
 
-  After the C source has been uploaded to the flash buffer, you can verify that it has been transferred by sending the 'zd' command to dump the contents of the flash buffer.  You can also save you C source to flash memory - the 'zw' command saves the flash buffer contents to flash sector #4, and the 'zr' command retrieves the flash sector #4 contents to the flash buffer.  
 
-  You aren't limited to storage/retrieval from flash sector #4.  There are 64 x 64kB flash sectors numbered 00 - 63, and the 'zW' and 'zR' commands allow you to save/retrieve the flash buffer contents to any flash sector except sectors 0 and 1 where srv1.ldr is stored.    
 
- Everything happens through the "flash buffer" - we use the same buffer for uploading new firmware, saving and retrieving data from flash memory, uploading and running C interpreter programs.
 
Here is a summary of commands for manipulating the flash buffer -  
 
X  -  for XMODEM receive
zc -  clear the flash buffer
zd  - display the contents of the flash buffer
zr -  retrieve the contents of flash sector 04
zw - write the flash buffer to flash sector 04
zR - retrieve the contents of any arbitrary flash sector 02-63
zW - write the contents of the flash buffer to any arbitrary flash sector 02-63
zC - compute the CRC-16 of flash buffer contents
E  - simple line editor which writes directly into the flash buffer
Q  - launches C interpreter to run contents of flash buffer
 
As noted above, if you use XMODEM to transfer a file, you can view the contents of the buffer using 'zd'.  If you want to save the contents, use 'zw' or 'zWxx'.  
 
-  It is somewhat easier to use the C interpreter in interactive mode with a line-based terminal such as telnet in "line mode" or nc ("netcat"), as the interpreter doesn't have an erase or backup key.  A Windows version of nc is found here -  
    http://www.surveyor.com/blackfin/nc111nt.zip
 
-  There is no "main()" function in picoC.  Take a look at the code samples in http://www.surveyor.com/C.html - any of those should be able to run as complete standalone programs, e.g.
Code:
int x, y, z;
for (x = 0; x < 2; x++)
{
  for (y = 0; y < 3; y++)
  {
    for (z = 0; z < 3; z++)
    {
	printf("%d %d %d\n", x, y, z);
    }
  }
} 


 
Questions about using the new C interpreter should be posted here.  Also, we would like to see interesting code samples posted here.  At some point, I hope we can build a repository of such samples.
Back to top
 
« Last Edit: 07/15/10 at 8:20am by admin »  

SRV-1 Development Team
Surveyor Corporation
Email WWW   IP Logged
michael
YaBB Newbies
*




Posts: 38
Re: Using the new C interpreter
Reply #1 - 04/13/09 at 5:37pm
 
It took me a little scrounging to figure out that you need to hit the ESCAPE key to exit interactive mode.  
Thanks for getting picoC integrated!
Back to top
 
 
WWW   IP Logged
mshapiro
Full Member
***




Posts: 145
Re: Using the new C interpreter
Reply #2 - 04/14/09 at 2:47pm
 
After running a stored program, is there any way from the interactive mode to rerun the program, so that data is preserved from one run to the next?  I would guess that dropping out of interactive mode with <ESC> and then restarting the program with "Q" from the main Surveyor loop would reset all data.
Back to top
 
 
  IP Logged
admin
YaBB Administrator
*****




Posts: 3676
Re: Using the new C interpreter
Reply #3 - 04/14/09 at 2:57pm
 
Once you hit ESC, you clear everything.  Create functions in your stored program code and then you can call those functions from interactive mode while using the existing stored variables and data structures.
Back to top
 
 

SRV-1 Development Team
Surveyor Corporation
Email WWW   IP Logged
mshapiro
Full Member
***




Posts: 145
Re: Using the new C interpreter
Reply #4 - 04/14/09 at 9:13pm
 
Quote from admin on 04/14/09 at 2:57pm:
Once you hit ESC, you clear everything.  Create functions in your stored program code and then you can call those functions from interactive mode while using the existing stored variables and data structures.

That should work fine.  Thanks.
Back to top
 
 
  IP Logged
mshapiro
Full Member
***




Posts: 145
Re: Using the new C interpreter
Reply #5 - 04/14/09 at 10:37pm
 
What am I doing wrong.  I haven't actually worked on my SRV1 in about a month, so I figured it was time to download the new firmware with the new picoC interpreter and see what it could do.  I checked out the svn version and the c.c file that I have is identical to the one I had on my system, dated, February 9.  It is 30893 bytes long.  I get the same file if I download the .zip file (srv-blackfin-041109a.zip).  Hold on.  I just noticed the picoc subdirectory.  I assume that is the new interpreter and the old file has just not been removed.  Is that correct?
Back to top
 
 
  IP Logged
admin
YaBB Administrator
*****




Posts: 3676
Re: Using the new C interpreter
Reply #6 - 04/15/09 at 12:22am
 
Correct - you need to ignore the old c.c.  Take a look at Makefile - it builds from the picoc subdirectory.  We need to clean out some of the old files in the code repository.
Back to top
 
 

SRV-1 Development Team
Surveyor Corporation
Email WWW   IP Logged
mshapiro
Full Member
***




Posts: 145
Re: Using the new C interpreter
Reply #7 - 04/15/09 at 9:00am
 
Thanks.
 
I had some problems with compilation, but they weren't hard to get around:
 
1) imgWidth changed from 'int' to 'unsigned int'?
2) 'silent_console' is no more?
 
I had to change the 'external int imgWidth' to 'external unsigned int imgWidth' in myfunc.c and remove the references to silent_console, entirely.
Back to top
 
 
  IP Logged
admin
YaBB Administrator
*****




Posts: 3676
Re: Using the new C interpreter
Reply #8 - 04/15/09 at 9:09am
 
Yes - we got rid of "silent console".  I don't recall the change from int to unsigned int, but it might have resulted from an effort to get rid of all compiler warnings after adding -Wall to the gcc command.
 
I just cleaned out a bunch of the old files from the svn repository.
 
One unfinished piece of business is to add direct access from picoC to the neural net data structures.  In particular, it would be nice to be able to reconfigure the neural net on-the-fly so that we're not limited to the current fixed structure of 64 inputs, 16 hidden, and 16 outputs.  I haven't decided how to approach this, but it's moving toward the top of the TODO list.
 
Back to top
 
« Last Edit: 04/15/09 at 9:22am by admin »  

SRV-1 Development Team
Surveyor Corporation
Email WWW   IP Logged
mshapiro
Full Member
***




Posts: 145
Re: Using the new C interpreter
Reply #9 - 06/08/09 at 6:57pm
 
One more question.  Is there any way to get, or set the value of a global (firmware) variable from within picoC?
Back to top
 
 
  IP Logged
admin
YaBB Administrator
*****




Posts: 3676
Re: Using the new C interpreter
Reply #10 - 06/08/09 at 8:19pm
 
You can access globals to read - in fact, you can see how access to globals is defined at the top of library_surveyor.c, though we defined the globals as read only.  I have not tried write access.  
 
By the way, I did connect the picoc neural functions so that the output neurons can be accessed.  This will be in the next code update.
Back to top
 
 

SRV-1 Development Team
Surveyor Corporation
Email WWW   IP Logged
mshapiro
Full Member
***




Posts: 145
Re: Using the new C interpreter
Reply #11 - 06/08/09 at 10:00pm
 
Quote from admin on 06/08/09 at 8:19pm:
You can access globals to read - in fact, you can see how access to globals is defined at the top of library_surveyor.c, though we defined the globals as read only.  I have not tried write access.

So I just add a line to PlatformLibraryInit() for each global variable that I want to be able to access in picoC?  And arrays are handled like scanvect is, just specify the size of the array when defining the variable type?
 
Read access is all I need for now, probably all that I need.  It's just that I wrote some routines to determine the offset from center since the camera is unlikely to be aligned perfectly straight and I want to find a line down the center to follow.  Those routines are in myfunc.c in the firmware and I will need to access the variables holding the offsets in picoC.  For that, I certainly only need read access.  Those are constants that should not change during a run.  In fact, they should not change at all unless I need to take the camera off, for some reason, and then put it back on.
 
If I need write access to globals it should probably be done in the firmware.  So where do I look to see how to set up a new function in picoC that can then call a firmware funtion?
Back to top
 
 
  IP Logged
admin
YaBB Administrator
*****




Posts: 3676
Re: Using the new C interpreter
Reply #12 - 06/08/09 at 11:01pm
 
I don't have the code in front of me at the moment,  but there is a wiki on the picoc google site that covers these extensions.  Look at library_surveyor.c for examples of linking to firmware functions.
Back to top
 
« Last Edit: 06/08/09 at 11:06pm by admin »  

SRV-1 Development Team
Surveyor Corporation
Email WWW   IP Logged
mshapiro
Full Member
***




Posts: 145
Re: Using the new C interpreter
Reply #13 - 06/13/09 at 7:43pm
 
OK.  I have successfully exposed global variables to my picoC script.  I have also added a new function to picoC.  So far, so good.  The problem is that I am not getting the results that I am expecting.  I can see what is happening and I am not sure if this is a picoC issue, or a Blackfin issue.
 
I have added the following lines to PlatformLibraryInit() in library_surveyor.c
Code:
    VariableDefinePlatformVar(NULL, "XOFF", &IntType, (union AnyValue *)&xoff, FALSE);
    VariableDefinePlatformVar(NULL, "XOFF_239", &IntType, (union AnyValue *)&xoff_239, FALSE);
    VariableDefinePlatformVar(NULL, "XOFF_119", &IntType, (union AnyValue *)&xoff_119, FALSE);
    VariableDefinePlatformVar(NULL, "XOFF_000", &IntType, (union AnyValue *)&xoff_000, FALSE); 


 
I have also added the following to library_surveyor.c
Code:
void Cstraight(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)  // straight()
{
    int x;
    x = straight();
    ReturnValue->Val->Integer = x;
} 
 


 
and this to struct LibraryFunction PlatformLibrary
Code:
    { Cstraight,    "int straight()" },
 


 
The function straight() is defined in myfunc.c
Code:
int straight(void) {
    xoff = -1;

    x1 = -1;
    x2 = -1;

    grab_frame();

    for (j=5; j<355; j++) {
        if (x1 == -1) {
            if (vpbk((unsigned char *)FRAME_BUF, j, 239)) {
                x1 = j;
            }
        }

        if (x1 != -1) {
            if (!vpbk((unsigned char *)FRAME_BUF, j, 239)) {
                x2 = j;
                break;
            }
        }
    }

    xoff = (x1 + x2)/2;

    return xoff_239 - xoff;
}
 


xoff_239 should be at, or very close to 159 (the center of the image, horizontally).
 
Here is my problem.  I am expecting to get a small number, either positive, or negative, returned from straight() as the robot veers away from the line it is following.  (I expect the number to be small because I hope to correct the robot's vector before it becomes large.)  I was getting numbers in the 200+ range, however.  That was when I exposed the raw variables to picoC so I could see what was happening.
 
When I have not moved the robot, I get a return value of "0", just as I expect.  If I move the robot slightly to the left, however, I get this
Code:
Offset from straight: 219    xoff: 196     xoff_239: 159
 


Instead of getting a result of -37, I am getting 256-37.  Are the integer values in picoC 8 bit, unsigned integers?  Is there a way to get a 16 bit signed value?  I should be able to deal with 8 bit signed, but 16 bit would be nice.
Back to top
 
 
  IP Logged
admin
YaBB Administrator
*****




Posts: 3676
Re: Using the new C interpreter
Reply #14 - 06/13/09 at 8:03pm
 
Where are xoff, x1 and x2 declared ?  Do you have "extern" statements somewhere so that they can be referenced between different code modules ?
 
Back to top
 
 

SRV-1 Development Team
Surveyor Corporation
Email WWW   IP Logged
Pages: 1 2 3 
Send Topic Print