Difference: SampleCode (1 vs. 3)

Revision 3
21 Dec 2013 - Main.ClifCox
Line: 1 to 1
 
META TOPICPARENT
<-- -->
Sorted ascending
name="ElectronicDoorLock"

Sample Code mostly for the Mbed

Line: 208 to 208
  }
Added:
>
>

QC-5000 Test Code

Exercise the Proto-board IO.
// Control the AMC2500 steppers

#include "mbed.h"

// Defines

#define YLEFT  p30
#define YRIGHT p29
#define XFRONT p28
#define XREAR  p27
#define XCLK   p26
#define XDIR   p25
#define YCLK   p24
#define YDIR   p23
#define ZAX    p21
#define RELAY   p22
#define MOSI    p5
#define MISO    p6
#define SCLK    p7
#define STCP    p8  // 74LS595 Latch outputs
#define SCAL    36.4
#define AIN     p20
#define INC     1
#define UP      2
#define CS      4
#define MSK     7

DigitalIn yl(YLEFT);
DigitalIn yr(YRIGHT);
DigitalIn xf(XFRONT);
DigitalIn xr(XREAR);
DigitalOut xdir(XDIR);
DigitalOut ydir(YDIR);
DigitalOut *adir = &xdir;
DigitalInOut zax(ZAX);
DigitalInOut relay(RELAY);
SPI spi(MOSI, MISO, SCLK);
DigitalOut latch(STCP);
AnalogIn spinv(AIN);

Serial pc(USBTX, USBRX); // tx, r
int ramp(float target, int step, float delay);
int write(unsigned int data);
int vadj(int up);

PwmOut xstep(XCLK);
PwmOut ystep(YCLK);
PwmOut *astep= &xstep;

float sps = 0; // start with a slow steps per second
unsigned int   data =8;

int main() {
    yl.mode(PullUp);
    yr.mode(PullUp);
    xf.mode(PullUp);
    xr.mode(PullUp);
    zax = 1; zax.output(); zax.mode(OpenDrain);
    relay = 1; relay.output(); relay.mode(OpenDrain);
    spi.format(8,0);
    spi.frequency(1000000);

    //  ramp(50, 10, 0.1);
    xstep.period(1/sps);            // set a starting steps per second
    xstep.pulsewidth(0.5/sps);      // and a 50% Duty cycle
    write(data);                       // Clear 74LS595 data so all controls are off
    pc.printf("Press 's' for slower, or 'd' for faster, 'c' to change direction.\n\r");

    while(1) {
        char c = pc.getc();
        // Slow down current axis speed
        if((c == 's')) {
            sps -= 50;
            // sps = 0.0005;
            if (sps < 0) sps = 0;
            astep->period(1/sps);
            astep->pulsewidth(0.5/sps);
        }
        // Speed up current axis speed
        if((c == 'd')) {
            sps += 50;
            // sps = 0.0024;
            astep->period(1/sps);
            astep->pulsewidth(0.5/sps);
        }
        // Change current axis direction
        if (c == 'c') {
            float s= sps;

            // pc.printf("Ramping down\n");            
            ramp(0, 200, 0.01);
            *adir = *adir ? 0 : 1;
            ramp(s, 200, 0.01);
        }
        // Stop current axis
        if (c == ' ') {
            ramp(0, 200, 0.01);
            astep->pulsewidth(0);
        }
        if (c == 'l') {
            int p=0;
            if (!yl) { pc.printf("Left "); p=1; }
            if (!yr) { pc.printf("Right "); p=1; }
            if (!xf) { pc.printf("Front "); p=1; }
            if (!xr) { pc.printf("Rear"); p=1; }
            if (p)   { printf("\n\r"); p=0; }
        }
        // Toggle Realay
        if (c == 'r') {
            relay = relay ? 0 : 1;
        }
        // Toggle Motor
        if (c == 'm') {
            if (8 & data) {
                data = ~8&data;
                write(data);
                wait(0.1);
                pc.printf("Spindle Voltage = %f V \n\r", SCAL*spinv);
            } else {
                data = 8|data;
                write(data);
            }
       }
       // Decrease? current Spindle voltage and report.
        if (c == 'n') {
            vadj(0);
            wait(0.1);
            pc.printf("Spindle Voltage = %f V \n\r", SCAL*spinv);
        }
        // Increase? current Spindle voltage and report.
       if (c == ',') {
            vadj(1);
            wait(0.1);
            pc.printf("Spindle Voltage = %f V \n\r", SCAL*spinv);
        }
        // Toggle Vacume
        if (c == 'v') {
            data = (16 & data) ? (~16)&data : 16|data;
            write(data);
        }
        // Toggle Z axis
        if (c == 'z') {
            zax = zax ? 0 : 1;
        }
        // Swich movment to other (Alternate) Axis
        if (c == 'a') {
            float s = sps;
            ramp(0, 200, 0.01);
            astep->pulsewidth(0);
            if (astep == &xstep) { astep = &ystep; adir = &ydir; }
            else { astep = &xstep; adir = &xdir; }
            ramp(s, 200, 0.01);
        }
        // Print current "speed"
        pc.printf("SPS= %f\n\r", sps);
    }
}

int ramp(float target, int step, float delay) {
    target = target < 0 ? 0 : target;
    if (target > sps) step = abs(step);
    else step = -abs(step);

    while( abs(sps - target) > 0.1) {
        wait(delay);
        if (abs(target - sps) >= abs(step)) sps += step;
        else sps = target;
        astep->period(1/sps);
        astep->pulsewidth(0.5/sps);
        // pc.printf("SPS = %f\n\r", sps);
    }
        return 0;
}

int write(unsigned int data) {
    // pc.printf("Writing: %0X\n\r", data);
    latch = 0;
    spi.write(data);
    // wait(0.1);
    latch = 1; // latch = 0;
    return 1;
}

int vadj(int up) {
    data &= ~MSK;
    write(INC|data);
    // wait(.1);
    write(INC|(up ? UP:0)|data);
    // wait(.1);
    write((up ? UP:0)|data);
    // wait(.1);
    write(INC|(up ? UP:0)|data);
    // wait(.1);
    write((up ? UP:0)|data);
    // wait(.1);
    write(INC|(up ? UP:0)|data);
    // wait(.1);
    write((up ? UP:0)|data);
    // wait(.1);
    write(CS|data);
    // wait(.1);
    // data |= INC;
    write(data);
    return 1;
}
  -- ClifCox - 19 Nov 2011
Revision 2
19 Nov 2011 - Main.ClifCox
Line: 1 to 1
 
META TOPICPARENT name="ElectronicDoorLock"

Sample Code mostly for the Mbed

Line: 74 to 74
 

Beeper Sweep

Changed:
<
<
This lets you sweep a Piseo's frequancy though a range with the s and d keys.
>
>
This lets you sweep a Piezo buzzer's frequency though a range with the s and d keys.
 
// Hello World to sweep a beeper through its full range
// Control a R/C model beeper
 
This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Eugene Maker Space Wiki? Send feedback