User Manual

5.h.2. Windows C
For example C code that shows how to control the Maestro using its serial interface in Microsoft
Windows, download MaestroSerialExampleCWindows.zip [https://www.pololu.com/file/0J545/
MaestroSerialExampleCWindows.zip] (4k zip). This zip archive contains a Microsoft Visual C++ 2010
Express project that shows how to send a Set Target command and also a Get Position command. It
can also be compiled with MinGW. The Maestro’s serial mode needs to be set to “USB Dual Port” for
this code to work. This example is like the previous example except it does the serial communication
using Windows-specific functions like CreateFile and SetCommState. See the comments in the source
code for more details.
5.h.3. PIC18F4550
The following example code for the PIC18F4550 was submitted to us by customer Ney Palma
Castillo. It is intended to be compiled with the PIC MCU C Compiler [http://www.ccsinfo.com/
content.php?page=compilers]. The code shows how to control a Maestro via serial commands using pins
C6 and C7. It first commands the channel 0 servo to go to its minimum position and then, one second
later, to go to its neutral position. Please see Section 5.e for information on connecting the PIC to the
Maestro and set the Maestro’s serial mode to “UART, detect baud rate.”
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#ifdef _WIN32
_setmode(fd, _O_BINARY);
#else
struct termios options;
tcgetattr(fd, &options);
options.c_iflag &= ~(INLCR | IGNCR | ICRNL | IXON | IXOFF);
options.c_oflag &= ~(ONLCR | OCRNL);
options.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
tcsetattr(fd, TCSANOW, &options);
#endif
int position = maestroGetPosition(fd, 0);
printf("Current position is %d.\n", position);
int target = (position < 6000) ? 7000 : 5000;
printf("Setting target to %d (%d us).\n", target, target/4);
maestroSetTarget(fd, 0, target);
close(fd);
return 0;
}
Pololu Maestro Servo Controller User’s Guide © 2001–2019 Pololu Corporation
5. Serial Interface Page 65 of 102