Cross-platform c example – Pololu Simple User Manual

Page 96

Advertising
background image

setup();

while (1)

{

loop();

}

}

6.7.3. Cross-platform C Example

The example C code below works on Windows, Linux, and Mac OS X 10.7 or later. It demonstrates how to get the
error status from the controller, how to read a variable, and how to set the target speed.

This code will work in Windows if compiled with MinGW, but it does not work with the Microsoft C compiler. For
Windows-specific example code that works with either compiler, see

Section 6.7.4

.

For this example to work, the Simple Motor Controller’s input mode must be Serial/USB, the serial
mode must be Binary, and the CRC Mode must be set to Disabled. These are the default settings that
the controller is shipped with. The controller should be connected to the computer via USB.

// Uses POSIX functions to send and receive data from the virtual serial

// port of a Pololu Simple Motor Controller.

// NOTE: The Simple Motor Controller's Input Mode must be set to Serial/USB.

// NOTE: You must change the 'const char * device' line below.

#include <fcntl.h>

#include <stdio.h>

#include <unistd.h>

#ifdef _WIN32

#define O_NOCTTY 0

#else

#include <termios.h>

#endif

#define SERIAL_ERROR -9999

// Reads a variable from the SMC and returns it as number between 0 and 65535.

// Returns SERIAL_ERROR if there was an error.

// The 'variableId' argument must be one of IDs listed in the

// "Controller Variables" section of the user's guide.

// For variables that are actually signed, additional processing is required

// (see smcGetTargetSpeed for an example).

int smcGetVariable(int fd, unsigned char variableId)

{

unsigned char command[] = {0xA1, variableId};

if(write(fd, &command, sizeof(command)) == -1)

{

perror("error writing");

return SERIAL_ERROR;

}

unsigned char response[2];

if(read(fd,response,2) != 2)

{

perror("error reading");

return SERIAL_ERROR;

}

return response[0] + 256*response[1];

}

// Returns the target speed (-3200 to 3200).

// Returns SERIAL_ERROR if there is an error.

int smcGetTargetSpeed(int fd)

{

int val = smcGetVariable(fd, 20);

return val == SERIAL_ERROR ? SERIAL_ERROR : (signed short)val;

}

Pololu Simple Motor Controller User's Guide

© 2001–2014 Pololu Corporation

6. Using the Serial Interface

Page 96 of 101

Advertising