Subversion Repositories Projects

Rev

Rev 178 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
178 Alpin 1
/*  
2
 *  *****MoteCtrl*****
186 Alpin 3
 *      written by AndreasB (aka Alpin)
178 Alpin 4
 *
5
 *               lets you control your MikroKopter with a Wiimote!
6
 *
7
 *
8
 *      Copyright December 2008
9
 *
10
 *      Credits & Thx:
11
 *      - Holger & Ingo for the MikroKopter Project (www.mikrokopter.de)
12
 *      - ExternalControl implementation of the FlightControl
13
 *  - Ligi for RIDDIM, proof of external control concept
14
 *      - Documentation of the MK's SerialProtocol (www.mikrokopter.de/ucwiki/en/SerialProtocol)
15
 *      - Michael Laforest for the wiiuse API (www.wiiuse.net)
16
 *
17
 *
18
 *      http://www.mikrokopter.de/ucwiki/en/MoteCtrl
19
 */
20
 
21
/*
22
*****Use at your own risk! No warranty. Only for private usage.*****
23
 
24
#LICENSE:       Released under the GNU General Public License Version 2 or
25
                        (at your option) a later version of this license.
26
                        For further details see <www.gnu.org/licenses/>.
27
 
28
#DISCLAIMER: Use this project at your own risk!
29
                         No warrenty.
30
                         Only for private usage.
31
 
32
***** See READ_ME_FIRST.txt for further details about MoteCtrl *****
33
*/
34
 
35
/*
36
Compiler & Linker Porperties:
37
Set "Character Set"-CompilerFlag to "Not Set" or "Multi Byte Character Set" to
38
avoid Errors about Windows API functions that can handle Unicode paramters.
39
In Visual Studio: Properties of Project -> Configuration Properties -> General
40
*/
41
 
42
/*
43
When compiling, don't forget to link wiiuse.lib (interface to the wiiuse.dll)
44
In Visual Studio: Copy wiiuse.lib & wiiuse.dll to project directory.
45
Afterwards: Properties of Project -> Configuration Properties -> Linker
46
-> Input -> add "wiiuse.lib" to additional dependencies
47
*/
48
 
49
/*
50
To avoid problems with dependencies on other windows machines, link the used
51
windows DLLs statically to the project.
52
In Visual Studio: Properties of Project -> Config. Properties -> C/C++
53
-> Code Generation -> set "Runtime Library" to Multi-Threaded (/MT)
54
*/
55
 
56
/*Main.c merges all the resources provided by the header files to a functional
57
program. The follwing "includes" include all the needed and used headers (which
58
provide the resources, like fuctions, wrappers, structs, etc. main.c uses)
59
*/
60
 
61
/**********Main Header*********/
62
//OS libs:
63
#include <windows.h>    //Windows API, for API macros, constants, etc. (like stdlib.h)
64
#include <stdio.h>              //Standard I/O (printf for instance)
65
 
66
//MoteCtrl libs:
67
#include "pc_serial_port.h"     //interface with a PC's serial port
68
#include "fc_comm.h"            //encode & send data to FlightControl
69
#include "wiimote.h"            //interface with the wiimote connected via Bluetooth
70
 
71
//#include "main.h"
72
 
73
 
74
//Pretty Text output: (==fun ;)
75
void prettyText(char* string) {
76
        int printed;
77
        int i;
78
 
79
        //pretty text output:
80
        printed = printf("*****%s", string);
81
 
82
        for (i=0;i<(65-printed);i++) {
83
                printf("*");
84
        }
85
        printf("\n");
86
}
87
 
88
 
