Shop OBEX P1 Docs P2 Docs Learn Events
anybody help me with servo controller in matlab — Parallax Forums

anybody help me with servo controller in matlab

yoyokilleryoyokiller Posts: 8
edited 2008-01-17 13:17 in General Discussion
hi,everyone,recently I am doing basic control of BLDC motor in matlab, I tried many ways but the dc motor still not going any body did control in MATLAB please give me a hand, I attached the code below please show me what's the mistake I had! Thanks

function y = servo_control(u)

pulse_width=round(u);

ser_obj=serial('COM8','baudrate',9600);

ser_obj.terminator = 'CR';

fopen(ser_obj);

fprintf(ser_obj,'%d\n',pulse_width); % send duration of pulse width used to control the servomotor

fclose(ser_obj);

y=u;

Comments

  • Shawn LoweShawn Lowe Posts: 635
    edited 2008-01-16 15:34
    Are you using a stamp to send the pulse widths to the servo? If you are just sending pulses from matlab, you might want ot try asking this same question on a matlab forum, as they probably have more experience with matlab.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Shawn Lowe


    Maybe I should have waited to do that......
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2008-01-16 15:38
    This thread is being moved from the BASIC Stamp Forum to the Sandbox Forum.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • yoyokilleryoyokiller Posts: 8
    edited 2008-01-16 21:04
    I have been solved thanks
  • Shawn LoweShawn Lowe Posts: 635
    edited 2008-01-17 02:50
    How did you solve it, if I may ask? I, too, have had to work with matlab (in college) and I am curious how you ended up doing this task.

    Thanks

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Shawn Lowe


    Maybe I should have waited to do that......
  • yoyokilleryoyokiller Posts: 8
    edited 2008-01-17 11:27
    I can give you the code here, but sadly my servo controller probably has dead since yesterday I made a mistake, I can not get the version number any more, I don't know whether the PSC can be rewrite again. I am going to change another servo board. the code below you can have a look.

    function servo_control(sp)
    global magSerial
    magSerial=serial('COM8');
    set(magSerial,'BaudRate',2400,'StopBits',2,'Terminator','cr') %USB Serial Port
    magSerial.Timeout = 1; %time to wait for the recorded data
    magSerial.ByteOrder = 'bigEndian';
    magSerial.terminator = 'CR';
    fopen(magSerial)
    servoPos=sp+750;
    servoRamp=20;
    highByte=floor(servoPos/256);
    lowByte=servoPos-highByte*256;
    %send data to the serial port
    fwrite(magSerial,[noparse][[/noparse]abs('!SC'),0,servoRamp,lowByte,highByte,13])
    %fwrite(magSerial,[noparse][[/noparse]abs('!SC'),1,servoRamp,lowByte,highByte,13])
    fclose(magSerial)
    clear magSerial
    end

    that is the position control and for example give a initial postion 1500 and desired postion 500 just give command in matlab servo_control(1500);servo_control(500)
  • Shawn LoweShawn Lowe Posts: 635
    edited 2008-01-17 13:17
    Cool, thanks!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Shawn Lowe


    Maybe I should have waited to do that......
Sign In or Register to comment.