Skip to content

Commit da36b49

Browse files
committed
analog voltage out added
1 parent 3c2bf19 commit da36b49

File tree

2 files changed

+174
-2
lines changed

2 files changed

+174
-2
lines changed
2.77 KB
Loading

content/hardware/07.opta/opta-family/opta/tutorials/01.user-manual/content.md

Lines changed: 174 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3643,10 +3643,182 @@ This output mode lets you control voltage-driven actuators.
36433643
To set a voltage in an analog output terminal use the built-in function `pinVoltage()` as shown below:
36443644

36453645
```arduino
3646-
float value = exp.getRtd(<input>); // this returns the resistive value measured in the input in ohms
3646+
exp.pinVoltage(ch, <voltage>, true); // the first argument is to define the output channel, the second, the voltage
36473647
```
3648+
You can also configure the output voltage using the `setDac()` function as follows:
36483649

3649-
For the following example a 2 wires **PT1000** will be used connected to **I1**. The sketch below will let you measure the resistance and convert it to a temperature value. This sketch is based on the built-in example found in **File > Examples > Arduino_Opta_Blueprint > Analog > RTD**:
3650+
```arduino
3651+
exp.setDac(ch, <dac_value>); // the first argument is to define the output channel, the second, the DAC output in bits
3652+
```
3653+
3654+
The following example will let you set an output voltage on every channel at once, increasing and decreasing it sequentially, this sketch is based on the built-in example found in **File > Examples > Arduino_Opta_Blueprint > Analog > DAC**:
3655+
3656+
```arduino
3657+
#include "OptaBlue.h"
3658+
3659+
#define PERIODIC_UPDATE_TIME 5000
3660+
#define DELAY_AFTER_SETUP 200
3661+
3662+
/* -------------------------------------------------------------------------- */
3663+
void printExpansionType(ExpansionType_t t) {
3664+
/* -------------------------------------------------------------------------- */
3665+
if (t == EXPANSION_NOT_VALID) {
3666+
Serial.print("Unknown!");
3667+
} else if (t == EXPANSION_OPTA_DIGITAL_MEC) {
3668+
Serial.print("Opta --- DIGITAL [Mechanical] ---");
3669+
} else if (t == EXPANSION_OPTA_DIGITAL_STS) {
3670+
Serial.print("Opta --- DIGITAL [Solid State] ---");
3671+
} else if (t == EXPANSION_DIGITAL_INVALID) {
3672+
Serial.print("Opta --- DIGITAL [!!Invalid!!] ---");
3673+
} else if (t == EXPANSION_OPTA_ANALOG) {
3674+
Serial.print("~~~ Opta ANALOG ~~~");
3675+
} else {
3676+
Serial.print("Unknown!");
3677+
}
3678+
}
3679+
3680+
/* -------------------------------------------------------------------------- */
3681+
void printExpansionInfo() {
3682+
/* -------------------------------------------------------------------------- */
3683+
static long int start = millis();
3684+
3685+
if (millis() - start > 5000) {
3686+
start = millis();
3687+
Serial.print("Number of expansions: ");
3688+
Serial.println(OptaController.getExpansionNum());
3689+
3690+
for (int i = 0; i < OptaController.getExpansionNum(); i++) {
3691+
Serial.print("Expansion n. ");
3692+
Serial.print(i);
3693+
Serial.print(" type ");
3694+
printExpansionType(OptaController.getExpansionType(i));
3695+
Serial.print(" I2C address ");
3696+
Serial.println(OptaController.getExpansionI2Caddress(i));
3697+
}
3698+
}
3699+
}
3700+
3701+
int8_t oa_index = -1;
3702+
/* -------------------------------------------------------------------------- */
3703+
/* SETUP */
3704+
/* -------------------------------------------------------------------------- */
3705+
void setup() {
3706+
/* -------------------------------------------------------------------------- */
3707+
Serial.begin(115200);
3708+
delay(2000);
3709+
3710+
OptaController.begin();
3711+
3712+
for (int device = 0; device < OptaController.getExpansionNum(); device++) {
3713+
3714+
for (int ch = 0; ch < OA_AN_CHANNELS_NUM; ch++) {
3715+
3716+
/* odd channel are configured as voltage DAC */
3717+
AnalogExpansion::beginChannelAsDac(OptaController,
3718+
device,
3719+
ch,
3720+
OA_VOLTAGE_DAC,
3721+
true,
3722+
false,
3723+
OA_SLEW_RATE_0);
3724+
}
3725+
}
3726+
}
3727+
3728+
3729+
/* -------------------------------------------------------------------------- */
3730+
void optaAnalogTask() {
3731+
/* -------------------------------------------------------------------------- */
3732+
3733+
static long int start = millis();
3734+
3735+
/* using this the code inside the if will run every PERIODIC_UPDATE_TIME ms
3736+
assuming the function is called repeteadly in the loop() function */
3737+
3738+
if (millis() - start > PERIODIC_UPDATE_TIME) {
3739+
start = millis();
3740+
3741+
/* this implement a 2 states state machine
3742+
in the rising state the values of all the dac are increased
3743+
of 1000 "bits"
3744+
in the falling state the values of all dac channel are decreased
3745+
of 1000 "bits"
3746+
we change state from rising to falling when bit are greater than 6000
3747+
and we go back to rising state when bit are lower than 1000*/
3748+
3749+
static uint16_t dac_value = 0;
3750+
static bool rising = 1;
3751+
if (rising) {
3752+
/* RISIGN STATE*/
3753+
dac_value += 1000;
3754+
if (dac_value > 6000) {
3755+
/* go in falling state */
3756+
rising = 0;
3757+
}
3758+
} else {
3759+
/* FALLING STATE */
3760+
dac_value -= 1000;
3761+
if (dac_value <= 1000) {
3762+
/* go in rising state */
3763+
rising = 1;
3764+
}
3765+
}
3766+
for (int i = 0; i < OptaController.getExpansionNum(); i++) {
3767+
AnalogExpansion exp = OptaController.getExpansion(i);
3768+
if (exp) {
3769+
Serial.println("Setting dac value " + String(dac_value) + " on expansion n. " + String(exp.getIndex()));
3770+
for (int ch = 0; ch < OA_AN_CHANNELS_NUM; ch++) {
3771+
exp.setDac(ch, dac_value);
3772+
}
3773+
}
3774+
}
3775+
}
3776+
}
3777+
3778+
/* -------------------------------------------------------------------------- */
3779+
/* LOOP */
3780+
/* -------------------------------------------------------------------------- */
3781+
void loop() {
3782+
/* -------------------------------------------------------------------------- */
3783+
OptaController.update();
3784+
printExpansionInfo();
3785+
optaAnalogTask();
3786+
}
3787+
```
3788+
Please take into account that `OptaController.update()` must be called cyclically to support the hot plug of new expansions. In other words, by calling the update() function cyclically, the controller will discover new expansions when they are plugged in while the controller is already running.
3789+
3790+
Thanks to this function, the action of plugging in a new expansion will cause the controller to start a completely new discovery process and a new I2C address assignment.
3791+
3792+
`OptaController.update()` function DOES NOT:
3793+
* Check if an expansion has been removed and remove their objects
3794+
* Update any data from or to the expansion
3795+
3796+
The expansion object in the example above is defined using the `OptaController.getExpansion(i);` function, as follows:
3797+
3798+
```arduino
3799+
for(int i = 0; i < OptaController.getExpansionNum(); i++) { // check all the available expansion slots
3800+
AnalogExpansion exp = OptaController.getExpansion(i);
3801+
}
3802+
```
3803+
The above method will check if there is an Ext A0602 expansion connected in the `i` index from the five admitted. This will ensure which expansion the read state belongs to.
3804+
3805+
The expansion channels are configured as **voltage output** using the function `beginChannelAsRtd` alongside the following parameters:
3806+
3807+
```arduino
3808+
AnalogExpansion::beginChannelAsDac(OptaController, // object class
3809+
device, // the device
3810+
ch, // the output channel
3811+
OA_VOLTAGE_DAC, // output mode
3812+
true, // enable current limit
3813+
false, // enable slew
3814+
OA_SLEW_RATE_0); // set slew rate
3815+
```
3816+
3817+
The function `optaAnalogTask()` increases sequentially the `dac_value` variable to set the voltage output on the expansion channels and decrease it back generating a swiping output.
3818+
3819+
After the Opta™ controller is programmed with the example sketch, you can measure the voltage on the expansion outputs and experience the following behavior:
3820+
3821+
![Analog Voltage Output Demo](assets/analog-voltage.png)
36503822

36513823
#### Analog Current Output Mode
36523824

0 commit comments

Comments
 (0)