[Newbie Help]Obtaining Digital Input In Code

C, C++, and Visual C++
george_syn
Fresh meat
Posts: 2
Joined: Tue Jun 21, 2016 4:47 pm
Contact:

[Newbie Help]Obtaining Digital Input In Code

Postby george_syn » Tue Jun 21, 2016 4:55 pm

Hello Phidgets community. I am new to Phidgets and I am having some trouble dealing with the digital inputs of the 1063 stepper motor driver. I am able to see that it reads digital input properly through the Phidget Control Panel. However, after I close it, and run the code that I obtain by modifying the stepper-simple example, it doesn't really work. Please give me some advice. Thank you.

p.s. I haven't received any formal training on embedded programming, and most of my knowledge comes from hacking up projects with arduino.

The main block of code I am having trouble with is the second to last function. "checkInput()"

//-----------------code below----------------------//

Code: Select all

/backAndForth
//basically modifying Stepper Simple, and trying various functions
// - Stepper simple -
// This simple example sets up a Stepper object, hooks the event handlers and opens it for device connections. 
// Once an Advanced Servo is attached it will move the motor to various positions.
//
// Please note that this example was designed to work with only one Phidget Stepper connected.
//
// Copyright 2008 Phidgets Inc.  All rights reserved.
// This work is licensed under the Creative Commons Attribution 2.5 Canada License.
// view a copy of this license, visit http://creativecommons.org/licenses/by/2.5/ca/
// modified by : George Huang

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

// define some constants first
int currLim = 0;
double minAccel_glob = 1;
double maxVel_glob = 2;

int CCONV AttachHandler(CPhidgetHandle stepper, void *userptr)
{
   int serialNo;
   const char *name;

   CPhidget_getDeviceName(stepper, &name);
   CPhidget_getSerialNumber(stepper, &serialNo);
   printf("%s %10d attached!\n", name, serialNo);

   return 0;
}

int CCONV DetachHandler(CPhidgetHandle stepper, void *userptr)
{
   int serialNo;
   const char *name;

   CPhidget_getDeviceName(stepper, &name);
   CPhidget_getSerialNumber(stepper, &serialNo);
   printf("%s %10d detached!\n", name, serialNo);

   return 0;
}

int CCONV ErrorHandler(CPhidgetHandle stepper, void *userptr, int ErrorCode, const char *Description)
{
   printf("Error handled. %d - %s\n", ErrorCode, Description);
   return 0;
}

int CCONV PositionChangeHandler(CPhidgetStepperHandle stepper, void *usrptr, int Index, __int64 Value)
{
   printf("Motor: %d > Current Position: %lld\n", Index, Value);
   return 0;
}

//Display the properties of the attached phidget to the screen.  We will be displaying the name, serial number and version of the attached device.
int display_properties(CPhidgetStepperHandle phid)
{
   int serialNo, version, numMotors;
   const char* ptr;

   CPhidget_getDeviceType((CPhidgetHandle)phid, &ptr);
   CPhidget_getSerialNumber((CPhidgetHandle)phid, &serialNo);
   CPhidget_getDeviceVersion((CPhidgetHandle)phid, &version);

   CPhidgetStepper_getMotorCount(phid, &numMotors);

   printf("%s\n", ptr);
   printf("Serial Number: %10d\nVersion: %8d\n# Motors: %d\n", serialNo, version, numMotors);

   return 0;
}

