Hello everyone, english is not my native language but I hope you will understand me well.
My project consists of building and developing control alghoritms for inverted pendulum. I assumed that arduino mega will be used for receiving data from encoders, sending them to PC, where Matlab will be computing the control signal that is sent to motor driver which controls my dc motor. The reason I've choosen matlab to control the process is that it is really easy to implement LQR alrghoritm and plot/edit data.
So, here is my problem: I downloaded arduino support package from official mathworks page and obtained good results with manually controlling PWM with writePWMDutyCycle(a, pin, dutyCycle) command (description). However this library doesn't have the code for reading encoder data (which is in the older library, here). I can either control the PWM signal, without reading encoder data, or read encoder data without being able to control PWM signal using mentioned command. Installing both of these doesn't work.
Could anyone provide me with some sort of solution? I could try using newer package which allows me to control the motor easily and read encoder data using Arduino application and then sending it to Matlab, but I don't know if that is the right way to do it.
Not having interfaced matlab with arduino I cannot answer your question. But I can tell you that your approach seems to be unnecessarily complicated. It is much simpler to implement the controller completely on the Mega and avoid matlab entirely. You need only to implement a digital filter using the Mega. The digital filter would accept an input signal from a gyro sensor and output a control signal to a motor.
Hi Robodziej,i've been working at the same project as yours, position control motor dc trough simulink.
if you used simulink for your project, first let me know what motor shield do you used? adafruit or generic motor shield? as i know those two motor shield has different interface setting.
if you used adafruit motor shield, you shuld try this library :
the library contain motor dc block and the encoder block so you just set the value of pwm (for the motor dc block) and just connect encoder block with scope or a display to read the pulse. don't forget to read the guide. because if you use this motor shield you have to setting some stuff first.
library for the generic motor shield its quite easy to use. you have to do just installed it and pick the block as you want. but this library only contain standart block (i.e:analogread,digitalread/digitalwrite/,pwm,serialtransmit,and etc). i recomend you to install that support package from the matlab itself. choose add-on and click the get hardware support package. choose install from internet. install library that support for arduino uno/nano/mega2560.
if you want to read the encoder you have to read the code your self using s-function builder. matlab has provide the examples named encoder_slsp_mega(arduino mega). you can find the files here :
sory for my bad english. hope this can help..