BasicMicro - Forums

www.basicmicro.com
It is currently Sat Feb 04, 2012 6:57 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Programming ARC32 with Matlab
PostPosted: Tue Aug 03, 2010 7:27 pm 
Offline
Citizen

Joined: Tue Aug 03, 2010 7:14 pm
Posts: 7
Hey,

I would like to program my ARC32 using Matlab but I'm not sure how. I was able to create a serial object and I could open it, but I'm not sure what to send to the device.

The code I used is:
s1 = serial('COM7')
fopen(s1)

I was wondering if there is any specific commands to send information to the individual ports on the microcontroller. For now all I want to do is control the position of servo motors. Any sample Matlab code would be appreciated. Links to relevant information would also be good.

Thanks


Top
 Profile  
 
 Post subject: Re: Programming ARC32 with Matlab
PostPosted: Wed Aug 04, 2010 9:52 am 
Offline
Site Admin
User avatar

Joined: Thu Mar 01, 2001 11:00 am
Posts: 784
Location: Temecula, CA
Thats a pretty wide open request. The ARC32 does not come with code loaded for any particular purpose. You can program any thing you want to it. For exmaple I'm making an SSC-32 compatible program for the ARC-32. Then you could talk to it(the ARC-32) from a PC using the same commands as used on an SSC-32.

Until you load some program ot the ARC-32 it won't do anything. You will need to download the BasicMicro Studio software and install it. Then you can either use my SSC-32 command system program and download it(it should be available in another day or so). or you can write your own program to do specifically what you'd like Matlab to do.

If you would provide more information on exactly what you want to do in Matlab and what outputs you need to happen on the ARC-32 would be a starting point.

_________________
Nathan Scherdin
Basic Micro - Robotic Technology Evolved


Top
 Profile  
 
 Post subject: Re: Programming ARC32 with Matlab
PostPosted: Wed Aug 04, 2010 3:53 pm 
Offline
Citizen

Joined: Tue Aug 03, 2010 7:14 pm
Posts: 7
I'm very new to programming with Microcontrollers so I wasn't totally sure how to convey what I'm looking for. I have the basic micro studio program and I've been able to send programs to the microcontroller but I have no idea how to write a program which would allow for inputs through Matlab.

I need a program that would allow me to send positions through Matlab to the controller. It sounds like the program you are working on would be perfect. I was reading another thread where they were discussing sending SSC-32 commands with Matlab. I guess I could work off of that for sending exact commands.

Could you send me link to a list of SSC-32 commands?

Thank you so much.


Top
 Profile  
 
 Post subject: Re: Programming ARC32 with Matlab
PostPosted: Thu Aug 05, 2010 8:57 am 
Offline
Site Admin
User avatar

Joined: Thu Mar 01, 2001 11:00 am
Posts: 784
Location: Temecula, CA
As soon as I've finished the SSC emulator code for ARC-32 I will post a link to it in the News section. Its pretty complicated and has taken a little longer than I expected. I hope to have something postable by tonight, but I may not get to that point until tomorrow.

_________________
Nathan Scherdin
Basic Micro - Robotic Technology Evolved


Top
 Profile  
 
 Post subject: Re: Programming ARC32 with Matlab
PostPosted: Mon Aug 23, 2010 4:08 pm 
Offline
Citizen

Joined: Tue Aug 03, 2010 7:14 pm
Posts: 7
Hey,

I've been trying to make a simple program based on some research that I've been doing online. I've run into a couple of problems though.

How does the hserin command work? What values are supposed to be written to it? Could you post an example of hserin with values in the parenthesis and explain what it does? When I put in an hserin, does it pause the program until a value is sent through the serial port? Also, from the matlab end, if I use fwrite or fprintf, is the value automatically picked up from hserin?

Also, is there away to delete the current programs on the ARC32?

Thanks.


Top
 Profile  
 
 Post subject: Re: Programming ARC32 with Matlab
PostPosted: Tue Aug 24, 2010 9:07 am 
Offline
Site Admin
User avatar

Joined: Thu Mar 01, 2001 11:00 am
Posts: 784
Location: Temecula, CA
Hserial requires 2 peices of startup code and then you can use hserin and hserout commands.

first make sure the compiler adds the HSerial library to your program by putting this line at the top of your code:

Code:
ENABLEHSERIAL


Then before using an hserin or hserout command you need to to the hserial system what baudrate/mode to use:

Code:
SetHSerial H38400,H8DATABITS,HNOPARITY,H1STOPBITS


This commadn sets up the hardware uart to run at 38400bps, 8 data bits, no parity and 1 stop bit(eg 8n1).

Now you can write a simple looping routine to read in any bytes that are received:

