Main Page | Directories | File List | Globals | Examples

stepper.c

This example shows you how to control stepper using the steppermotor controller module.

#include <bluesense.h>

//#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>


int main(int argc, char **argv) {
        char *progname = *argv++; argc--;

        // check argument count
        if(argc < 6) {
                fprintf(stderr, "\n");
                fprintf(stderr, "Please re-run this example with \n");
                fprintf(stderr, "  an identifier, \n");
                fprintf(stderr, "  a current value (0-2000 mA), \n");
                fprintf(stderr, "  an autostop flag (0 = no automatic stop, 1 = stop when limit switch is triggered) \n");
                fprintf(stderr, "  a halfStep flag (0 = full Step, 1 = half Step) \n");
                fprintf(stderr, "  a wave Drive flag (0 = no wave drive, 1 = use wave drive) \n");
                fprintf(stderr, "  a decay flag (0 = normal/slow decay, 1 = fast decay) \n");
                fprintf(stderr, "\n");
                fprintf(stderr, "Usage: %s <identifier> <current> <autoStop> <halfStep> <waveDrive> <fastDecay> \n", progname);
                fprintf(stderr, "  eg.: %s 1311i 500 1\n", progname);
                fprintf(stderr, "\n");
                fprintf(stderr, "To find out what devices are connected try the 'list_devices' example.\n");
                fprintf(stderr, "\n");
                return(1);
        }

        // get identifier and side from command line
        identifier_t id = atoi(argv[0]);
        uint16_t current = (uint16_t)atoi(argv[1]);
        uint8_t autostop = (uint8_t)atoi(argv[2]);
        uint8_t halfStep = (uint8_t)atoi(argv[3]);
        uint8_t waveDrive = (uint8_t)atoi(argv[4]);
        uint8_t fastDecay = (uint8_t)atoi(argv[5]);

        // allocate memory for an interface table and resigster a known device interfaces
        bsDeviceInterface_registeredDeviceInterfaces_t registered = bsDeviceInterface_allocInterfaceTable();
        
        // allocate memory of a device table and open a master device
        bsDeviceTable_table_t devices = bsDeviceTable_allocDeviceTable();
        bsMaster_device_t master = bsMaster_allocMaster(registered,devices,0);

        printf("Detecting device %d...\n", id);
        fflush(stdout);

        // try to find our device within the next 5 seconds
        bsDeviceTable_deviceInfo_t dev = bsMaster_detectDevice(master, id, 5 * 1000 * 1000);

        // device not found?
        if(!dev) {
                fprintf(stderr, "Device %d not found!\n", id);

                // clean up previously allocated memory
                bsMaster_freeMaster(master);
                bsDeviceTable_freeDeviceTable(devices);
                bsDeviceInterface_freeInterfaceTable(registered);
                return(0);
        }

        // get specific device
        bsStepperDevice_specifics_t specDev = bsStepperDevice_getDeviceSpecifics(dev);
        uint32_t autoYieldParam = 0;
        
        autoYieldParam = bsMaster_autoYield(master,1000000,2000,autoYieldParam);
        // calling yield for one second ensures that everything is initialized

        printf("Setting current %u, autostop %u, halfStep %u, waveDrive %u, fastDecay %u \n",current,autostop, halfStep, waveDrive, fastDecay);
        // set configuration
        bsStepperDevice_requestSetConfig(specDev,current,autostop,halfStep, waveDrive, fastDecay);
        while (!bsStepperDevice_getRequestSetConfigDone(specDev)) {
                autoYieldParam = bsMaster_autoYield(master,100*1000,2000,autoYieldParam);
        }
        printf("Setting configuration done \n");
        
        printf("  The '-'+enter and '=' key decrease and increase the speed.\n");
        printf( "  The 'q'+enter key quits.\n");
        printf( "\n");

        
        int16_t speed = 0;
        
        int8_t speedstep = 4;
        
        while (1) {
                //fd_set fds;
                //FD_ZERO(&fds);
                //FD_SET(0,&fds);
                //struct timeval tv;
                //tv.tv_sec = 0;
                //tv.tv_usec = 0;
                char key;
                //if(select(0+1, &fds, NULL, NULL, &tv) > 0) {
                        key = getchar();
                //} else {
                //      key = 0;
                //}

                                
                if (key=='-') {
                        speed -= speedstep;
                        if (speed<-128) {
                                speed = -128;
                        }
                } else if (key=='=') {
                        speed += speedstep;
                        if (speed>127) {
                                speed = 127;
                        }
                } else if (key=='q') {
                        break;
                } else if (key>20) {
                        speed = (int8_t)key-32;
                }
                
                if (key!=0) {
                        printf("Speed %d\n",speed);
                        fflush(stdout);
                        
                        bsStepperDevice_requestSetSpeed(specDev,speed);
                }
                
                autoYieldParam = bsMaster_autoYield(master,100*1000,2000,autoYieldParam);
        }
        

        // clean up previously allocated memory
        bsMaster_freeMaster(master);
        bsDeviceTable_freeDeviceTable(devices);
        bsDeviceInterface_freeInterfaceTable(registered);
        return(0);
}       




Generated on Mon Jan 21 17:51:55 2008 for BlueSense by  doxygen 1.4.4