Announcement

Collapse
No announcement yet.

Controlling Stepper Motor With Websocket

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Controlling Stepper Motor With Websocket

    Hello! I've been struggling to control a stepper motor from a webpage. The example program that does this 08.pes_2405_touch_rotate works well but I was trying to adapt it so I could just use html buttons to rotate the motor. My code in index.php is the same as the example with the following being different.

    Added this function
    Code:
    function raise_focus()
    {
    if(ws != null)
        if(ws.readyState == 1)
            ws.send(0, "-100\r\n"); //-100 sent just to test. Eventually it will be a variable
    }
    And this html button
    Code:
    <button id="raise" type="button" onclick="raise_focus();">Raise</button>

    My task0.php looks like this:
    Code:
    <?php
    
    if(_SERVER("REQUEST_METHOD"))
    exit; // avoid php execution via http request
    
    include "/lib/sd_340.php";
    include "/lib/sd_spc.php";
    include "/lib/sn_tcp_ws.php";
    
    define("STEPPER_SID", 1);
    define("STEPS_PER_REV", 200); // 200 steps per revolution (1.8 degree each step)
    define("SPEED_LOWER_LIMIT", 32);
    define("SPEED_UPPER_LIMIT", 100000);
    
    spc_reset();
    spc_sync_baud(115200);
    
    spc_request_dev(STEPPER_SID, "set vref drive 12");
    spc_request_dev(STEPPER_SID, "set mode 32");
    
    ws_setup(0, "pes_2405_touch_rotate", "csv.phpoc");
    
    while(1)
    {
        $rbuf = "";
        if(ws_state(0) == TCP_CONNECTED)
        {
            $rlen = ws_read_line(0, $rbuf);
            $pos = (int)spc_request_dev(STEPPER_SID, "get pos");
    
            if($rlen)
            {
                $pos += (int)$rbuf;
                spc_request_dev(STEPPER_SID, "goto $pos 2000 1000k");
            }
        }
    }
    
    ?>
    My code doesn't seem to turn the stepper (it seems to do nothing) but I'm struggling to figure out what I'm doing wrong.

    Thank you for your help!

  • #2
    could you please post your full code of web (php + html + javascript).
    And do you see any log from PHPoC Debugger ?

    Comment


    • #3
      Khanh, sorry for my late response but I ended up getting it figured out. For anyone else with a similar problem adding .toFixed() (as it's seen in some of the examples) fixed my problem. Thank you for your response!

      Comment


      • #4
        Thank you for sharing your experience!

        Comment

        Working...
        X