added support for interactive mode in SURVEYOR_HOST

added neural net functions for SURVEYOR_HOST



git-svn-id: http://picoc.googlecode.com/svn/trunk@173 21eae674-98b7-11dd-bd71-f92a316d2d60
This commit is contained in:
surveyor.com 2009-03-08 23:53:19 +00:00
parent 1169e6f909
commit 0f5675271d
3 changed files with 192 additions and 72 deletions

18
picoc.c
View file

@ -46,19 +46,23 @@ int picoc(char *SourceStr)
int ix;
Initialise();
for (ix=0; ix<strlen(SourceStr); ix++) // clear out ctrl-z from XMODEM transfer
if (SourceStr[ix] == 0x1A)
SourceStr[ix] = 0x20;
printf("%s\n\r", SourceStr); // display program source
printf("=====================\n");
if (SourceStr) {
for (ix=0; ix<strlen(SourceStr); ix++) // clear out ctrl-z from XMODEM transfer
if (SourceStr[ix] == 0x1A)
SourceStr[ix] = 0x20;
printf("%s\n\r", SourceStr); // display program source
printf("=====================\n");
}
errjmp[40] = 0;
setjmp(errjmp);
if (errjmp[40]) {
printf("goodbye ...\n\r");
printf("leaving picoC\n\r");
return 1;
}
Parse("test.c", SourceStr, strlen(SourceStr), TRUE);
if (SourceStr)
Parse("test.c", SourceStr, strlen(SourceStr), TRUE);
ParseInteractive();
return 0;
}
# endif

View file