89
int main(int argc, char* argv[]) {
90
 
91
        //temps & counters
92
        int tmp;
93
        int tmp1;
94
        //int i;
95
 
96
        //Print title:
97
        printf("\n"); prettyText("*"); printf("*v0.1\n"
98
                "*\t****MoteCtrl****     written by Alpin\n"
99
                "*\tlets you control your MikroKopter with a Wiimote!\n*\n");
100
        prettyText("*"); printf("\n");
101
 
102
 
103
        //**********Serial COM Port init & config: 
104
        prettyText("COM Port init");
105
        printf("Serial COM Port you want to use to connect to the MikroKopter\n"
106
                "(only type the number then hit enter):\n");
107
 
108
        scanf_s("%d", &tmp);    //read a decimal from terminal
109
 
110
        tmp1 = init_com_port(tmp);      //initialize & config COM port tmp
111
 
112
        //init_com_port returned successfull?
113
        if( tmp1 == EXIT_FAILURE ) {
114
                printf("\n***COM%i Initilizaition/Configuration failed\n\n", tmp);
115
                prettyText("COM Port Init FAILED!");
116
                printf("exiting MoteCtrl...\n");
117
                Sleep(1000);
118
                return EXIT_FAILURE;
119
        }
120
        else { prettyText("Init & config of COM DONE!"); printf("\n\n"); }
121
 
122
 
123
        //**********Wiimote init & config: 
124
        prettyText("Wiimote Init");
125
        Sleep(1000);
126
 
127
        tmp = init_wiimote ();          //initialize wiimote
128
 
129
        //was init_wiimote successfull?
130
        if (tmp == EXIT_FAILURE ) {
131
                prettyText("Wiimote Init FAILED!");
132
                printf("exiting MoteCtrl...\n");
133
                Sleep(1000);
134
                return EXIT_FAILURE;
135
        }
136
        else { prettyText("Wiimote Init DONE!"); printf("\n\n"); }
137
 
138
        /* Does not work yet:
139
        if(mote[0]->exp.type == EXP_NUNCHUK) {printf("Nunchuck plugged in!"); }
140
        else { printf("No Nunchuck extension found!"); }
141
        */
142
 
143
 
144
        /***********Main: reading acc-values of wiimote, sending them as external-
145
        control data to the MikroKopter's FlightControl: */
146
 
147
        /* This is the Core of MotionCtrl, since it transfers the Wiimote data via the
148
        external control srtuct and the serial protocol encoding to the
149
        FlightCtrl.*/
150
 
151
        //Initialize entire ExternControl-struct with zeros
152
        SecureZeroMemory(&ExternControl, sizeof(struct str_ExternControl));
153
 
154
        //Activate the ExternalControl
155
        //gas = 0;      //init gas value
156
        ExternControl.Config = 1;
157
 
158
 
159
        //Main loop, "virtual" polling the wiimote events:
160
        while (1) {
161
                //wiiuse_poll returns the number of wiimotes, at which an event occured:
162
                if (wiiuse_poll(mote, MAX_WIIMOTES)) {
163
                        switch (mote[0]->event) {       //what type of event happened?
164
 
165
                                case WIIUSE_EVENT:
166
                                        //a generic event occured!
167
                                        /*Call function, which does the following: transferring
168
                                        the wiimote data to the ExternalControl struct: */
169
                                        handle_event(mote[0]);
170
 
171
                                        //Send the (by handle_event) updated ExternControl struct
172
                                        //to the FlightCtrl:
173
                                        SendOutData('b', 1, (unsigned char *) &ExternControl, sizeof(struct str_ExternControl));
174
                                        break;
175
 
176
                                default:
177
                                        break;
178
                        }
179
                }
180
        }
181
 
182
/* Some early tests
183
 
184
 
185
        muh:
186
        i=0;
187
        while (1) {
188
                //externcon typedeffen
189
        SendOutData('b', 1, (unsigned char *) &ExternControl, sizeof(struct str_ExternControl));
190
                //i++;
191
                }
192
        //sendStringToCom("#bR@T\r",6);         //send reset
193
        i=0;
194
        while(i<255) {
195
                tmp = getCharFromCom(&b);
196
                if (tmp) {
197
                        printf("%c", b);
198
                        i++;
199
                }
200
        }
201
        goto muh;
202
 
203
*/
204
 
205
 
206
        return EXIT_SUCCESS;
207
}