Patrick222122
Posts: 80
Joined: Sun Dec 02, 2012 5:51 pm

Raspberry pi communicating with the Parrallax Propellor

Tue Feb 26, 2013 11:15 pm

I need some help with this code to send commands down a usb cable from my pi to a propelled quick start board. (If you are wondering I this code is from a discussion on Parallax's forum that I modified)

Code: Select all

CON
  _CLKMODE      = XTAL1 + PLL16X
  _XINFREQ      = 5_000_000
 ServoCh1 = 3
ServoCh2 = 2         
input_pin=0  
input_pin2=1 
OBJ
SERVO : "Servo32v7.spin"
serial: "FullDuplexSerial"     
VAR
byte input
byte output
pub go
serial.start(31, 30, 0, 230400) '57600 ' 115200) ' 230400)

SERVO.Start                 'Start Servo handler
SERVO.Ramp  '<-OPTIONAL     'Start Background Ramping
  dira [input_pin]~
  dira [input_pin2]~
  input:="9"
repeat
 if ina[input_pin]==0
          input:="1" 
 elseif ina[input_pin2]==0
        input:="2"    
 elseif ina[input_pin]==0 AND ina[input_pin2]==0
        input:="3"    
 elseif ina[input_pin]==1 AND ina[input_pin2]==1 
         input:="4" 

 serial.tx(input) 

 output:=serial.rx
 if strcomp(output,"5")
   outa[23]:=0  
 elseif strcomp(output,"6")
   outa[21]:=0  
 elseif strcomp(output,"7")
  outa[22]:=0  
 elseif strcomp(output,"8")
  outa[23]:=0
 else
  forward
  
  
pri forward
SERVO.SetRamp(ServoCh1,2000,1)           'Pan Servo
SERVO.SetRamp(ServoCh2,1000,1)
pri turnleft
SERVO.SetRamp(ServoCh1,1000,1)           'Pan Servo
SERVO.SetRamp(ServoCh2,2000,1)
  waitcnt(125_000_000+cnt)
SERVO.SetRamp(ServoCh1,2000,1)           'Pan Servo
SERVO.SetRamp(ServoCh2,2000,1)
waitcnt(125_000_000+cnt)
return 
pri turnright
 SERVO.SetRamp(ServoCh1,1000,1)           'Pan Servo
SERVO.SetRamp(ServoCh2,2000,1)
  waitcnt(125_000_000+cnt)
SERVO.SetRamp(ServoCh1,1000,1)           'Pan Servo
SERVO.SetRamp(ServoCh2,1000,1)
waitcnt(125_000_000+cnt)
SERVO.SetRamp(ServoCh2,1000,1)
return
pri reverse
   SERVO.SetRamp(ServoCh1,2000,1)
 SERVO.SetRamp(ServoCh1,1000,1)
waitcnt(125_000_000+cnt)
SERVO.SetRamp(ServoCh2,2000,1)           'Pan Servo
SERVO.SetRamp(ServoCh2,1000,1)           'Pan Servo
waitcnt(250_000_000+cnt)
return
I will be posting the pi code in the next post.

Patrick222122
Posts: 80
Joined: Sun Dec 02, 2012 5:51 pm

Re: Raspberry pi communicating with the Parrallax Propellor

Tue Feb 26, 2013 11:22 pm

And here is the pi code

Code: Select all

#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <termios.h>
#include <sys/fcntl.h>
char process(char input);
int main(int argc,char* argv[])
{
struct termios termOptions;
char port [1024];
int ttyFid;

if(argc==2)
{
strcpy(port,argv[1]);
printf("Read port name: %s\n",port);
}
else
{
printf("Usage:setSer /dev/ttyUSB0\n");
return -1;
}

ttyFid = open("/dev/ttyUSB0",O_RDWR);
if(ttyFid==-1)
{
printf("Error unable to open port!\n");
return-1;
}

tcgetattr(ttyFid,&termOptions);

cfsetispeed(&termOptions,B230400);//57600//B115200);//B230400);
cfsetospeed(&termOptions,B230400);//57600//B115200);//B230400);

tcsetattr(ttyFid,TCSANOW,&termOptions);

char ch='9';
int n=0;
while(1)
{
write(ttyFid,&ch,sizeof(ch));
read(ttyFid,&ch,sizeof(ch));
ch=process(ch);
printf("%s\n",ch);
}

close(ttyFid);
}
char process(char input){
if (input=='4'){
return '5';}
else if(input=='3'){
return '8';}
else if(input=='1'){
return '6';}
if(input=='2'){
return '7';
}
}

I am pretty sure the problem is so obvious I completely overlooked it.

User avatar
joan
Posts: 14986
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Raspberry pi communicating with the Parrallax Propellor

Tue Feb 26, 2013 11:30 pm

I'm not sure if the problem is at the parallax or Pi. Are there any symptoms which show the problem?

Patrick222122
Posts: 80
Joined: Sun Dec 02, 2012 5:51 pm

Re: Raspberry pi communicating with the Parrallax Propellor

Tue Feb 26, 2013 11:45 pm

no problems per say but as shown in the pi program the variable ch is supose to be displayed in the terminal and it is not. One of the intigrated led, the one connected to pin 23 on the quickstart board is suppose to turn on and it does not.

