helicontrol.c

Go to the documentation of this file.
00001 /***************************************************************************
00002  *  helicontrol - c/c++ control api for silverlit helicopters/quadcopters  *
00003  *  Copyright (C) 2008 by                                                  *
00004  *  Joint Robotics Lab                                                     *
00005  *  Goethe-University Frankfurt, Germany                                   *
00006  *  http://www.jrl.cs.uni-frankfurt.de                                     *
00007  *                                                                         *
00008  *  This library is free software; you can redistribute it and/or          *
00009  *  modify it under the terms of the GNU General Public                    *
00010  *  License as published by the Free Software Foundation                   *
00011  *  version 2 of the License.                                              *
00012  *                                                                         *
00013  *  This program is distributed in the hope that it will be useful,        *
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU      *
00016  *  General Public License for more details.                               *
00017  *                                                                         *
00018  *  You should have received a copy of the GNU General Public              *
00019  *  License along with this library; if not, write to the Free Software    *
00020  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA02110-1301 USA*
00021  ***************************************************************************/
00022 
00023 /* $Id: helicontrol.c 269 2008-12-17 22:25:11Z holgerf $ */
00024 
00031 #include "../firmware/com_def.h"
00032 #include "helicontrol.h"
00033 #include "helicontrol_firmware.h"
00034 #include "frame.h"
00035 #include <string.h>
00036 #include <stdio.h>
00037 
00038 #define TIMEOUT_MS    2000
00039 
00040 struct usb_dev_handle *HC_Init(void)
00041 {
00042     struct usb_bus *bus;
00043     struct usb_device *dev;
00044     struct usb_dev_handle *handle;
00045     char myfirmware[] = "1.0";
00046     int res;
00047     char s[30];
00048 
00049     usb_init();
00050     usb_find_busses();
00051     usb_find_devices();
00052 
00053     for (bus = usb_get_busses(); bus; bus = bus->next)
00054     {
00055         for (dev = bus->devices; dev; dev = dev->next)
00056         {
00057             if ((dev->descriptor.idVendor == HC_VendorID)
00058                     && (dev->descriptor.idProduct == HC_ProductID))
00059             {
00060                 if ((handle = usb_open(dev)) == NULL)
00061                 {
00062                     fprintf(stderr, "usb device: busy, cannot open\n");
00063                 }
00064                 else
00065                 {
00066                     if (usb_set_configuration(handle, 1) < 0)
00067                     {
00068                         /* error */
00069                         fprintf(stderr, "usb device: cannot set configuration\n");
00070                         usb_close(handle);
00071                         handle = 0;
00072                     }
00073                     else
00074                     {
00075                         /* needed for WIN32, ok for Linux, maybe bad idea on MacOS X. remove it if you get into trouble */
00076                         if (usb_claim_interface(handle, 0) < 0)
00077                         {
00078                             /* error */
00079                             fprintf(stderr, "usb device: cannot claim interface\n");
00080                             usb_close(handle);
00081                             handle = 0;
00082                         }
00083                         else
00084                         {
00085                             /* dev found, opened, maybe claimed. */
00086                             /* query firmware rev */
00087 
00088 
00089                             res = HC_Read_Firmware_Revision(handle, s, sizeof(s));
00090                             /* printf("res=%d, s=%s\n", res, s); */
00091                             if (res<=0)
00092                             {
00093                                 fprintf(stderr, "usb device: cannot read firmware revision\n");
00094                             }
00095                             else
00096                             {
00097                                 if (0!= strcmp(s, myfirmware))
00098                                 {
00099                                     fprintf(stderr, "usb device: incompatible firmware (sw: \"%s\", hw: \"%s\").\nplease update firmware and/or software.\n",
00100                                             myfirmware, s);
00101                                     usb_close(handle);
00102                                     handle = 0;
00103                                 }
00104                                 /* else: ok, known device... */
00105                             }
00106 
00107                             return handle;
00108                         }
00109                     }
00110                 }
00111             }
00112         }
00113     }
00114     return NULL;
00115 }
00116 
00117 
00118 int HC_Read_Firmware_Revision(usb_dev_handle *handle, char *fwstring, size_t maxlen)
00119 {
00120     char s[30];
00121     int res;
00122     res = usb_get_string_simple(handle, USB_VendorFirmwareRevIndex, s, sizeof(s));
00123     s[29] = 0;
00124     if (fwstring)
00125     {
00126         if (res <= 0)
00127         {
00128             fwstring[0] = 0;
00129         }
00130         else
00131         {
00132             strncpy(fwstring, s, maxlen);
00133             if (maxlen>0) fwstring[maxlen-1] = 0;
00134         }
00135     }
00136     return res;
00137 }
00138 
00139 int HC_Read_Firmware_Build_Date(usb_dev_handle *handle, char *datestring, size_t maxlen)
00140 {
00141     char s[30];
00142     int res;
00143     res = usb_get_string_simple(handle, USB_VendorFirmwareBuildDate, s, sizeof(s));
00144     s[29] = 0;
00145     if (datestring)
00146     {
00147         if (res <= 0)
00148         {
00149             datestring[0] = 0;
00150         }
00151         else
00152         {
00153             strncpy(datestring, s, maxlen);
00154             if (maxlen>0) datestring[maxlen-1] = 0;
00155         }
00156     }
00157     return res;
00158 }
00159 
00160 void HC_Read_Lib_Build_Date(char *datestring, size_t maxlen)
00161 {
00162     char *buildstring = __DATE__ " " __TIME__;
00163     strncpy(datestring, buildstring, maxlen);
00164     if (maxlen>0) datestring[maxlen-1] = 0;
00165 }
00166 
00167 int HC_CMD_Repeat(usb_dev_handle *handle, char repeat)
00168 {
00169     return usb_control_msg(handle, USB_VendorRequestCode | USB_ENDPOINT_OUT, CMD_AUTOREPEAT, repeat, 0, NULL, 0, TIMEOUT_MS);
00170 }
00171 
00172 
00173 int HC_CMD_Interval(usb_dev_handle *handle, int interval_ms)
00174 {
00175     return usb_control_msg(handle, USB_VendorRequestCode | USB_ENDPOINT_OUT, CMD_REPEAT_INTERVAL, interval_ms, 0, NULL, 0, TIMEOUT_MS);
00176 }
00177 
00178 
00179 int HC_CMD_Stop(usb_dev_handle *handle)
00180 {
00181     return usb_control_msg(handle, USB_VendorRequestCode | USB_ENDPOINT_OUT, CMD_STOP, 0, 0, NULL, 0, TIMEOUT_MS);
00182 }
00183 
00184 
00185 int HC_Send_Picooz(usb_dev_handle *handle, char channel, char throttle, char yaw, char trim)
00186 {
00187     frame.raw = 0;
00188     frame.picooz.channel = channel;
00189     frame.picooz.throttle = throttle;
00190     frame.picooz.trim = trim;
00191     frame.picooz.yaw = yaw;
00192 
00193     return usb_control_msg(handle, USB_VendorRequestCode | USB_ENDPOINT_OUT, PROTO_PICOOZ, frame.usb.value, frame.usb.index, NULL, 0, TIMEOUT_MS);
00194 }
00195 
00196 
00197 int HC_Send_Challenger(usb_dev_handle *handle, char channel, char throttle, char yaw, char trim, char fire)
00198 {
00199     frame.raw = 0;
00200     frame.challenger.channel = channel;
00201     frame.challenger.throttle = throttle;
00202     frame.challenger.trim = trim;
00203     frame.challenger.yaw = yaw;
00204     frame.challenger.fire = 3*!!fire;
00205 
00206     return usb_control_msg(handle, USB_VendorRequestCode | USB_ENDPOINT_OUT, PROTO_CHALLENGER, frame.usb.value, frame.usb.index, NULL, 0, TIMEOUT_MS);
00207 }
00208 
00209 
00210 int HC_Send_Uranus(usb_dev_handle *handle, char channel, char throttle, char yaw, char pitch, char trim, char light)
00211 {
00212     frame.raw = 0;
00213     frame.uranus.channel = channel;
00214     frame.uranus.throttle = throttle;
00215     frame.uranus.trim = trim;
00216     frame.uranus.yaw = yaw;
00217     frame.uranus.pitch = pitch;
00218     frame.uranus.light = !!light;
00219 
00220     return usb_control_msg(handle, USB_VendorRequestCode | USB_ENDPOINT_OUT, PROTO_URANUS, frame.usb.value, frame.usb.index, NULL, 0, TIMEOUT_MS);
00221 }
00222 
00223 
00224 int HC_Send_Tandemz(usb_dev_handle *handle, char channel, char throttle, char yaw, char pitch, char pitch_trim, char light)
00225 {
00226     frame.raw = 0;
00227     frame.tandemz.channel = channel;
00228     frame.tandemz.light = !!light;
00229     frame.tandemz.throttle = throttle;
00230     frame.tandemz.pitch = pitch;
00231     frame.tandemz.ptrim = pitch_trim;
00232     frame.tandemz.yaw = yaw;
00233 
00234     return usb_control_msg(handle, USB_VendorRequestCode | USB_ENDPOINT_OUT, PROTO_TANDEMZ, frame.usb.value, frame.usb.index, NULL, 0, TIMEOUT_MS);
00235 }
00236 
00237 
00238 /* FIXME: protocol not fully analysed yet (esp. no checksum calculated by AVR firmware) */
00239 int HC_Send_Ibird(usb_dev_handle *handle, char channel, char throttle, char yaw, char unknown, char checksum)
00240 {
00241     frame.raw = 0;
00242     frame.ibird.channel = channel;
00243     frame.ibird.throttle = throttle;
00244     frame.ibird.yaw = yaw;
00245     frame.ibird.unknown = unknown;
00246     frame.ibird.checksum = checksum;
00247 
00248     return usb_control_msg(handle, USB_VendorRequestCode | USB_ENDPOINT_OUT, PROTO_IBIRD, frame.usb.value, frame.usb.index, NULL, 0, TIMEOUT_MS);
00249 }
00250 
00251 
00252 int HC_Send_Saucer(usb_dev_handle *handle, char throttle, char yaw, char pitch, char roll)
00253 {
00254     frame.raw = 0;
00255     frame.saucer.roll = roll;
00256     frame.saucer.pitch = pitch;
00257     frame.saucer.throttle = throttle;
00258     frame.saucer.yaw = yaw;
00259 
00260     return usb_control_msg(handle, USB_VendorRequestCode | USB_ENDPOINT_OUT, PROTO_SAUCER, frame.usb.value, frame.usb.index, NULL, 0, TIMEOUT_MS);
00261 }
00262 
00263 
00264 void HC_Close(usb_dev_handle *handle)
00265 {
00266     usb_release_interface(handle, 0);
00267     usb_close(handle);
00268 }

Generated on Thu Dec 18 00:02:52 2008 for libhelicontrol by  doxygen 1.5.3