StepperBee

Hi everyone,
I’m doing a project with object tracking and motor-adjusments.
But I’m quite new to C++. Yet, I understand a lot now.
But I ordered an Steppermotor control-board, called StepperBee.
Anyone heard about it? The StepperBee is C++ compatible.
http://www.pc-control.co.uk/stepperbee-info.htm

The manual says that I should initialize the StepperBee handle and work with that, but I don’t really understand how that works.
I’ve read a LOT of tutorials but none help me with understanding Handles and external program interface.

Could anyone help me please? :frowning:

Thanks

Basically you have some functions that are not directly accessible from your code. They are in a dll, so first you need to load that dll (get a handle to it) and then get pointers to the functions in it to be able to call them. Once you have this you can call those pointers as if they were normal functions…

According to the manual:

  
  
// declaration of variable to hold the handle to the dll  
HINSTANCE HStpDll;   
  
// load the dll into memory and return handle  
HStpDll = LoadLibrary(“stp.dll”);   
  
//declare a pointer to InitStp function  
Type_InitStp          InitStp;  
  
//get the pointer to it  
InitStp = (Type_InitStp)GetProcAddress( HStpDll, "InitStp");  
  
// call it  
InitStp ();  
  

you’ll need that the dll is in the same directory as your binary, in the bin directory or specify where it is when you call LoadLibary…

Wow, that’s a fast reply! :smiley:

Really thanks arturo, I think I understand what they mean now.
Only thing that remains is to wait untill the package arrives:P
But how should I imlpement it? into my declarations in my header file or in the setup part?

So there are funtions that I can use within that DLL to control my StepperMotors?

Thanks!

I suposse it should be something like:

testApp.h:

  
  
// declaration of variable to hold the handle to the dll  
HINSTANCE HStpDll;  
  
//declare a pointer to InitStp function  
Type_InitStp          InitStp;  
  
//the same for the rest of the functions you need  
...  
  

tespApp.cpp:

  
testApp::setup(){  
    // load the dll into memory and return handle  
    HStpDll = LoadLibrary(“stp.dll”);  
    //get the pointer to it  
    InitStp = (Type_InitStp)GetProcAddress( HStpDll, "InitStp");  
    // call it  
    InitStp ();  
...  
}  

of course you’ll need to make some calls in setup to init the thing, and some in update to change it’s state.

and yes, dll’s (shared libraires) contain functions in binary format or even classes, you can specify how to load them at compile time or at run-time which is what you do when you create a handle in this case.

another really fast reply!:smiley:

Really thanks arturo, that makes things a lot clearer now:)

I will post my program when I’m done, but first, I will have to wait untill the package arrives:S Especially that information about hot to initiate the DLL and retrieve it’s functions really helped me out!:slight_smile:

Thansk Arturo!

WOOOT! It’s alive!!:D:D

Thanks arturo for your guidance!

My program is actually following my laserpointer attached to the two steppermotors:D
But it still is very, very slow…(Or maybe it’s my laptop…:P)

But as promised, here’s the code:)

  
#include "videoIn.h"  
#include "st.h"  
  
void videoIn::setup(){  
  
    ofSetWindowTitle("VideoIn");  
  
     HINSTANCE HStpDll;  
     HStpDll = LoadLibrary("stp.dll");  
  
  
        InitStp = (Type_InitStp)GetProcAddress( HStpDll, "InitStp");  
        RunMotor1 = (Type_RunMotor1)GetProcAddress( HStpDll, "RunMotor1");  
        RunMotor2 = (Type_RunMotor2)GetProcAddress( HStpDll, "RunMotor2");  
        StopMotor1 = (Type_StopMotor1)GetProcAddress( HStpDll, "StopMotor1");  
        StopMotor2 = (Type_StopMotor2)GetProcAddress( HStpDll, "StopMotor2");  
        SetStepMode = (Type_SetStepMode)GetProcAddress( HStpDll, "SetStepMode");  
        GetCurrentStatus = (Type_GetCurrentStatus)GetProcAddress(HStpDll,"GetCurrentStatus");  
  
    camWidth = 640;  /*vanwege de snelheid even het beeld vekleind, de /2 weghalen in het uiteindelijke programma voor precisie*/  
    camHeight = 480;  
  
	#ifdef _USE_LIVE_VIDEO  
        vidGrabber.setVerbose(true);  
        vidGrabber.initGrabber(camWidth, camHeight);  
	#endif  
  
    colorImg.allocate(camWidth, camHeight);  
	grayImage.allocate(camWidth, camHeight);  
	grayBg.allocate(camWidth, camHeight);  
	grayDiff.allocate(camWidth, camHeight);  
  
	bLearnBakground = true;  
	threshold = 80;  
  
	status = InitStp();  
}  
  
