How to use Arduino with 2 DAC(Adafruit4725) in embed
Hey Guys and thanks for your help. I'm a student at University of Trier (Germany) and i want to transfer this working Arduino IDE C-Code to Altair Embed.
#include <Wire.h> #include <Adafruit_MCP4725.h> Adafruit_MCP4725 dac,dac1; void setup(void) { Serial.begin(9600); dac.begin(0x60); dac1.begin(0x61); } void loop(void) { uint16_t i=200; dac.setVoltage(i, false); dac1.setVoltage(i, false); }
The i variable(see above arduino ide) should be replaced with slider properties in Altair Embed for real-time
changes(does Target Interface Block work for this?)).
The problem is also that the method "setVoltage"(Arduino IDE) needs an uint16_t datatype.
The "convert to" block in embed has no uint16_t type.
My embed vsm does not work see attachments. I'm thankful for ideas.
Best Answer
-
Hi Nico,
Please find attached the corrected vsm diagram. Here are some pointers for you that could help and the corrections I have made so that for future it could be handy.
Change 1: The Adafruit library for MCP4725 dependency Adafruit_I2CDevice.cpp needs to be added along with the Adafruit_MCP4725.cpp. This will help you to compile the diagram to generate the needed hex file.
Change 2: Now if your intention is to also use the sliders from the PC and control the inputs that are being sent to the dac then you need to setup the embed diagram to generate code with the target interface enabled. The target interface block can be found here . The target interface block can take input for the .elf or the executable file for the target and provides the necessary input and output ports that you can connect the sliders on the host .
Change 3: For embed diagrams that need a target interface . The code generation needs to be done for a compound block of interest with an option as below. The check box while checked helps in generating code for a compound block with ability to exchange data of the inputs/outputs with the host pc.
Change 4:
If you are planning to use the slider in real time then the simulation should also be setup rightly so. The way to achieve this is using the simulation pane as below. These settings ensure that the diagram on the PC is running in real time in this case with a time step of 0.01 sec and runs to about 100 seconds and keeps repeating infinitely given the auto restart at end is selected .
Please find attached a diagram with the changes made.
Here is a summary of the steps you need to take to test the diagram
* Step 1: Open the attached diagram in Altair Embed
* Step 2: Ensure that you select the compound block and then go to the tools menu and launch the code gen option with the check box selected to "Use Selected compound edge pins for data exchange"
Step 3: Compile the diagram. This produces the .elf file .
Step 4: Double click on the target interface block to see if the .elf file is loaded and that is it.
Step 5: You should hit on GO or the green button to start the simulation. Embed will recognize that a target interface block is present and hence will download the .elf to the connected target (say Arduino Uno or Mega) and then once the download is successful will start running the algorithm. At this point you should be able to move the sliders and also observe data that you have selected on the slider on the scopes. In addition on the hardrware you should be able to measure the analog voltage on the pins from the dac outputs.
In case you also want to see the analog signal back in embed. You know what to do now. Add couple of ADC read blocks on the embed diagram and then you should be able to see the analog signals read back too.
I do not have this DAC part at my end but the diagram should pretty much work. Please select the appropriate target lets say UNO or mega in the target interface and appropriate serial port on your machine for the target.
Note : If you are using an Arduino uno it only has only 1 serial port and if you are using it for the HIL simulation as is the case here you may not be able to use the serial blocks for other tasks . Nevertheless if you have parts like Mega there are 3 serial ports and embed would always use the Serial 0 and you are free to use the other 2.
Hope this is helps.
Let us know if you need further support .
I also recommend these videos if you are using Arduino with Altair Embed first time :
--sreeram
1
Answers
-
Hi Nico,
Please find attached the corrected vsm diagram. Here are some pointers for you that could help and the corrections I have made so that for future it could be handy.
Change 1: The Adafruit library for MCP4725 dependency Adafruit_I2CDevice.cpp needs to be added along with the Adafruit_MCP4725.cpp. This will help you to compile the diagram to generate the needed hex file.
Change 2: Now if your intention is to also use the sliders from the PC and control the inputs that are being sent to the dac then you need to setup the embed diagram to generate code with the target interface enabled. The target interface block can be found here . The target interface block can take input for the .elf or the executable file for the target and provides the necessary input and output ports that you can connect the sliders on the host .
Change 3: For embed diagrams that need a target interface . The code generation needs to be done for a compound block of interest with an option as below. The check box while checked helps in generating code for a compound block with ability to exchange data of the inputs/outputs with the host pc.
Change 4:
If you are planning to use the slider in real time then the simulation should also be setup rightly so. The way to achieve this is using the simulation pane as below. These settings ensure that the diagram on the PC is running in real time in this case with a time step of 0.01 sec and runs to about 100 seconds and keeps repeating infinitely given the auto restart at end is selected .
Please find attached a diagram with the changes made.
Here is a summary of the steps you need to take to test the diagram
* Step 1: Open the attached diagram in Altair Embed
* Step 2: Ensure that you select the compound block and then go to the tools menu and launch the code gen option with the check box selected to "Use Selected compound edge pins for data exchange"
Step 3: Compile the diagram. This produces the .elf file .
Step 4: Double click on the target interface block to see if the .elf file is loaded and that is it.
Step 5: You should hit on GO or the green button to start the simulation. Embed will recognize that a target interface block is present and hence will download the .elf to the connected target (say Arduino Uno or Mega) and then once the download is successful will start running the algorithm. At this point you should be able to move the sliders and also observe data that you have selected on the slider on the scopes. In addition on the hardrware you should be able to measure the analog voltage on the pins from the dac outputs.
In case you also want to see the analog signal back in embed. You know what to do now. Add couple of ADC read blocks on the embed diagram and then you should be able to see the analog signals read back too.
I do not have this DAC part at my end but the diagram should pretty much work. Please select the appropriate target lets say UNO or mega in the target interface and appropriate serial port on your machine for the target.
Note : If you are using an Arduino uno it only has only 1 serial port and if you are using it for the HIL simulation as is the case here you may not be able to use the serial blocks for other tasks . Nevertheless if you have parts like Mega there are 3 serial ports and embed would always use the Serial 0 and you are free to use the other 2.
Hope this is helps.
Let us know if you need further support .
I also recommend these videos if you are using Arduino with Altair Embed first time :
--sreeram
1 -
Hi,
it works now for me. I've made a few minor changes.
If someone should work with the DAC 4725 in the future and comes across this article, the required libraries and the vsm are attached.
0