Code:
temp var byte
main
   hserin [temp]
   if(temp="A")then
      ...do something here...
   elseif(temp="B")
      ...do something else here...
   endif

_________________
Nathan Scherdin
Basic Micro - Robotic Technology Evolved


Top
 Profile  
 
 Post subject: Re: Programming ARC32 with Matlab
PostPosted: Tue Aug 24, 2010 4:54 pm 
Offline
Citizen

Joined: Tue Aug 03, 2010 7:14 pm
Posts: 7
Thank you. I'll try it out.

How do I delete the current programs on the ARC32?


Top
 Profile  
 
 Post subject: Re: Programming ARC32 with Matlab
PostPosted: Tue Aug 24, 2010 5:29 pm 
Offline
Master

Joined: Tue Nov 21, 2006 9:34 am
Posts: 500
The easiest way to delete a program is to overwrite it with a new one... That is each time you download a program it replaces what is currently on the processor.

Kurt


Top
 Profile  
 
 Post subject: Re: Programming ARC32 with Matlab
PostPosted: Wed Aug 25, 2010 8:33 am 
Offline
Site Admin
User avatar

Joined: Thu Mar 01, 2001 11:00 am
Posts: 784
Location: Temecula, CA
Actually, that is the only way to delete a program from the module. :)

If you really want to delete whats on the module but not putting anything in it's place program this code:

Code:
main
goto main

_________________
Nathan Scherdin
Basic Micro - Robotic Technology Evolved


Top
 Profile  
 
 Post subject: Re: Programming ARC32 with Matlab
PostPosted: Sat Sep 18, 2010 8:56 pm 
Offline
Citizen

Joined: Tue Aug 03, 2010 7:14 pm
Posts: 7
I set up a program with hserin and I was able to send a value using Matlab. The problem is that I can only send one value each time I put the program on the ARC32.

I thought that I might need to put hserin in a while loop so that it would keep waiting for values but I get the same issue. The program is:

ENABLEHSERVO2
ENABLEHSERIAL
SetHSerial H38400,H8DATABITS,HNOPARITY,H1STOPBITS
hservo [p0\50000]
num var byte
num = 0
while(num >= 0)
hserin [num]
hservo [p0\num]
wend


The first value sends fine and the motor moves. I then get the following error in Matlab:

??? Error using ==> serial.fwrite at 199
An error occurred during writing.


What do I need to change to allow the controller to keep taking values from Matlab?

Thank you.


Top
 Profile  
 
 Post subject: Re: Programming ARC32 with Matlab
PostPosted: Sun Sep 19, 2010 7:55 am 
Offline
Master

Joined: Tue Nov 21, 2006 9:34 am
Posts: 500
Unlike the serin command, the HSerial system will automatically buffer up any inputs it receives, so you do not have to be in the hserin command to have the system receive the data. So it should be able to send multiple bytes...

I am not a matlab user so can not give much advice for that. I am not sure if for example when Matlab makes it's first usage of the serial port if the Arc32 is rebooted or not. It often happens when I use other programs to talk to a bap (DTR line...)

One thing I did notice with your program is that I am not sure if your program can ever exit the while loop. That is because num is defined as "num var byte". So num is an unsigned variable that will always have a value between 0 and 255 and as such will always be >=0.

Kurt


Top
 Profile  
 
 Post subject: Re: Programming ARC32 with Matlab
PostPosted: Mon Sep 20, 2010 12:24 pm 
Offline
Site Admin
User avatar

Joined: Thu Mar 01, 2001 11:00 am
Posts: 784
Location: Temecula, CA
Sounds like your serial port on your PC is disconnecting. Somethign like that is what's causing the fwrite error I suspect. I expect your hservo command is causing a power spike which is causing the USB to serial chip on the ARC to disconnect/reset. Make sure you are using enough power supply for the situation. In the worse case you should use a seperate logic power supply and servo power supply.

_________________
Nathan Scherdin
Basic Micro - Robotic Technology Evolved


Top
 Profile  
 
 Post subject: Re: Programming ARC32 with Matlab
PostPosted: Sun Sep 26, 2010 2:43 pm 
Offline
Citizen

Joined: Tue Aug 03, 2010 7:14 pm
Posts: 7
I separated the logic and servo supplies and I no longer get that error. I am able to send multiple values but the motor still only moves to the first position that is sent. The first program I wrote is:

ENABLEHSERVO2
ENABLEHSERIAL
SetHSerial H38400,H8DATABITS,HNOPARITY,H1STOPBITS
num var byte
hserin [num]
hservo [p0\num]


I thought that maybe I had to loop it because it would only take the first value but even after putting it in a loop it still only moves to the first motor position that is sent. The program with the loop is:

