确认状态


确认步进电机动作状态

通过 getState() 函数可确认步进电机的动作状态。

state = step.getState()

此函数返还步进电机动作状态的代码。返还的动作状态的种类如下。

返还值 状态
0 stopped
1 control locked
otherwise running

示例

#include <PhpocExpansion.h>
#include <Phpoc.h>

byte spcId = 1;

ExpansionStepper step(spcId);

int state;

void setup() {
    Serial.begin(9600);
    while(!Serial)
        ;

    Phpoc.begin(PF_LOG_SPI | PF_LOG_NET);
    Expansion.begin();

    Serial.println(step.getPID());
    Serial.println(step.getName());

    step.setMode(4);
    step.setVrefStop(2);
    step.setVrefDrive(8);
    step.setResonance(120, 250);
    step.setSpeed(400);
    step.setAccel(800);

    step.stepGoto(400);

    while(state = step.getState()) {
        Serial.print("state: ");
        Serial.println(state);
        delay(200);
    }

    Serial.print("state: ");
    Serial.println(state);
}

void loop() {

}
state: 2
state: 2
state: 2
state: 2
state: 2
state: 2
state: 2
state: 0

确认计数位置

通过 getPosition() 函数可确认当前计数位置。

pos = step.getPosition()

此函数返还步进电机的当前计数位置。

示例

#include <PhpocExpansion.h>
#include <Phpoc.h>

byte spcId = 1;

ExpansionStepper step(spcId);

int pos = -400;

void setup() {
    Serial.begin(9600);
    while(!Serial)
        ;

    Phpoc.begin(PF_LOG_SPI | PF_LOG_NET);
    Expansion.begin();

    Serial.println(step.getPID());
    Serial.println(step.getName());

    step.setMode(4);
    step.setVrefStop(2);
    step.setVrefDrive(8);
    step.setResonance(120, 250);
    step.setSpeed(400);
    step.setAccel(800);
    step.setPosition(pos);

    step.stepGoto(400);

    while(step.getState()) {
        pos = step.getPosition();
        Serial.print("position: ");
        Serial.println(pos);
        delay(200);
    }
}

void loop() {

}
position: -400
position: -369
position: -302
position: -214
position: -126
position: -39
position: 48
position: 135
position: 223
position: 310
position: 375
position: 400

确认数字输入端口的状态

通过 getEio() 函数可确认数字输入端口的当前状态。

state = step.getEio(id)

返还的输入端口的状态种类如下。

返还值 状态
0 LOW
1 HIGH (default)

示例

#include <PhpocExpansion.h>
#include <Phpoc.h>

byte spcId = 1;

ExpansionStepper step(spcId);

void setup() {
    Serial.begin(9600);
    while(!Serial)
        ;

    Phpoc.begin(PF_LOG_SPI | PF_LOG_NET);
    Expansion.begin();

    Serial.println(step.getPID());
    Serial.println(step.getName());
}

void loop() {
    Serial.print(step.getEio(0));
    Serial.print(step.getEio(1));
    Serial.print(step.getEio(2));
    Serial.print(step.getEio(3));
    Serial.println();
    delay(1000);
}
1111  
1111  
1110  
0110  
...