Send new program to Arduino without Simulink

71 次查看(过去 30 天)
Hi,
I'd like to send a new program/sketch to an Arduino, through Matlab. The closest I've been able to come is to use:
This seems to require using Simulink to send the new program. Is there a way to use this code without Simulink? Or maybe someone knows of another route?
Please note that the ArduinoIO package doesn't help us either in this regard.
  1 个评论
Paul
Paul 2014-8-8
Solution has been worked out:
1) Develop in the Arduino IDE all of the programs/sketches that you will need
2) In the Arduino IDE, turn ON verbose output via File -> Preferences
3) Upload program 1
4) When upload is finished, record the last line of the compilation output. If my program is called blink1, then I saved this line to blink1_cmd.txt
Note that in the example shown, the last line is:
C:\ArduinoIDE\hardware/tools/avr/bin/avrdude -CC:\ArduinoIDE\hardware/tools/avr/etc/avrdude.conf -v -v -v -v -patmega328p -carduino -P\\.\COM9 -b115200 -D -Uflash:w:C:\Users\PAULJO~1\AppData\Local\Temp\build6145494704296263461.tmp\blink1.cpp.hex:i
5) Find the HEX file associated with your program and save it elsewhere. In the case above, the HEX file is:
C:\Users\PAULJO~1\AppData\Local\Temp\build6145494704296263461.tmp\blink1.cpp.hex
And I moved it to:
C:\Users\PAULJO~1\Documents\MATLAB\TestCode\Arduino\ArduinoIdeProgramming\blink1.cpp.hex
Please note: If you run into trouble, try using folders without spaces
6) In your text file, update the HEX file path
7) Copy the entire line from the text file
8) In the Matlab command window:
[status,cmdout] = dos(' paste code here ')
You should get an output of:
avrdude done. Thank you.
9) Repeat as necessary for the other programs.

请先登录,再进行评论。

采纳的回答

Paul
Paul 2014-8-14
Solution works for Windows, Mac, and Linux
-----------------------------------------------------
What you want to be able to do:
Through MATLAB, upload a program/sketch to your Arduino.
During the upload process, the Arduino IDE creates a temporary HEX file. We want to copy that HEX file to a permanent location and be able to send it to the Arduino via MATLAB
-----------------------------------------------------
Before beginning:
  1. Make sure you have the Arduino IDE installed on your computer
  2. Connect your Arduino via USB to the computer
  3. Locate the program you want to upload to the Arduino
-----------------------------------------------------
Important: As I was looking for this solution, I've heard that some people encounter problems when folder paths include spaces. I haven't done testing myself, I instead re-installed the Arduino IDE to C:\ArduinoIDE\ on the PC and /Applications/Arduino.app/ on the mac
-----------------------------------------------------
-----------------------------------------------------
Solution
-----------------------------------------------------
1. Launch Arduino IDE ( Arduino.app on mac, Arduino.exe on PC)
2. Go to Preferences by using CTRL + comma on the PC or COMMAND + comma on the Mac
3. In Preferences , find the fourth line that starts "Show verbose output...". Check ON the compilation box
4. Open your file and upload it to the Arduino.
5. When the uploading is done, find the second to last line of the output (immediately before Binary sketch size: ...) on Mac or the very last line on PC
5.a. On a mac, the line should begin with /Applications/Arduino.app/Contents/...
5.b. On a PC, the line should begin with C:\Program Files\Arduino\hardware... if you have the default install, otherwise I get C:\ArduinoIDE\hardware...
6. Copy the line, then launch Matlab and save the line in strOutput. Also, define the COM port used to connect to the Arduino (in the Arduino IDE, go to Tools -> Serial Port)
strOutput = ' [paste line here] '; % Be sure to remove the extra whitespaces at the front and end
comPort = '/dev/cu.usbmodem1d11' % mac example
comPort = 'COM9' % windows example
7. Get path to avrdude and store it in pathAVR
idxSpace = regexp(strOutput,'\s');
strAVRBIN = 'avr.bin';
idxAVRBIN = regexp(strOutput, strAVRBIN );
idxSpace = idxSpace( idxSpace>idxAVRBIN );
strAVR = strOutput(1:idxSpace(1)-1);
pathAVR = strAVR(1:idxAVRBIN+size(strAVRBIN,2));
pathAVR = sprintf('%s%s',pathAVR,'avrdude');
8. Get path of the temporary HEX file (this is your Arduino program)
idxHEX = regexp(strOutput,'hex');
pathHEX = strOutput(idxSpace(end)+1:idxHEX(end)+2); % This may be incorrect if there are spaces
9. Provide path to your permanent library of HEX files. Cannot have spaces. For now, I'll use the desktop
accountName = 'Paul';
if ispc
% Windows
pathHexLib = sprintf('C:\\Users\\%s\\Desktop\\',accountName);
else
% Mac
pathHexLib = sprintf('/Users/%s/Desktop/',accountName);
end
10. Save the temporary HEX file
if ispc
% Windows therefore slash is \
idxSlash = regexp(pathHEX,'[\\]');
else
% Mac therefore slash is /
idxSlash = regexp(pathHEX,'[/]');
end
filenameHEX = pathHEX(idxSlash(end)+1:end);
pathHEX_new = sprintf('%s%s',pathHexLib,filenameHEX);
[status] = copyfile(pathHEX,pathHEX_new);
if ~status
[status] = copyfile(pathHEX,pathHEX_new,'f');
end
11. We have saved our HEX file. Now we can send it to the Arduino
pathAVRconf = sprintf('%s%s%s',pathAVR(1:end-11),'etc','/avrdude.conf');
deviceType = 'atmega328p';
programmerType = 'arduino';
baudRate = '115200'; % Not needed. To use in strUpload, add before '-D' as '-b%s'
if ispc
% Windows
strUpload = sprintf('%s -C%s -p%s -c%s -P%s -D -Uflash:w:%s:i',...
pathAVR,pathAVRconf,deviceType,programmerType,comPort,pathHEX_new);
else
% Mac
strUpload = sprintf('%s -C%s -p%s -c%s -P%s -D -Uflash:w:%s',...
pathAVR,pathAVRconf,deviceType,programmerType,comPort,pathHEX_new);
end
[status,result] = system( strUpload );
  7 个评论
Xiaokai Li
Xiaokai Li 2017-5-2
Hello Paul, I followed your instruction and was able to upload script (HEX) to Uno board successfully. But the same method does not seem to work for MEGA2560 board. I got the following error "avrdude: stk500_recv(): programmer is not responding". I have made sure the comPort is correct and the device type is changed to "atmega2560".
Atiyeh
Atiyeh 2024-4-17,22:25
I have the same problem with arduini Mega. Please let me know if you hav efound the solution. Thanks.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB Support Package for Arduino Hardware 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by