Patrick222122
Posts: 80
Joined: Sun Dec 02, 2012 5:51 pm

Re: Raspberry pi communicating with the Parrallax Propellor

Wed Feb 27, 2013 2:44 am

OH, I should clarify one more thing, I have yet to wire the components to the quick start board.

Patrick222122
Posts: 80
Joined: Sun Dec 02, 2012 5:51 pm

Re: Raspberry pi communicating with the Parrallax Propellor

Sat Mar 02, 2013 10:35 pm

update! made the following change to the pi code

Code: Select all

#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <termios.h>
#include <sys/fcntl.h>
char process(char input);
int main(int argc,char* argv[])
{
struct termios termOptions;
char port [1024];
int ttyFid;

if(argc==2)
{
strcpy(port,argv[1]);
printf("Read port name: %s\n",port);
}
else
{
printf("Usage:setSer /dev/ttyUSB0\n");
return -1;
}

ttyFid = open("/dev/ttyUSB0",O_RDWR);
if(ttyFid==-1)
{
printf("Error unable to open port!\n");
return-1;
}

tcgetattr(ttyFid,&termOptions);

cfsetispeed(&termOptions,B230400);//57600//B115200);//B230400);
cfsetospeed(&termOptions,B230400);//57600//B115200);//B230400);

tcsetattr(ttyFid,TCSANOW,&termOptions);

char ch='9';
int n=0;
while(1)
{
printf("%s\n",ch);/*the change is here*/
write(ttyFid,&ch,sizeof(ch));
read(ttyFid,&ch,sizeof(ch));
ch=process(ch);
}

close(ttyFid);
}
char process(char input){
if (input=='4'){
return '5';}
else if(input=='3'){
return '8';}
else if(input=='1'){
return '6';}
if(input=='2'){
return '7';
}
}





now the code is giving me a segmentation fault error when the code runs, which before this only happened when I terminated the code by unplugging the propellor. Does this help at all.

Patrick222122
Posts: 80
Joined: Sun Dec 02, 2012 5:51 pm

Re: Raspberry pi communicating with the Parrallax Propellor

Sat Mar 02, 2013 11:50 pm

I think I found the source of the problem. Take a look at this new version of the code

Code: Select all

#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <termios.h>
#include <sys/fcntl.h>
void process(char input);

int main(int argc,char* argv[])
{
struct termios termOptions;
char port [1024];
int ttyFid;

if(argc==2)
{
strcpy(port,argv[1]);
printf("Read port name: %s\n",port);
}
else
{
printf("Usage:setSer /dev/ttyUSB0\n");
return -1;
}

ttyFid = open("/dev/ttyUSB0",O_RDWR);
if(ttyFid==-1)
{
printf("Error unable to open port!\n");
return-1;
}

tcgetattr(ttyFid,&termOptions);

cfsetispeed(&termOptions,B230400);//57600//B115200);//B230400);
cfsetospeed(&termOptions,B230400);//57600//B115200);//B230400);

tcsetattr(ttyFid,TCSANOW,&termOptions);
printf("starting sending routine");/*the program stops working before this point*/
char ch='9';
int n=0;
while(n<=500)
{

write(ttyFid,&ch,sizeof(ch));
read(ttyFid,&ch,sizeof(ch));
process(ch);
printf("%s\n",ch);/*the change is here*/
n++;
}

close(ttyFid);
}
void process(char input){
if (input=='4'){
ch='5';}
else if(input=='3'){
ch='8';}
else if(input=='1'){
ch='6';}
if(input=='2'){
ch='7';
}
}
As you can plainly see I added a new print command to the code before the sending/processing/reciving loop. When I tested this code the string did not appear. Ergo the problem exist before that point. I am also including bellow the original demo program from which the program is derived and known to works.

Code: Select all

#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <termios.h>
#include <sys/fcntl.h>

int main(int argc,char* argv[])
{
struct termios termOptions;
char port [1024];
int ttyFid;

if(argc==2)
{
strcpy(port,argv[1]);
printf("Read port name: %s\n",port);
}
else
{
printf("Usage:setSer /dev/ttyUSB0\n");
return -1;
}

ttyFid = open("/dev/ttyUSB0",O_RDWR);
if(ttyFid==-1)
{
printf("Error unable to open port!\n");
return-1;
}

tcgetattr(ttyFid,&termOptions);

cfsetispeed(&termOptions,B230400);//57600//B115200);//B230400);
cfsetospeed(&termOptions,B230400);//57600//B115200);//B230400);

tcsetattr(ttyFid,TCSANOW,&termOptions);

char ch;
int n=0;
while(1)
{
read(ttyFid,&ch,sizeof(ch));
write(ttyFid,&ch,sizeof(ch));
if(n%10000==0)
printf("%d\n",n);
n++;
}

close(ttyFid);
}
I'll try to give you a better idea of where the problem is in future posts

Patrick222122
Posts: 80
Joined: Sun Dec 02, 2012 5:51 pm

Re: Raspberry pi communicating with the Parrallax Propellor

Thu Mar 07, 2013 10:19 pm

I got the pit to send data down the usb cable but unfortuantly the data is not being received or at least proccessed. THe propellor code has not changed from earlier post. What do you think?

Return to “Automation, sensing and robotics”