int checkInput() {
   int result;
   __int64 curr_pos;
   const char *err;
   double minAccel, maxVel;
   int stopped;
   int inputState = 1;

   //Declare an stepper handle
   CPhidgetStepperHandle stepper = 0;

   //create the stepper object
   CPhidgetStepper_create(&stepper);

   //Set the handlers to be run when the device is plugged in or opened from software, unplugged or closed from software, or generates an error.
   CPhidget_set_OnAttach_Handler((CPhidgetHandle)stepper, AttachHandler, NULL);
   CPhidget_set_OnDetach_Handler((CPhidgetHandle)stepper, DetachHandler, NULL);
   CPhidget_set_OnError_Handler((CPhidgetHandle)stepper, ErrorHandler, NULL);

   //Registers a callback that will run when the motor position is changed.
   //Requires the handle for the Phidget, the function that will be called, and an arbitrary pointer that will be supplied to the callback function (may be NULL).
   CPhidgetStepper_set_OnPositionChange_Handler(stepper, PositionChangeHandler, NULL);

   //open the device for connections
   CPhidget_open((CPhidgetHandle)stepper, -1);

   //get the program to wait for an stepper device to be attached
   printf("Waiting for Phidget to be attached....");
   if ((result = CPhidget_waitForAttachment((CPhidgetHandle)stepper, 10000)))
   {
      CPhidget_getErrorDescription(result, &err);
      printf("Problem waiting for attachment: %s\n", err);
      return 0;
   }

   //Display the properties of the attached device
   display_properties(stepper);

   if (CPhidgetStepper_getInputState(stepper, 0, &inputState) == PTRUE) {
      printf("input is false\n");
   }
   if (CPhidgetStepper_getInputState(stepper, 1, &inputState) == PTRUE) {
      printf("input is false\n");
   }
   if (CPhidgetStepper_getInputState(stepper, 2, &inputState) == PTRUE) {
      printf("input is false\n");
   }
   if (CPhidgetStepper_getInputState(stepper, 3, &inputState) == PTRUE) {
      printf("input is true\n");
   }

   //end routine
   printf("Press any key to end\n");
   getchar();

   //since user input has been read, this is a signal to terminate the program so we will close the phidget and delete the object we created
   printf("Closing...\n");
   CPhidget_close((CPhidgetHandle)stepper);
   CPhidget_delete((CPhidgetHandle)stepper);
   return 0;
}

int main(int argc, char* argv[])
{
   //stepper_simple();
   //outputParameters(); //not working yet
   checkInput();
   return 0;
}



frodegill
Phidget Mastermind
Posts: 114
Joined: Thu Mar 04, 2010 2:51 am
Contact:

Re: [Newbie Help]Obtaining Digital Input In Code

Postby frodegill » Wed Jun 22, 2016 1:49 am

george_syn wrote:

Code: Select all

   printf("Waiting for Phidget to be attached....");
   if ((result = CPhidget_waitForAttachment((CPhidgetHandle)stepper, 10000)))


Here you're checking for fail if result is true (defined as not 0), inside a pair of extra parantheses. This time it happens to give correct result (as EPHIDGET_OK is defined as 0), but you really want do this this explicit, as in

Code: Select all

   printf("Waiting for Phidget to be attached....");
   if (EPHIDGET_OK != (result = CPhidget_waitForAttachment((CPhidgetHandle)stepper, 10000)))


george_syn wrote:

Code: Select all

   if (CPhidgetStepper_getInputState(stepper, 0, &inputState) == PTRUE) {
      printf("input is false\n");
   }


..but here your return value mismatch fails. CPhidgetStepper_getInputState returns EPHIDGET_OK on ok, and you're checking for PTRUE (which is defined as 1, which will only be returned if the call fails with EPHIDGET_NOTFOUND). The input state is set in inputState, not in the return value.

Code: Select all

   if (EPHIDGET_OK==CPhidgetStepper_getInputState(stepper, 0, &inputState) && PFALSE==inputState) {
      printf("input is false\n");
   }

george_syn
Fresh meat
Posts: 2
Joined: Tue Jun 21, 2016 4:47 pm
Contact:

Re: [Newbie Help]Obtaining Digital Input In Code

Postby george_syn » Wed Jun 22, 2016 11:21 am

@frodegill

Thank you so much. Your suggestions work perfectly!

I think the part I messed up is mainly not understanding the output of the getInputState function. Your comment on being more explicit is certainly helpful and will definitely save me from a lot of future bugs.


Return to “C/C++”

Who is online

Users browsing this forum: No registered users and 0 guests