Data acquisition and device control with Scilab.
Desciription
Scilab is a freeware alternative to MATLAB. For low-cost data acquisition and device control a nice Arduino toolbox is available.
This site presents a Mbed port which allows to use Mbed boards (equipped with Arduino header) rather than Arduino to import real time data into Scilab and to control real equipment witch Scilab.
Installation
- Install Scilab to your PC, if not done yet.
- Launch Scilab and install the Arduino toolbox by executing the following command from the Scilab console:
--> atomsInstall("arduino")
Controlling Mbed's digital output from Scilab
- In Xcos open
examples/Arduino1.zcos
- Double click on the
Board
setup block and replace the serial port number with mbed's actual virtual serial port number.
- Double click on the
Digital WRITE
block and set Digital Pin to 13 (D13 is connected to LED1).
- Start simulation and LED1 on the Mbed board should start blinking.
Reading and displaying Mbed's analog input
- In Xcos open
examples/Arduino2.zcos
- Double click on the
Board
setup block and replace the serial port number with mbed's actual virtual serial port number.
- Double click on the
Analog READ
block and set Analog Pin to 2.
- Start simulation and a graph should appear showing the analog signal measured on Mbed's pin A2.
NOTE: Currently, there is bug in the toolbox ARDUINO_ANALOG_READ_sim function (I have reported to Scilab) so the analog readings are not correct.
PID controller
- In Xcos open
examples/Arduino9.zcos
Diff: ScilabSerial/ScilabSerial.h
- Revision:
- 0:295b7e1c12f3
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ScilabSerial/ScilabSerial.h Mon Jan 18 19:51:22 2021 +0000 @@ -0,0 +1,103 @@ +#ifndef SCILABSERIAL_H +#define SCILABSERIAL_H + +#include "mbed.h" +#include "Servo.h" +#include "IntrInCounter.h" +#include "Encoder.h" +#include "DcMotor.h" +#include "MPU6050.h" + +//#define USBSERIAL +#define RX_MAX 64 + +#ifdef USBSERIAL +#include "USBSerial.h" +//#include "stm32f103c8t6.h" +#endif +/** + * @brief The PcSerial class + * @note For serial communication with PC + */ +#ifdef USBSERIAL +class PcSerial : + public USBSerial +{ +public: + PcSerial() : + USBSerial(0x1f00, 0x2012, 0x0001, false) + { + rxDataLen = 0; + attach(this, &PcSerial::onRx); + } + + void onRx() + { + memset(rxData, 0, RX_MAX); + while (available()) { + char c = _getc(); + if (c != '\n') { + if (rxDataLen >= RX_MAX) + rxDataLen = 0; // rxBuf overflow + rxData[rxDataLen++] = c; + } + } + } + + ssize_t write(const void* buffer, size_t length) { return writeBlock((uint8_t*)buffer, length); } + char rxData[RX_MAX]; + volatile size_t rxDataLen; +}; +#else +class ScilabSerial : + public SerialBase +{ + bool _rxComplete; + char _rxBuf[RX_MAX]; + volatile size_t _rxBufLen; + char _rxData[RX_MAX]; + volatile size_t _rxDataLen; + +#if defined(TARGET_BLUEPILL) + PinName _digitalPinName[16] = { NC, NC, D2, D3, D4, D5, D6, D7, D8, NC, D10, D11, D12, D13, D14, D15 }; + PinName _analogPinName[6] = { A0, A1, A2, A3, NC, NC }; +#elif defined(TARGET_BLACKPILL) + PinName _digitalPinName[16] = { NC, NC, D2, D3, D4, D5, D6, D7, D8, NC, D10, D11, D12, D13, D14, D15 }; + PinName _analogPinName[6] = { A0, A1, A2, A3, NC, NC }; +#else + PinName _digitalPinName[16] = { NC, NC, D2, D3, D4, D5, D6, D7, D8, D9, D10, D11, D12, D13, D14, D15 }; + PinName _analogPinName[6] = { A0, A1, A2, A3, A4, A5 }; +#endif + DigitalInOut* _digitalInOuts[8]; + AnalogIn* _analogIns[6]; + PwmOut* _pwmOuts[4]; + IntrInCounter* _intrInCounters[8]; + Servo* _servos[2]; + Encoder* _encoders[2]; + DcMotor* _dcMotors[2]; + MPU6050* _mpu6050; + Timer _mpu6050Timer; + uint32_t _now; + float _deltaT; + bool _mpu6050Available; +public: + + ScilabSerial(PinName tx, PinName rx, int baud = 115200); + + void run(); + void onRx(); + ssize_t write(const void* buffer, size_t length); + void analogRead(); + void pwmWrite(); + void digitalReadWrite(); + void servo(); + void mpu6050(); + void intrInCounter(); + void encoder(); + void dcMotorInit(); + void dcMotor(); + void revision(); + void mpu6050UpdateData(); +}; +#endif +#endif // SCILABSERIAL_H