ENABLEHSERVO2
ENABLEHSERIAL
SetHSerial H38400,H8DATABITS,HNOPARITY,H1STOPBITS
hservo [p0\50000]
num var byte
num = 0
while(num >= 0)
hserin [num]
hservo [p0\num]
wend


How can I write this program to allow the motor to accept different values as I send them and move to each of the positions sent?

Thanks.


Top
 Profile  
 
 Post subject: Re: Programming ARC32 with Matlab
PostPosted: Mon Sep 27, 2010 8:57 am 
Offline
Site Admin
User avatar

Joined: Thu Mar 01, 2001 11:00 am
Posts: 784
Location: Temecula, CA
you are reading in a single ascii character with your hserin, not a decimal number. For example if you send 0 you are receiving the binary value of the ascii character "0", not 0.

Change your hserin to:

hserin [dec num]

Also you should loop your code otherwise it only runs once.

Code:
ENABLEHSERVO2
ENABLEHSERIAL
SetHSerial H38400,H8DATABITS,HNOPARITY,H1STOPBITS
num var byte
main
  hserin [dec num]
  hservo [p0\num]
  goto main


Note that as of Studio 2.0 you no longer need to use the ENABLEHSERVO,ENABLEHSERVO2 or ENABLEHSERIAL compile time directives. It won't hurt anything if you do but they are now not needed.

Same problem with hserin on your second program.

Note if you are sending binary values then 0 to 255 is too small to really do anything with hservo. The range of HSERVO on the ARC32 is +-30000. You average servo can use from about +-12000 of that. So if you only send a binary value between 0 and 255 you could hardly see any movement.

If you want to send binary values in the correct range you need to send 2 bytes:

Code:
ENABLEHSERVO2
ENABLEHSERIAL
SetHSerial H38400,H8DATABITS,HNOPARITY,H1STOPBITS
num var wordmain
  hserin [num.byte1,num.byte0]
  hservo [p0\num]
  goto main


This code is setup for big endian data. Send the high byte and then the low byte as binary data.

_________________
Nathan Scherdin
Basic Micro - Robotic Technology Evolved


Top
 Profile  
 
 Post subject: Re: Programming ARC32 with Matlab
PostPosted: Mon Sep 27, 2010 6:04 pm 
Offline
Citizen

Joined: Tue Aug 03, 2010 7:14 pm
Posts: 7
I tried the program you wrote and the motor didn't turn. I upgraded to 2.0.0.0 and now I get the following error:

Error: FILE C:\DOCUMENTS AND SETTINGS\OWNER\MY DOCUMENTS\ARC32 PROGRAMS\ARC32 CONTROL CODE3.BAS(LINE 1) : [TOKEN SETHSERIAL] : Unknown Command
Error: FILE C:\DOCUMENTS AND SETTINGS\OWNER\MY DOCUMENTS\ARC32 PROGRAMS\ARC32 CONTROL CODE3.BAS(LINE 1) : [TOKEN H38400] : Unknown Command
Error: FILE C:\DOCUMENTS AND SETTINGS\OWNER\MY DOCUMENTS\ARC32 PROGRAMS\ARC32 CONTROL CODE3.BAS(LINE 1) : [TOKEN ,] : Unknown Command
Error: FILE C:\DOCUMENTS AND SETTINGS\OWNER\MY DOCUMENTS\ARC32 PROGRAMS\ARC32 CONTROL CODE3.BAS(LINE 1) : [TOKEN H8DATABITS] : Unknown Command
Error: FILE C:\DOCUMENTS AND SETTINGS\OWNER\MY DOCUMENTS\ARC32 PROGRAMS\ARC32 CONTROL CODE3.BAS(LINE 1) : [TOKEN ,] : Unknown Command
Error: FILE C:\DOCUMENTS AND SETTINGS\OWNER\MY DOCUMENTS\ARC32 PROGRAMS\ARC32 CONTROL CODE3.BAS(LINE 1) : [TOKEN HNOPARITY] : Unknown Command
Error: FILE C:\DOCUMENTS AND SETTINGS\OWNER\MY DOCUMENTS\ARC32 PROGRAMS\ARC32 CONTROL CODE3.BAS(LINE 1) : [TOKEN ,] : Unknown Command
Error: FILE C:\DOCUMENTS AND SETTINGS\OWNER\MY DOCUMENTS\ARC32 PROGRAMS\ARC32 CONTROL CODE3.BAS(LINE 1) : [TOKEN H1STOPBITS] : Unknown Command
Error: FILE C:\DOCUMENTS AND SETTINGS\OWNER\MY DOCUMENTS\ARC32 PROGRAMS\ARC32 CONTROL CODE3.BAS(LINE 3) : [TOKEN MAIN] : Compiler error 102
Errors Detected

Whats going on??


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next

All times are UTC - 8 hours [ DST ]


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

Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

phpBB SEO