void videoIn::update(){  
  
	ofBackground(0,0,0);  
  
    bool bNewFrame = false;  
  
	#ifdef _USE_LIVE_VIDEO  
       vidGrabber.grabFrame();  
	   bNewFrame = vidGrabber.isFrameNew();  
	#endif  
  
	if (bNewFrame){  
  
		#ifdef _USE_LIVE_VIDEO  
            colorImg.setFromPixels(vidGrabber.getPixels(), camWidth, camHeight);  
        #endif  
  
        grayImage = colorImg;  
		if (bLearnBakground == true){  
			grayBg = grayImage;  
			bLearnBakground = false;  
		}  
        grayDiff.absDiff(grayBg, grayImage);  
		grayDiff.threshold(threshold);  
        contourFinder.findContours(grayDiff, 1, 128, 1, false);  
  
  
        if ((mouseX != 0)&&(mouseY != 0)){  
        if (dotX < mouseX){  
            RunMotor1(10,1,0,0);  
        };  
        if (dotX > mouseX){  
            RunMotor1(10,1,1,0);  
        };  
        if (dotY < mouseY){  
            RunMotor2(10,1,0,0);  
        };  
        if (dotY > mouseY){  
            RunMotor2(10,1,1,0);  
        };  
}}}  
  
void videoIn::draw(){  
  
	ofSetColor(0xffffff);  
	colorImg.draw(0,0);  
  
    for (int i = 0; i < contourFinder.nBlobs; i++){  
        contourFinder.blobs[i].draw(0,0);  
    }  
    if ( contourFinder.nBlobs == 1 ){  
    float x = contourFinder.blobs[0].centroid.x;  
    float y = contourFinder.blobs[0].centroid.y;  
    dotX = x;  
    dotY = y;  
    }  
  
	ofSetColor(0xffffff);  
	char reportStr[1024];  
	sprintf(reportStr,"Background subtraction and blob detection\nPress spacebar to capture bg - Press 's' to reset coordinates\nThreshold %i (press: +/-)\nNum blobs found %i\nPress mousebutton for coordinates: ( %i ; %i )\nCentrecoordinates: ( %i ; %i )\nStatus: %i", threshold, contourFinder.nBlobs, mouseX, mouseY, dotX, dotY, status);  
	ofDrawBitmapString(reportStr, 20, 500);  
  
	if ( (( dotX == mouseX )&&(dotY == mouseY))&&(((dotX != 0)&&(dotY != 0))&&((mouseX != 0)&&(mouseY != 0)))){  
	    ofSetColor(0xffffff);  
	    char reportStr[1024];  
        sprintf(reportStr,"Target Locked");  
        ofDrawBitmapString(reportStr, 100, 580);  
	}  
}  
  
void videoIn::keyPressed  (int key){  
  
	switch (key){  
		case ' ':  
			bLearnBakground = true;  
			break;  
		case '+':  
			threshold ++;  
			if (threshold > 255) threshold = 255;  
			break;  
		case '-':  
			threshold --;  
			if (threshold < 0) threshold = 0;  
			break;  
        case          's':  
            dotX = 0;  
            dotY = 0;  
            mouseX = 0;  
            mouseY = 0;  
            break;  
	}  
}  
  
void videoIn::mousePressed(int x, int y, int button){  
  
    mouseX = x;  
    mouseY = y;  
  
}  
  

Maybe someone knows what I should alter to make my program run faster(A)

WOOT!!:D:D

Hello,
If running your example with OF_v0073_win_cb_release, CodeBlocks (gcc), Windows 7 64 bits it gets the stupiest error when compiling: ‘LoadLibrary’ was not declared in this scope.
Any guess if some different includes are needed to use LoadLibrary on Windows 64 bits?

Ok, I got it. Just use LoadLibraryA instead of LoadLibrary