@ -349,92 +349,182 @@ void Ctan(struct ParseState *Parser, struct Value *ReturnValue, struct Value **P
void Casin(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // asin(y,hyp)
{
int y, hyp, quot, sgn, ix;
y = Param[0]->Val->Integer;
hyp = Param[1]->Val->Integer;
y = Param[0]->Val->Integer;
hyp = Param[1]->Val->Integer;
if (y > hyp)
ProgramFail(NULL, "asin(): opposite greater than hypotenuse");
if (y == 0) {
if (y == 0) {
ReturnValue->Val->Integer = 0;
return;
}
sgn = hyp * y;
if (hyp < 0)
hyp = -hyp;
if (y < 0)
y = -y;
quot = (y * 10000) / hyp;
if (quot > 9999)
quot = 9999;
for (ix=0; ix<90; ix++)
if ((quot < cosine[ix]) && (quot >= cosine[ix+1]))
break;
if (sgn < 0)
ReturnValue->Val->Integer = -(90-ix);
else
ReturnValue->Val->Integer = 90-ix;
return;
}
sgn = hyp * y;
if (hyp < 0)
hyp = -hyp;
if (y < 0)
y = -y;
quot = (y * 10000) / hyp;
if (quot > 9999)
quot = 9999;
for (ix=0; ix<90; ix++)
if ((quot < cosine[ix]) && (quot >= cosine[ix+1]))
break;
if (sgn < 0)
ReturnValue->Val->Integer = -(90-ix);
else
ReturnValue->Val->Integer = 90-ix;
}
void Cacos(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // acos(x,hyp)
{
int x, hyp, quot, sgn, ix;
x = Param[0]->Val->Integer;
hyp = Param[1]->Val->Integer;
x = Param[0]->Val->Integer;
hyp = Param[1]->Val->Integer;
if (x > hyp)
ProgramFail(NULL, "acos(): adjacent greater than hypotenuse");
if (x == 0) {
if (hyp < 0)
if (x == 0) {
if (hyp < 0)
ReturnValue->Val->Integer = -90;
else
ReturnValue->Val->Integer = 90;
return;
}
sgn = hyp * x;
if (hyp < 0)
hyp = -hyp;
if (x < 0)
x = -x;
quot = (x * 10000) / hyp;
if (quot > 9999)
quot = 9999;
for (ix=0; ix<90; ix++)
if ((quot < cosine[ix]) && (quot >= cosine[ix+1]))
break;
if (sgn < 0)
ReturnValue->Val->Integer = -ix;
else
ReturnValue->Val->Integer = ix;
return;
}
sgn = hyp * x;
if (hyp < 0)
hyp = -hyp;
if (x < 0)
x = -x;
quot = (x * 10000) / hyp;
if (quot > 9999)
quot = 9999;
for (ix=0; ix<90; ix++)
if ((quot < cosine[ix]) && (quot >= cosine[ix+1]))
break;
if (sgn < 0)
ReturnValue->Val->Integer = -ix;
else
ReturnValue->Val->Integer = ix;
}
void Catan(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // atan(y,x)
{
int x,y, angle, coeff_1, coeff_2, r;
y = Param[0]->Val->Integer;
x = Param[1]->Val->Integer;
if (x == 0) {
if (y >= 0)
ReturnValue->Val->Integer = 90;
else
ReturnValue->Val->Integer = -90;
return;
}
coeff_1 = 3141/4;
coeff_2 = coeff_1*3;
y = Param[0]->Val->Integer;
x = Param[1]->Val->Integer;
if (x == 0) {
if (y >= 0)
ReturnValue->Val->Integer = 90;
else
ReturnValue->Val->Integer = -90;
return;
}
coeff_1 = 3141/4;
coeff_2 = coeff_1*3;
if (y < 0)
y = -y;
if (x >= 0) {
r = (x - y)*1000 / (x + y);
angle = (coeff_1*1000 - coeff_1 * r);
} else {
r = (x + y)*1000 / (y - x);
angle = (coeff_2*1000 - coeff_1 * r);
}
angle = angle*57/1000000;
if (y < 0)
ReturnValue->Val->Integer = -angle;
else
ReturnValue->Val->Integer = angle;
if (x >= 0) {
r = (x - y)*1000 / (x + y);
angle = (coeff_1*1000 - coeff_1 * r);
} else {
r = (x + y)*1000 / (y - x);
angle = (coeff_2*1000 - coeff_1 * r);
}
angle = angle*57/1000000;
if (y < 0)
ReturnValue->Val->Integer = -angle;
else
ReturnValue->Val->Integer = angle;
}
void Cnnset(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) {
int ix, i1;
ix = Param[0]->Val->Integer;
if (ix > NUM_NPATTERNS)
ProgramFail(NULL, "nnset(): invalid index");
for (i1=0; i1<8; i1++)
npattern[ix*8 + i1] = (unsigned char)Param[i1+1]->Val->Integer;
}
void Cnnshow(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) {
int ix;
ix = Param[0]->Val->Integer;
if (ix > NUM_NPATTERNS)
ProgramFail(NULL, "nnshow(): invalid index");
nndisplay(ix);
}
void Cnninit(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) {
nninit_network();
}
void Cnntrain(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) {
int ix, i1;
nntrain_network(10000);
for (ix=0; ix<NUM_NPATTERNS; ix++) {
nnset_pattern(ix);
nncalculate_network();
for (i1=0; i1<NUM_OUTPUT; i1++)
printf(" %3d", N_OUT(i1)/10);
printf("\n\r");
}
}
void Cnntest(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) {
int ix, i1, i2;
unsigned char ch;
ix = 0;
for (i1=0; i1<8; i1++) {
ch = (unsigned char)Param[i1]->Val->Integer;
for (i2=0; i2<8; i2++) {
if (ch & nmask[i2])
N_IN(ix++) = 1024;
else
N_IN(ix++) = 0;
}
}
nncalculate_network();
for (i1=0; i1<NUM_OUTPUT; i1++)
printf(" %3d", N_OUT(i1)/10);
printf("\n\r");
}
void Cnnmatchblob(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) {
int ix, i1;
ix = Param[0]->Val->Integer;
if (ix > MAX_BLOBS)
ProgramFail(NULL, "nnmatchblob(): invalid blob index");
if (!blobcnt[ix])
ProgramFail(NULL, "nnmatchblob(): not a valid blob");
/* use data still in blob_buf[] (FRAME_BUF3)
square the aspect ratio of x1, x2, y1, y2
then subsample blob pixels to populate N_IN(0:63) with 0:1024 values
then nncalculate_network() and display the N_OUT() results */
nnscale8x8((unsigned char *)FRAME_BUF3, blobix[ix], blobx1[ix], blobx2[ix],
bloby1[ix], bloby2[ix], imgWidth, imgHeight);
nncalculate_network();
for (i1=0; i1<NUM_OUTPUT; i1++)
printf(" %3d", N_OUT(i1)/10);
printf("\n\r");
}
void Cnnlearnblob (struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) {
int ix;
ix = Param[0]->Val->Integer;
if (ix > NUM_NPATTERNS)
ProgramFail(NULL, "nnlearnblob(): invalid index");
if (!blobcnt[0])
ProgramFail(NULL, "nnlearnblob(): no blob to grab");
nnscale8x8((unsigned char *)FRAME_BUF3, blobix[0], blobx1[0], blobx2[0],
bloby1[0], bloby2[0], imgWidth, imgHeight);
nnpack8x8(ix);
nndisplay(ix);
}
/* list of all library functions and their prototypes */
struct LibraryFunction PlatformLibrary[] =
{
@ -458,7 +548,7 @@ struct LibraryFunction PlatformLibrary[] =
{ Cvpix, "void vpix(int, int)" },
{ Cvmean, "void vmean()" },
{ Cvblob, "int vblob(int, int)" },
{ Ccompass, "int compass(int)" },
{ Ccompass, "int compass()" },
{ Creadi2c, "int readi2c(int, int)" },
{ Creadi2c2, "int readi2c2(int, int)" },
{ Cwritei2c, "void writei2c(int, int, int)" },
@ -468,6 +558,13 @@ struct LibraryFunction PlatformLibrary[] =
{ Casin, "int asin(int, int)" },
{ Cacos, "int acos(int, int)" },
{ Catan, "int atan(int, int)" },
{ Cnnshow, "void nnshow(int)" },
{ Cnnset, "void nnset(int, int, int, int, int, int, int, int, int)" },
{ Cnninit, "void nninit()" },
{ Cnntrain, "void nntrain()" },
{ Cnntest, "void nntest(int, int, int, int, int, int, int, int)" },
{ Cnnmatchblob, "void nnmatchblob(int)" },
{ Cnnlearnblob, "void nnlearnblob(int)" },
{ NULL, NULL }
};
#endif

View file

@ -68,6 +68,25 @@ void PlatformExit()
/* get a line of interactive input */
char *PlatformGetLine(char *Buf, int MaxLen)
{
int ix;
char ch, *cp;
ix = 0;
cp = Buf;
while (ix++ < MaxLen) {
ch = getch();
if (ch == 0x1B) { // ESC character - exit
printf("leaving picoC\n\r");
return NULL;
}
if (ch == '\n') {
*cp++ = '\n'; // if newline, send newline character followed by null
*cp = 0;
return Buf;
}
*cp++ = ch;
ix++;
}
return NULL;
}