├── CREDITS
├── LICENSE.TXT
├── README.md
└── coptermanager
├── a7105.cpp
├── a7105.h
├── common.h
├── config.h
├── coptermanager.ino
├── diagnostics.cpp
├── diagnostics.h
├── hubsan.cpp
├── hubsan.h
├── macros.h
├── manager.cpp
├── manager.h
└── session.h
/CREDITS:
--------------------------------------------------------------------------------
1 | # CREDITS
2 |
3 | * PhracturedBlue for creating the deviation firmware and reverse engineering the hubsan protocol: https://bitbucket.org/PhracturedBlue/deviation
4 | * shinyshez for the Arduino/A7105 instructions and original code: http://www.instructables.com/id/Easy-Android-controllable-PC-Interfaceable-Relati/
5 | * Cass May for 4 wire SPI mode instructions and code: https://github.com/cassm/a7105
6 | * Prof. G.K. Egan (gke) and goebish for decoding telemetry data: https://github.com/gke/TxAdapter_gke & https://github.com/goebish
7 |
--------------------------------------------------------------------------------
/LICENSE.TXT:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 |
635 | Copyright (C)
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | Copyright (C)
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Coptermanager
2 |
3 | A set of applications to program your quadrocopters. Contains code for the transmitter station (arduino board with A7105 chip), a server application to control your drones in the browser and execute custom code using a JavaScript API, and a node.js package for connecting to a local or remote transmitter station.
4 |
5 | ## Overview
6 |
7 | * [coptermanager-arduino](https://github.com/andihit/coptermanager-arduino): arduino application which communicates with the quadrocopters
8 | * [coptermanager_server](https://github.com/andihit/coptermanager_server): web interface and HTTP API for controlling multiple quadrocopters
9 | * [coptermanager-client](https://github.com/andihit/coptermanager-client): client library to control quadrocopters with javascript (node.js)
10 |
11 | ## Possible setups
12 |
13 | ### Variant 1: full stack solution
14 |
15 | This setup is recommended. It allows you to control multiple quadrocopters with just a single arduino board and transmitter chip. You can open the webinterface and start programming right away, inside the browser. Furthermore you can connect other apps to the HTTP API (e.g. apps for mobile devices).
16 |
17 | ### Variant 2: coptermanager-arduino and coptermanager-client
18 |
19 | It is also possible to talk directly from the client to the arduino board. The JavaScript API is identical to variant 1.
20 |
21 | ## Setup instructions
22 |
23 | 1. Follow the [instructions to build the transmitter station](http://www.instructables.com/id/Easy-Android-controllable-PC-Interfaceable-Relati/step5/Building-the-Arduino-driven-radio/) with a small modification:
24 | * instead of 3 wire SPI use 4 wire SPI:
25 | * skip step 10 "Put an additional wire from the 'SDIO' pin of the A7105 to the 'MISO' pin of the due."
26 | * **instead wire 'gio1' of the A7105 chip to the 'MISO' port of the arduino due**
27 | * test some resistor values (as written in the tutorial) - mine didn't work with 22K Ohm, but it works with 10K Ohm
28 |
29 | 2. Open the source code of this project and send it to your arduino board
30 |
31 | ### Pictures
32 |
33 | 
34 | 
35 | (Note: the floppy cable isn't required - I didn't have any male-to-female cables for the SPI port of the arduino due so I had to improvise...)
36 |
37 | ## Documentation
38 |
39 | ### Protocol
40 |
41 | **coptermanager-arduino** listens on the serial port for new commands. The protocol is very simple: each command contains 3 bytes - `copterid`, `command` and `value`. The response is always a one byte.
42 |
43 | #### List of commands:
44 |
45 | | Name | Code | Command value | Description |
46 | | ------------------ | ---- | ---------------------------------- | ------------------------------------------------------------------------------------------------------------------- |
47 | | COPTER_BIND | 0x01 | copter type (see below) | Initiate binding process. Timing is very important here, so during this time (max. 500ms) the processor is blocked. Return values the copterid |
48 | | COPTER_THROTTLE | 0x02 | throttle value (range 0x00 - 0xFF) | set throttle (top/down) value |
49 | | COPTER_RUDDER | 0x03 | rudder value (range 0x34 - 0xCC) | set rudder (rotate left/right) value |
50 | | COPTER_AILERON | 0x04 | aileron value (range 0x45 - 0xC3) | set aileron (drift left/right) value |
51 | | COPTER_ELEVATOR | 0x05 | elevator value (range 0x3E - 0xBC) | set elevator (forward/backward) value |
52 | | COPTER_LED | 0x06 | enable or disable (1 or 0) | enable/disable leds |
53 | | COPTER_FLIP | 0x07 | enable or disable (1 or 0) | enable/disable flips |
54 | | COPTER_VIDEO | 0x08 | enable or disable (1 or 0) | enable/disable video (if the copter has a camera) |
55 | | COPTER_GETSTATE | 0x09 | - | get binding state |
56 | | COPTER_EMERGENCY | 0x0A | - | set emergency mode on: in this mode all values are set to default (the copter will fall down like a rock) and no further commands will be accepted (except COPTER_DISCONNECT) |
57 | | COPTER_DISCONNECT | 0x0B | - | disconnect copter |
58 | | COPTER_LISTCOPTERS | 0x0C | - | get bitmask of connected copters (LSB = copterid 1, bit 1 = copterid 2, ...) |
59 |
60 | #### List of copter types:
61 |
62 | | Name | Code | Description |
63 | | --------- | ---- | ----------------------------------------------------------------------------- |
64 | | HUBSAN_X4 | 0x01 | The Hubsan X4 (tested with the V2, but should work with V1 and maybe FPV too) |
65 |
66 | #### List of response codes:
67 |
68 | | Name | Code | Description |
69 | | ---------------------------- | ---- | --------------------------------------------------------------------- |
70 | | PROTOCOL_OK | 0x00 | Success |
71 | | PROTOCOL_UNBOUND | 0xE0 | Copter is unbound |
72 | | PROTOCOL_BOUND | 0xE1 | Copter is bound |
73 | | PROTOCOL_INVALID_COPTER_TYPE | 0xF0 | Invalid copter type. See table above on valid copter types |
74 | | PROTOCOL_ALL_SLOTS_FULL | 0xF1 | All copter slots are full. Configurable inside config.h `NUM_COPTERS` |
75 | | PROTOCOL_INVALID_SLOT | 0xF2 | Invalid copterid. Range 1 - `NUM_COPTERS` |
76 | | PROTOCOL_VALUE_OUT_OF_RANGE | 0xF3 | Value out of range |
77 | | PROTOCOL_EMERGENCY_MODE_ON | 0xF4 | If emergency mode is on, only the disconnect command is supported |
78 | | PROTOCOL_UNKNOWN_COMMAND | 0xF5 | Unkown command |
79 |
80 | #### Example session:
81 |
82 | ```
83 | > 0x00 0x01 0x01 # bind a hubsan x4
84 | < 0x01 # assigned copterid is 0x01
85 | > 0x01 0x02 0x0F # set throttle of copter #1 to 0x0F
86 | < 0x00 # success
87 | > 0x01 0x06 0x00 # set of copter #1 off
88 | < 0x00 # success
89 | > 0x01 0x0B 0x00 # disconnect copter #1
90 | < 0x00 # success
91 | ```
92 |
93 | In this session `>` indicates a request and `<` the response. The protocol is binary: spaces are just for readability, and values must be transmitted as their value and *not* as ASCII strings.
94 |
95 | #### Debugging:
96 |
97 | In `config.h` there are 2 debug modes available: `DEBUG` and `SERIAL_ASCII`. The first one enables debug messages and the last one enables write and read operations as ASCII strings. With `SERIAL_ASCII` turned on the requests are in the format `AABBCC` where `AA` is the copterid, `BB` the command and `CC` the command value in hexadecimal format with ASCII characters (don't prefix them with `0x`!). Example request: `000101` (transmitted as ASCII characters, e.g. wrote in the arduino IDE's serial monitor, with no line ending) to bind the hubsan.
98 |
--------------------------------------------------------------------------------
/coptermanager/a7105.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | This project is free software: you can redistribute it and/or modify
3 | it under the terms of the GNU General Public License as published by
4 | the Free Software Foundation, either version 3 of the License, or
5 | (at your option) any later version.
6 |
7 | Deviation is distributed in the hope that it will be useful,
8 | but WITHOUT ANY WARRANTY; without even the implied warranty of
9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 | GNU General Public License for more details.
11 |
12 | You should have received a copy of the GNU General Public License
13 | along with Deviation. If not, see .
14 | */
15 |
16 | #include
17 | #include
18 | #include "a7105.h"
19 | #include "macros.h"
20 |
21 | #define PROTOSPI_xfer(x) SPI.transfer(x)
22 | #define PROTOSPI_read3wire() SPI.transfer(0)
23 |
24 | inline static void CS_HI() {
25 | digitalWrite(CS_PIN, HIGH);
26 | }
27 |
28 | inline static void CS_LO() {
29 | digitalWrite(CS_PIN, LOW);
30 | }
31 |
32 | // Set CS pin mode, initialse and set sensible defaults for SPI, set GIO1 as output on chip
33 | void A7105_Setup() {
34 | // initialise the cs lock pin
35 | pinMode(CS_PIN, OUTPUT);
36 |
37 | // initialise SPI, set mode and byte order
38 | SPI.begin();
39 | SPI.setDataMode(SPI_MODE0);
40 | SPI.setBitOrder(MSBFIRST);
41 |
42 | // set GIO1 to SDO (MISO) by writing to reg GIO1S
43 | // (this instructs the chip to use GIO1 as the output pin)
44 | A7105_WriteReg(A7105_0B_GPIO1_PIN1, 0x19); // 0b11001
45 | }
46 |
47 | void A7105_WriteReg(u8 address, u8 data)
48 | {
49 | CS_LO();
50 | PROTOSPI_xfer(address);
51 | PROTOSPI_xfer(data);
52 | CS_HI();
53 | }
54 |
55 | u8 A7105_ReadReg(u8 address)
56 | {
57 | u8 data;
58 | CS_LO();
59 | PROTOSPI_xfer(0x40 | address);
60 | /* Wait for tx completion before spi shutdown */
61 | data = PROTOSPI_read3wire();
62 | CS_HI();
63 | return data;
64 | }
65 |
66 | void A7105_WriteData(u8 *dpbuffer, u8 len, u8 channel)
67 | {
68 | int i;
69 | CS_LO();
70 | PROTOSPI_xfer(A7105_RST_WRPTR);
71 | PROTOSPI_xfer(0x05);
72 | for (i = 0; i < len; i++)
73 | PROTOSPI_xfer(dpbuffer[i]);
74 | CS_HI();
75 |
76 | A7105_WriteReg(0x0F, channel);
77 |
78 | CS_LO();
79 | PROTOSPI_xfer(A7105_TX);
80 | CS_HI();
81 | }
82 | void A7105_ReadData(u8 *dpbuffer, u8 len)
83 | {
84 | A7105_Strobe(A7105_RST_RDPTR); //A7105_RST_RDPTR
85 | for(int i = 0; i < len; i++)
86 | dpbuffer[i] = A7105_ReadReg(0x05);
87 | return;
88 | }
89 |
90 | /*
91 | * we use 4 wire SPI mode (gio1 as output pin)
92 | */
93 | void A7105_SetTxRxMode(enum TXRX_State mode)
94 | {
95 | }
96 |
97 | int A7105_Reset()
98 | {
99 | // this writes a null value to register 0x00, which triggers the reset
100 | A7105_WriteReg(0x00, 0x00);
101 | usleep(1000);
102 | // set gpio1 to SDO (MISO) by writing to reg GIO1S
103 | A7105_WriteReg(A7105_0B_GPIO1_PIN1, 0x19); // 0b11001
104 | A7105_SetTxRxMode(TXRX_OFF);
105 | int result = A7105_ReadReg(0x10) == 0x9E;
106 | A7105_Strobe(A7105_STANDBY);
107 | return result;
108 | }
109 |
110 | void A7105_WriteID(u32 id)
111 | {
112 | CS_LO();
113 | PROTOSPI_xfer(0x06);
114 | PROTOSPI_xfer((id >> 24) & 0xFF);
115 | PROTOSPI_xfer((id >> 16) & 0xFF);
116 | PROTOSPI_xfer((id >> 8) & 0xFF);
117 | PROTOSPI_xfer((id >> 0) & 0xFF);
118 | CS_HI();
119 | }
120 |
121 | void A7105_SetPower(int power)
122 | {
123 | /*
124 | Power amp is ~+16dBm so:
125 | TXPOWER_100uW = -23dBm == PAC=0 TBG=0
126 | TXPOWER_300uW = -20dBm == PAC=0 TBG=1
127 | TXPOWER_1mW = -16dBm == PAC=0 TBG=2
128 | TXPOWER_3mW = -11dBm == PAC=0 TBG=4
129 | TXPOWER_10mW = -6dBm == PAC=1 TBG=5
130 | TXPOWER_30mW = 0dBm == PAC=2 TBG=7
131 | TXPOWER_100mW = 1dBm == PAC=3 TBG=7
132 | TXPOWER_150mW = 1dBm == PAC=3 TBG=7
133 | */
134 | u8 pac, tbg;
135 | switch(power) {
136 | case 0: pac = 0; tbg = 0; break;
137 | case 1: pac = 0; tbg = 1; break;
138 | case 2: pac = 0; tbg = 2; break;
139 | case 3: pac = 0; tbg = 4; break;
140 | case 4: pac = 1; tbg = 5; break;
141 | case 5: pac = 2; tbg = 7; break;
142 | case 6: pac = 3; tbg = 7; break;
143 | case 7: pac = 3; tbg = 7; break;
144 | default: pac = 0; tbg = 0; break;
145 | };
146 | A7105_WriteReg(0x28, (pac << 3) | tbg);
147 | }
148 |
149 | void A7105_Strobe(enum A7105_State state)
150 | {
151 | CS_LO();
152 | PROTOSPI_xfer(state);
153 | CS_HI();
154 | }
155 |
--------------------------------------------------------------------------------
/coptermanager/a7105.h:
--------------------------------------------------------------------------------
1 | #ifndef A7105_H
2 | #define A7105_H
3 |
4 | #include "common.h"
5 |
6 | enum A7105_State {
7 | A7105_SLEEP = 0x80,
8 | A7105_IDLE = 0x90,
9 | A7105_STANDBY = 0xA0,
10 | A7105_PLL = 0xB0,
11 | A7105_RX = 0xC0,
12 | A7105_TX = 0xD0,
13 | A7105_RST_WRPTR = 0xE0,
14 | A7105_RST_RDPTR = 0xF0,
15 | };
16 |
17 | enum {
18 | A7105_00_MODE = 0x00,
19 | A7105_01_MODE_CONTROL = 0x01,
20 | A7105_02_CALC = 0x02,
21 | A7105_03_FIFOI = 0x03,
22 | A7105_04_FIFOII = 0x04,
23 | A7105_05_FIFO_DATA = 0x05,
24 | A7105_06_ID_DATA = 0x06,
25 | A7105_07_RC_OSC_I = 0x07,
26 | A7105_08_RC_OSC_II = 0x08,
27 | A7105_09_RC_OSC_III = 0x09,
28 | A7105_0A_CK0_PIN = 0x0A,
29 | A7105_0B_GPIO1_PIN1 = 0x0B,
30 | A7105_0C_GPIO2_PIN_II = 0x0C,
31 | A7105_0D_CLOCK = 0x0D,
32 | A7105_0E_DATA_RATE = 0x0E,
33 | A7105_0F_PLL_I = 0x0F,
34 | A7105_10_PLL_II = 0x10,
35 | A7105_11_PLL_III = 0x11,
36 | A7105_12_PLL_IV = 0x12,
37 | A7105_13_PLL_V = 0x13,
38 | A7105_14_TX_I = 0x14,
39 | A7105_15_TX_II = 0x15,
40 | A7105_16_DELAY_I = 0x16,
41 | A7105_17_DELAY_II = 0x17,
42 | A7105_18_RX = 0x18,
43 | A7105_19_RX_GAIN_I = 0x19,
44 | A7105_1A_RX_GAIN_II = 0x1A,
45 | A7105_1B_RX_GAIN_III = 0x1B,
46 | A7105_1C_RX_GAIN_IV = 0x1C,
47 | A7105_1D_RSSI_THOLD = 0x1D,
48 | A7105_1E_ADC = 0x1E,
49 | A7105_1F_CODE_I = 0x1F,
50 | A7105_20_CODE_II = 0x20,
51 | A7105_21_CODE_III = 0x21,
52 | A7105_22_IF_CALIB_I = 0x22,
53 | A7105_23_IF_CALIB_II = 0x23,
54 | A7105_24_VCO_CURCAL = 0x24,
55 | A7105_25_VCO_SBCAL_I = 0x25,
56 | A7105_26_VCO_SBCAL_II = 0x26,
57 | A7105_27_BATTERY_DET = 0x27,
58 | A7105_28_TX_TEST = 0x28,
59 | A7105_29_RX_DEM_TEST_I = 0x29,
60 | A7105_2A_RX_DEM_TEST_II = 0x2A,
61 | A7105_2B_CPC = 0x2B,
62 | A7105_2C_XTAL_TEST = 0x2C,
63 | A7105_2D_PLL_TEST = 0x2D,
64 | A7105_2E_VCO_TEST_I = 0x2E,
65 | A7105_2F_VCO_TEST_II = 0x2F,
66 | A7105_30_IFAT = 0x30,
67 | A7105_31_RSCALE = 0x31,
68 | A7105_32_FILTER_TEST = 0x32,
69 | };
70 | #define A7105_0F_CHANNEL A7105_0F_PLL_I
71 |
72 | enum A7105_MASK {
73 | A7105_MASK_FBCF = 1 << 4,
74 | A7105_MASK_VBCF = 1 << 3,
75 | };
76 |
77 | void A7105_Setup();
78 | void A7105_Initialize();
79 | void A7105_WriteReg(u8 addr, u8 value);
80 | void A7105_WriteData(u8 *dpbuffer, u8 len, u8 channel);
81 | void A7105_ReadData(u8 *dpbuffer, u8 len);
82 | u8 A7105_ReadReg(u8 addr);
83 | int A7105_Reset();
84 | void A7105_WriteID(u32 id);
85 | void A7105_Strobe(enum A7105_State);
86 | void A7105_SetPower(int power);
87 | void A7105_SetTxRxMode(enum TXRX_State);
88 |
89 |
90 | #endif
91 |
--------------------------------------------------------------------------------
/coptermanager/common.h:
--------------------------------------------------------------------------------
1 | #ifndef COMMON_H
2 | #define COMMON_H
3 |
4 | #include
5 | #include "config.h"
6 |
7 | #ifdef DEBUG
8 | #define DEBUG_MSG(x) Serial.println (x)
9 | #else
10 | #define DEBUG_MSG(x) (void)0
11 | #endif
12 |
13 | typedef int8_t s8;
14 | typedef int16_t s16;
15 | typedef int32_t s32;
16 | typedef uint8_t u8;
17 | typedef uint16_t u16;
18 | typedef uint32_t u32;
19 | typedef uint64_t u64;
20 |
21 | enum TxPower {
22 | TXPOWER_100uW,
23 | TXPOWER_300uW,
24 | TXPOWER_1mW,
25 | TXPOWER_3mW,
26 | TXPOWER_10mW,
27 | TXPOWER_30mW,
28 | TXPOWER_100mW,
29 | TXPOWER_150mW,
30 | TXPOWER_LAST,
31 | };
32 |
33 | enum TXRX_State {
34 | TXRX_OFF,
35 | TX_EN,
36 | RX_EN,
37 | };
38 |
39 | #define NUM_PROTO_OPTS 4
40 |
41 | struct Model {
42 | s16 proto_opts[NUM_PROTO_OPTS];
43 | enum TxPower tx_power;
44 | };
45 |
46 | #endif
47 |
--------------------------------------------------------------------------------
/coptermanager/config.h:
--------------------------------------------------------------------------------
1 | #ifndef CONFIG_H
2 | #define CONFIG_H
3 |
4 | // SPI CS PIN
5 | #define CS_PIN 10
6 |
7 | // serial port baudrate
8 | #define BAUDRATE 115200
9 |
10 | // number of copter slots, max. 8 (otherwise COPTER_LISTCOPTERS command won't work)
11 | #define NUM_COPTERS 8
12 |
13 | // max time of unbound state in ms
14 | #define MAX_UNBOUND_TIME 1000*60
15 |
16 | // max time of bound state in ms (should be greater than akku capacity)
17 | #define MAX_BOUND_TIME 1000*60*10
18 |
19 | //#define DEBUG
20 | //#define DEBUG_TELEMETRY
21 | //#define SERIAL_ASCII
22 | //#define FIXED_CHANNEL 0x6E
23 |
24 | #endif
25 |
--------------------------------------------------------------------------------
/coptermanager/coptermanager.ino:
--------------------------------------------------------------------------------
1 | // some .ino file must include the SPI library, otherwise it's not available inside a7105.cpp
2 | #include
3 | #include "common.h"
4 | #include "manager.h"
5 | #include "diagnostics.h"
6 |
7 | void setup()
8 | {
9 | pinMode(13, OUTPUT);
10 | digitalWrite(13, LOW);
11 | manager_init();
12 | delay(500);
13 | digitalWrite(13, HIGH);
14 | delay(500);
15 | digitalWrite(13, LOW);
16 | }
17 |
18 | int hex2int(byte *arr, int startpos, int endpos)
19 | {
20 | int base = 1;
21 | int ret = 0;
22 | for(int i = endpos; i >= startpos; i--) {
23 | int val = 0;
24 | if (arr[i] >= 'A' && arr[i] <= 'F')
25 | val = 10 + (arr[i] - 'A');
26 | else if (arr[i] >= 'a' && arr[i] <= 'f')
27 | val = 10 + (arr[i] - 'a');
28 | else if (arr[i] >= '0' && arr[i] <= '9')
29 | val = arr[i] - '0';
30 |
31 | ret += val * base;
32 | base *= 16;
33 | }
34 | return ret;
35 | }
36 |
37 | void send_response(int result_code, int* response)
38 | {
39 | #ifdef SERIAL_ASCII
40 | Serial.print("command result ");
41 | Serial.print(String(result_code, HEX));
42 | for (int i=1; i <= response[0]; i++) {
43 | Serial.print(" "+String(response[i], HEX));
44 | }
45 | Serial.println();
46 | #else
47 | int sum = result_code;
48 | Serial.write(result_code);
49 | for (int i=1; i <= response[0]; i++) {
50 | Serial.write(response[i]);
51 | sum += response[i];
52 | }
53 | Serial.write((256 - (sum % 256)) & 0xFF);
54 | #endif
55 | }
56 |
57 | int response[7] = {0};
58 | void loop()
59 | {
60 | #ifdef SERIAL_ASCII
61 | if (Serial.available() >= 6) {
62 | byte data[6];
63 | Serial.readBytes(data, 6);
64 | int copterid = hex2int(data, 0, 1);
65 | int command = hex2int(data, 2, 3);
66 | int value = hex2int(data, 4, 5);
67 | int checksum = calculate_checksum(copterid, command, value); // don't use checksum when SERIAL_ASCII is enabled
68 | #else
69 | if (Serial.available() >= 4) {
70 | int copterid = Serial.read();
71 | int command = Serial.read();
72 | int value = Serial.read();
73 | int checksum = Serial.read();
74 | #endif
75 |
76 | DEBUG_MSG("read values " + String(copterid) + " " + String(command) + " " + String(value));
77 | int result_code = manager_processcommand(copterid, command, value, checksum, response);
78 | send_response(result_code, response);
79 | }
80 |
81 | manager_loop();
82 | }
83 |
--------------------------------------------------------------------------------
/coptermanager/diagnostics.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | Thanks to Cass May, https://github.com/cassm/a7105
3 | */
4 |
5 | #include
6 | #include "diagnostics.h"
7 | #include "a7105.h"
8 | #include "hubsan.h"
9 |
10 | u8 receivedpacket[16];
11 | u8 no_allowed_channels = sizeof(allowed_channels)/sizeof(allowed_channels[0]);
12 |
13 | // print the contents of packet in a human-readable way
14 | void printpacket(u8 packet[]) {
15 | int j;
16 | Serial.print("Packet received: ");
17 | for (j = 0 ; j < 16 ; j++) {
18 | if (packet[j] < 10)
19 | Serial.print("0");
20 | Serial.print(packet[j], HEX);
21 | Serial.print(" ");
22 | }
23 | Serial.println("");
24 | }
25 |
26 | // Eavesdrop on a hubsan exchange. This must be started prior to the binding exchange.
27 | void eavesdrop() {
28 | u8 prebind_packet[16];
29 | int wait_start, wait_end;
30 |
31 | Serial.println("Eavesdropping...");
32 |
33 | // use findchannel to locate the channel which is currently being broadcast on
34 | u8 sess_channel = A7105_findchannel();
35 |
36 | // strobe to receiver mode, intercept a packet
37 | A7105_Strobe(A7105_RX);
38 | while(A7105_ReadReg(A7105_00_MODE) & 0x01)
39 | delayMicroseconds(1);
40 | A7105_ReadData(prebind_packet, 16);
41 |
42 | // use the data from the packet to switch the chip over to the transactions session ID
43 | A7105_WriteID((prebind_packet[2] << 24) | (prebind_packet[3] << 16) | (prebind_packet[4] << 8) | prebind_packet[5]);
44 |
45 | // It is now acceptable to allow the Tx to bind with an Rx
46 |
47 | // measure haw long it takes for the next packet to arrive
48 | wait_start = micros();
49 | while(true) {
50 | A7105_Strobe(A7105_RX);
51 | while(A7105_ReadReg(A7105_00_MODE) & 0x01) {
52 | delayMicroseconds(1);
53 |
54 | // if it takes more than 5 seconds, we can assume that the session has been terminated
55 | if ((micros()-wait_start) > 5000000) {
56 | Serial.println("Session terminated. Rescanning...");
57 | // ...and therefore start listening for pre-bind packets again
58 | return;
59 | }
60 | }
61 |
62 | // record the end time, retrieve the packet
63 | wait_end = micros();
64 | A7105_ReadData(receivedpacket, 16);
65 |
66 | // print the packet along with the time interval between it and the previous packet
67 | int timeinterval = (wait_end - wait_start)/1000;
68 | if (timeinterval < 10)
69 | Serial.print("0");
70 | Serial.print(timeinterval);
71 |
72 | // start timing for the next packet
73 | wait_start = micros();
74 | Serial.print("us: ");
75 | printpacket(receivedpacket);
76 | }
77 | }
78 |
79 | // find which channel the binding exchange is taking place on
80 | u8 A7105_findchannel() {
81 | int pack_count;
82 | while (true) {
83 | // scan all allowed channels
84 | for (int i = 0 ; i < no_allowed_channels ; i++) {
85 | pack_count = 0;
86 | A7105_WriteReg(A7105_0F_CHANNEL, allowed_channels[i]);
87 |
88 | // Listen on channel
89 | for (int j = 0 ; j < 20 ; j++)
90 | pack_count += A7105_sniffchannel();
91 |
92 | // if channel has more than 3 packets, assume channel is correct
93 | if (pack_count > 3) {
94 | Serial.println("Channel found: "+String(allowed_channels[i], HEX));
95 | return allowed_channels[i];
96 | }
97 | }
98 | }
99 | }
100 |
101 | // sniffs the currently set channel, prints packets to serial
102 | int A7105_sniffchannel() {
103 | // put chip in receiver mode
104 | A7105_Strobe(A7105_RX);
105 |
106 | // wait for packet to arrive
107 | delayMicroseconds(3000);
108 |
109 | // if flag is high, buffer has filled, meaning a packet has been intercepted
110 | if(!(A7105_ReadReg(A7105_00_MODE) & 0x01)) {
111 |
112 | // accept and print packet
113 | A7105_ReadData(receivedpacket, 16);
114 | printpacket(receivedpacket);
115 | return 1;
116 | }
117 | else
118 | return 0;
119 | }
120 |
121 | // version of sniffchannel which sniffs a channel other than the one which is currently set.
122 | void A7105_sniffchannel(u8 _channel) {
123 | Serial.print("Switching to channel ");
124 | Serial.println(_channel);
125 | Serial.println("");
126 | A7105_WriteReg(A7105_0F_CHANNEL, _channel);
127 | while (1) {
128 | A7105_sniffchannel();
129 | }
130 | }
131 |
132 | // function to sniff a list of channels and see what is being broadcast on them
133 | // attempts sniffing 20 times on each channel before looping, print any results to serial
134 | void A7105_scanchannels(const u8 channels[]) {
135 | int packetsreceived;
136 | for (int i = 0 ; i < no_allowed_channels ; i++) {
137 | packetsreceived = 0;
138 | Serial.println("");
139 | Serial.print("Switching to channel ");
140 | Serial.println(channels[i], HEX);
141 | Serial.println("");
142 | A7105_WriteReg(A7105_0F_CHANNEL, channels[i]);
143 | for (int j = 0 ; j < 20 ; j++) {
144 | packetsreceived += A7105_sniffchannel();
145 | }
146 | if (packetsreceived) {
147 | Serial.print(packetsreceived);
148 | Serial.print(" packets received on channel ");
149 | Serial.println(channels[i], HEX);
150 | }
151 | else {
152 | Serial.print("Channel ");
153 | Serial.print(channels[i], HEX);
154 | Serial.println(" is clear.");
155 | }
156 | }
157 | }
158 |
--------------------------------------------------------------------------------
/coptermanager/diagnostics.h:
--------------------------------------------------------------------------------
1 | #ifndef DIAGNOSTICS_H
2 | #define DIAGNOSTICS_H
3 |
4 | #include "common.h"
5 |
6 | void printpacket(u8 packet[]);
7 | void eavesdrop();
8 | u8 A7105_findchannel();
9 | int A7105_sniffchannel();
10 | void A7105_sniffchannel(u8 _channel);
11 | void A7105_scanchannels(const u8 channels[]);
12 |
13 | #endif
14 |
--------------------------------------------------------------------------------
/coptermanager/hubsan.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | This project is free software: you can redistribute it and/or modify
3 | it under the terms of the GNU General Public License as published by
4 | the Free Software Foundation, either version 3 of the License, or
5 | (at your option) any later version.
6 |
7 | Deviation is distributed in the hope that it will be useful,
8 | but WITHOUT ANY WARRANTY; without even the implied warranty of
9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 | GNU General Public License for more details.
11 |
12 | You should have received a copy of the GNU General Public License
13 | along with Deviation. If not, see .
14 | */
15 |
16 | #include
17 | #include "hubsan.h"
18 | #include "common.h"
19 | #include "macros.h"
20 | #include "a7105.h"
21 |
22 | static struct Model Model = {
23 | {0, 0, 0, 0},
24 | TXPOWER_10mW
25 | };
26 |
27 | #define TELEM_ON 0
28 | #define TELEM_OFF 1
29 |
30 | enum{
31 | FLAG_VIDEO= 0x01, // record video
32 | FLAG_FLIP = 0x08, // enable flips
33 | FLAG_LED = 0x04 // enable LEDs
34 | };
35 |
36 | #define VTX_STEP_SIZE "5"
37 |
38 | enum {
39 | PROTOOPTS_VTX_FREQ = 0,
40 | PROTOOPTS_TELEMETRY,
41 | LAST_PROTO_OPT,
42 | };
43 |
44 | enum {
45 | BIND_1,
46 | BIND_2,
47 | BIND_3,
48 | BIND_4,
49 | BIND_5,
50 | BIND_6,
51 | BIND_7,
52 | BIND_8,
53 | DATA_1,
54 | DATA_2,
55 | DATA_3,
56 | DATA_4,
57 | DATA_5,
58 | };
59 | #define WAIT_WRITE 0x80
60 |
61 | static int hubsan_init()
62 | {
63 | u8 if_calibration1;
64 | u8 vco_calibration0;
65 | u8 vco_calibration1;
66 | //u8 vco_current;
67 |
68 | A7105_WriteID(0x55201041);
69 | A7105_WriteReg(A7105_01_MODE_CONTROL, 0x63);
70 | A7105_WriteReg(A7105_03_FIFOI, 0x0f);
71 | A7105_WriteReg(A7105_0D_CLOCK, 0x05);
72 | A7105_WriteReg(A7105_0E_DATA_RATE, 0x04);
73 | A7105_WriteReg(A7105_15_TX_II, 0x2b);
74 | A7105_WriteReg(A7105_18_RX, 0x62);
75 | A7105_WriteReg(A7105_19_RX_GAIN_I, 0x80);
76 | A7105_WriteReg(A7105_1C_RX_GAIN_IV, 0x0A);
77 | A7105_WriteReg(A7105_1F_CODE_I, 0x07);
78 | A7105_WriteReg(A7105_20_CODE_II, 0x17);
79 | A7105_WriteReg(A7105_29_RX_DEM_TEST_I, 0x47);
80 |
81 | A7105_Strobe(A7105_STANDBY);
82 |
83 | //IF Filter Bank Calibration
84 | A7105_WriteReg(0x02, 1);
85 | //vco_current =
86 | A7105_ReadReg(0x02);
87 | u32 ms = CLOCK_getms();
88 | CLOCK_ResetWatchdog();
89 | while(CLOCK_getms() - ms < 500) {
90 | if(! A7105_ReadReg(0x02))
91 | break;
92 | }
93 | if (CLOCK_getms() - ms >= 500) {
94 | DEBUG_MSG("calibration failed");
95 | return 0;
96 | }
97 | if_calibration1 = A7105_ReadReg(A7105_22_IF_CALIB_I);
98 | A7105_ReadReg(A7105_24_VCO_CURCAL);
99 | if(if_calibration1 & A7105_MASK_FBCF) {
100 | //Calibration failed...what do we do?
101 | return 0;
102 | }
103 |
104 | //VCO Current Calibration
105 | //A7105_WriteReg(0x24, 0x13); //Recomended calibration from A7105 Datasheet
106 |
107 | //VCO Bank Calibration
108 | //A7105_WriteReg(0x26, 0x3b); //Recomended limits from A7105 Datasheet
109 |
110 | //VCO Bank Calibrate channel 0?
111 | //Set Channel
112 | A7105_WriteReg(A7105_0F_CHANNEL, 0);
113 | //VCO Calibration
114 | A7105_WriteReg(0x02, 2);
115 | ms = CLOCK_getms();
116 | CLOCK_ResetWatchdog();
117 | while(CLOCK_getms() - ms < 500) {
118 | if(! A7105_ReadReg(0x02))
119 | break;
120 | }
121 | if (CLOCK_getms() - ms >= 500) {
122 | return 0;
123 | }
124 | vco_calibration0 = A7105_ReadReg(A7105_25_VCO_SBCAL_I);
125 | if (vco_calibration0 & A7105_MASK_VBCF) {
126 | //Calibration failed...what do we do?
127 | return 0;
128 | }
129 |
130 | //Calibrate channel 0xa0?
131 | //Set Channel
132 | A7105_WriteReg(A7105_0F_CHANNEL, 0xa0);
133 | //VCO Calibration
134 | A7105_WriteReg(A7105_02_CALC, 2);
135 | ms = CLOCK_getms();
136 | CLOCK_ResetWatchdog();
137 | while(CLOCK_getms() - ms < 500) {
138 | if(! A7105_ReadReg(A7105_02_CALC))
139 | break;
140 | }
141 | if (CLOCK_getms() - ms >= 500)
142 | return 0;
143 | vco_calibration1 = A7105_ReadReg(A7105_25_VCO_SBCAL_I);
144 | if (vco_calibration1 & A7105_MASK_VBCF) {
145 | //Calibration failed...what do we do?
146 | }
147 |
148 | //Reset VCO Band calibration
149 | //A7105_WriteReg(0x25, 0x08);
150 | A7105_SetTxRxMode(TX_EN);
151 |
152 | A7105_SetPower(Model.tx_power);
153 |
154 |
155 | A7105_Strobe(A7105_STANDBY);
156 | return 1;
157 | }
158 |
159 | static void update_crc(HubsanSession *session)
160 | {
161 | int sum = 0;
162 | for(int i = 0; i < 15; i++)
163 | sum += session->packet[i];
164 | session->packet[15] = (256 - (sum % 256)) & 0xff;
165 | }
166 |
167 | static void hubsan_build_bind_packet(HubsanSession *session, u8 state)
168 | {
169 | session->packet[0] = state;
170 | session->packet[1] = session->channel;
171 | session->packet[2] = (session->sessionid >> 24) & 0xff;
172 | session->packet[3] = (session->sessionid >> 16) & 0xff;
173 | session->packet[4] = (session->sessionid >> 8) & 0xff;
174 | session->packet[5] = (session->sessionid >> 0) & 0xff;
175 | session->packet[6] = 0x08;
176 | session->packet[7] = 0xe4; //???
177 | session->packet[8] = 0xea;
178 | session->packet[9] = 0x9e;
179 | session->packet[10] = 0x50;
180 | session->packet[11] = (session->txid >> 24) & 0xff;
181 | session->packet[12] = (session->txid >> 16) & 0xff;
182 | session->packet[13] = (session->txid >> 8) & 0xff;
183 | session->packet[14] = (session->txid >> 0) & 0xff;
184 | update_crc(session);
185 | }
186 |
187 | static void hubsan_build_packet(HubsanSession *session)
188 | {
189 | static s16 vtx_freq = 0;
190 | memset(session->packet, 0, 16);
191 | if(vtx_freq != Model.proto_opts[PROTOOPTS_VTX_FREQ] || session->packet_count==100) // set vTX frequency (H107D)
192 | {
193 | vtx_freq = Model.proto_opts[PROTOOPTS_VTX_FREQ];
194 | session->packet[0] = 0x40;
195 | session->packet[1] = (vtx_freq >> 8) & 0xff;
196 | session->packet[2] = vtx_freq & 0xff;
197 | session->packet[3] = 0x82;
198 | session->packet_count++;
199 | }
200 | else //20 00 00 00 80 00 7d 00 84 02 64 db 04 26 79 7b
201 | {
202 | session->packet[0] = 0x20;
203 | session->packet[2] = session->throttle; //Throttle
204 | }
205 | session->packet[4] = 0xff - session->rudder; //Rudder is reversed
206 | session->packet[6] = 0xff - session->elevator; //Elevator is reversed
207 | session->packet[8] = session->aileron; //Aileron
208 | if(session->packet_count < 100)
209 | {
210 | session->packet[9] = 0x02 | FLAG_LED | FLAG_FLIP; // sends default value for the 100 first packets
211 | session->packet_count++;
212 | }
213 | else
214 | {
215 | session->packet[9] = 0x02;
216 | // Channel 5
217 | if(session->led)
218 | session->packet[9] |= FLAG_LED;
219 | // Channel 6
220 | if(session->flip >= 0)
221 | session->packet[9] |= FLAG_FLIP;
222 | // Channel 7
223 | if(session->video >0) // off by default
224 | session->packet[9] |= FLAG_VIDEO;
225 | }
226 | session->packet[10] = 0x64;
227 | session->packet[11] = (session->txid >> 24) & 0xff;
228 | session->packet[12] = (session->txid >> 16) & 0xff;
229 | session->packet[13] = (session->txid >> 8) & 0xff;
230 | session->packet[14] = (session->txid >> 0) & 0xff;
231 | update_crc(session);
232 | }
233 |
234 | static u8 hubsan_check_integrity(HubsanSession *session)
235 | {
236 | int sum = 0;
237 | for(int i = 0; i < 15; i++)
238 | sum += session->packet[i];
239 | return session->packet[15] == ((256 - (sum % 256)) & 0xff);
240 | }
241 |
242 | enum Tag0xe0 {
243 | TAG,
244 | ROC_MSB,
245 | ROC_LSB,
246 | XUNK3,
247 | XUNK4,
248 | XUNK5,
249 | XUNK6,
250 | XUNK7,
251 | XUNK8,
252 | Z_ACC_MSB,
253 | Z_ACC_LSB,
254 | YAW_GYRO_MSB,
255 | YAW_GYRO_LSB,
256 | VBAT,
257 | CRC0,
258 | CRC1
259 | };
260 |
261 | enum Tag0xe1 {
262 | TAG1,
263 | PITCH_ACC_MSB,
264 | PITCH_ACC_LSB,
265 | ROLL_ACC_MSB,
266 | ROLL_ACC_LSB,
267 | UNK5,
268 | UNK6,
269 | PITCH_GYRO_MSB,
270 | PITCH_GYRO_LSB,
271 | ROLL_GYRO_MSB,
272 | ROLL_GYRO_LSB,
273 | UNK11,
274 | UNK12,
275 | //VBAT,
276 | //CRC0,
277 | //CRC1
278 | };
279 |
280 | enum rc { // must be in this order
281 | ROLL,
282 | PITCH,
283 | YAW,
284 | THROTTLE,
285 | AUX1,
286 | AUX2,
287 | AUX3,
288 | AUX4
289 | };
290 |
291 | static void hubsan_update_telemetry(HubsanSession *session)
292 | {
293 | if(hubsan_check_integrity(session)) {
294 | switch (session->packet[0]) {
295 | case 0xE0:
296 | session->estAltitude = -(session->packet[ROC_MSB] << 8 | session->packet[ROC_LSB]);// 1,2
297 | //session->debug[0] = session->packet[3] << 8 | session->packet[4];
298 | //session->debug[1] = session->packet[5] << 8 | session->packet[6];
299 | //session->debug[2] = session->packet[7] << 8 | session->packet[8];
300 | session->accData[YAW] = session->packet[Z_ACC_MSB] << 8 | session->packet[Z_ACC_LSB]; // OK
301 | session->gyroData[YAW] = session->packet[YAW_GYRO_MSB] << 8 | session->packet[YAW_GYRO_LSB];
302 | // batteryVolts = session->packet[VBAT]; // only in 0xe1 Packet! (http://www.deviationtx.com/forum/protocol-development/1848-new-hubsan-upgraded-version-on-the-way?start=340#17668)
303 | break;
304 | case 0xE1:
305 | session->accData[PITCH] = session->packet[PITCH_ACC_MSB] << 8 | session->packet[PITCH_ACC_LSB];
306 | session->accData[ROLL] = session->packet[ROLL_ACC_MSB] << 8 | session->packet[ROLL_ACC_LSB];
307 |
308 | session->gyroData[PITCH] = session->packet[PITCH_GYRO_MSB] << 8 | session->packet[PITCH_GYRO_LSB];
309 | session->gyroData[ROLL] = session->packet[ROLL_GYRO_MSB] << 8 | session->packet[ROLL_GYRO_LSB];
310 |
311 | // use acc as angle alias
312 | session->angle[PITCH] = session->accData[PITCH];
313 | session->angle[ROLL] = -session->accData[ROLL];
314 | session->batteryVolts = session->packet[VBAT];
315 | break;
316 | default:
317 | break;
318 | }
319 |
320 | #ifdef DEBUG_TELEMETRY
321 | Serial.print ("Telemetry data: ");
322 | for(int i=0; i<16;i++) {
323 | if (session->packet[i] <= 15)
324 | Serial.print("0");
325 | Serial.print(session->packet[i], HEX);
326 | Serial.print(" ");
327 | }
328 |
329 | Serial.print (" Alt: " + String(session->estAltitude));
330 | Serial.print (", Battery: " + String(session->batteryVolts));
331 | Serial.print (", Gyro:");
332 | for(int i=0; i<3; i++) {
333 | Serial.print(" "+String(session->gyroData[i]));
334 | }
335 | Serial.print (", Acc:");
336 | for(int i=0; i<3; i++) {
337 | Serial.print(" "+String(session->accData[i]));
338 | }
339 | Serial.print (", Angle:");
340 | for(int i=0; i<3; i++) {
341 | Serial.print(" "+String(session->angle[i]));
342 | }
343 | Serial.println();
344 | #endif
345 | }
346 | }
347 |
348 | inline boolean A7105_busy(void) {
349 | return A7105_ReadReg(A7105_00_MODE) & 0x01;
350 | }
351 |
352 | u16 hubsan_cb(HubsanSession *session)
353 | {
354 | // odd stages: TX -> RX (sending)
355 | // even stages: RX -> TX (receiving)
356 |
357 | int i;
358 | switch(session->state) {
359 | case BIND_1:
360 | case BIND_3:
361 | case BIND_5:
362 | case BIND_7:
363 | hubsan_build_bind_packet(session, session->state == BIND_7 ? 9 : (session->state == BIND_5 ? 1 : session->state + 1 - BIND_1));
364 | A7105_Strobe(A7105_STANDBY);
365 | A7105_WriteData(session->packet, 16, session->channel);
366 | session->state |= WAIT_WRITE;
367 | return 3000;
368 | case BIND_1 | WAIT_WRITE:
369 | case BIND_3 | WAIT_WRITE:
370 | case BIND_5 | WAIT_WRITE:
371 | case BIND_7 | WAIT_WRITE:
372 | //wait for completion
373 | for(i = 0; i< 20; i++) {
374 | if(! (A7105_ReadReg(A7105_00_MODE) & 0x01))
375 | break;
376 | }
377 | if (i == 20)
378 | DEBUG_MSG("Failed to complete write\n");
379 | A7105_SetTxRxMode(RX_EN);
380 | A7105_Strobe(A7105_RX);
381 | session->state &= ~WAIT_WRITE;
382 | session->state++;
383 | return 4500; //7.5msec elapsed since last write
384 | case BIND_2:
385 | case BIND_4:
386 | case BIND_6:
387 | A7105_SetTxRxMode(TX_EN);
388 | if(A7105_ReadReg(A7105_00_MODE) & 0x01) {
389 | session->state = BIND_1;
390 | //DEBUG_MSG("No signal, restart binding procedure");
391 | return 4500; //No signal, restart binding procedure. 12msec elapsed since last write
392 | }
393 | A7105_ReadData(session->packet, 16);
394 | session->state++;
395 | if (session->state == BIND_5)
396 | A7105_WriteID((session->packet[2] << 24) | (session->packet[3] << 16) | (session->packet[4] << 8) | session->packet[5]);
397 |
398 | return 500; //8msec elapsed time since last write;
399 | case BIND_8:
400 | A7105_SetTxRxMode(TX_EN);
401 | if(A7105_ReadReg(A7105_00_MODE) & 0x01) {
402 | session->state = BIND_7;
403 | //DEBUG_MSG("Transmittion in progress, waiting");
404 | return 15000; //22.5msec elapsed since last write
405 | }
406 | A7105_ReadData(session->packet, 16);
407 | if(session->packet[1] == 9) {
408 | session->state = DATA_1;
409 | A7105_WriteReg(A7105_1F_CODE_I, 0x0F);
410 | //PROTOCOL_SetBindState(0);
411 | DEBUG_MSG("copter bound.");
412 | return 28000; //35.5msec elapsed since last write
413 | } else {
414 | session->state = BIND_7;
415 | DEBUG_MSG("Got unknown packet, first 2 bytes: "+String(session->packet[0], HEX) + " " + String(session->packet[1], HEX));
416 | return 15000; //22.5 msec elapsed since last write
417 | }
418 | case DATA_1:
419 | A7105_SetPower(Model.tx_power); // keep transmit power in sync
420 | case DATA_2:
421 | case DATA_3:
422 | case DATA_4:
423 | case DATA_5:
424 | uint16_t d;
425 | switch (session->telemetryState) { // Goebish - telemetry is every ~0.1S r 10 Tx packets
426 | case doTx:
427 | hubsan_build_packet(session);
428 | A7105_Strobe(A7105_STANDBY);
429 | A7105_WriteData(session->packet, 16, session->state == DATA_5 ? session->channel + 0x23 : session->channel);
430 | d = 3000; // nominal tx time
431 | session->telemetryState = waitTx;
432 | break;
433 | case waitTx:
434 | if(A7105_busy())
435 | d = 0;
436 | else { // wait for tx completion
437 | A7105_Strobe(A7105_RX);
438 | session->polls = 0;
439 | session->telemetryState = pollRx;
440 | d = 3000; // nominal rx time
441 | }
442 | break;
443 | case pollRx: // check for telemetry
444 | if(A7105_busy())
445 | d = 1000;
446 | else {
447 | A7105_ReadData(session->packet, 16);
448 | hubsan_update_telemetry(session);
449 | A7105_Strobe(A7105_RX);
450 | d = 1000;
451 | }
452 |
453 | if (++session->polls >= 7) { // 3ms + 3mS + 4*1ms
454 | if (session->state == DATA_5)
455 | session->state = DATA_1;
456 | else
457 | session->state++;
458 | session->telemetryState = doTx;
459 | }
460 | break;
461 | }
462 | return d;
463 | }
464 | return 0;
465 | }
466 |
467 | u16 hubsan_multiple_cb(HubsanSession *session)
468 | {
469 | static HubsanSession *bindingSession = NULL;
470 | int i;
471 | switch(session->state) {
472 | case BIND_1:
473 | bindingSession = session;
474 | A7105_WriteID(0x55201041);
475 | case BIND_3:
476 | case BIND_5:
477 | case BIND_7:
478 | hubsan_build_bind_packet(session, session->state == BIND_7 ? 9 : (session->state == BIND_5 ? 1 : session->state + 1 - BIND_1));
479 | A7105_Strobe(A7105_STANDBY);
480 | A7105_WriteData(session->packet, 16, session->channel);
481 | session->state |= WAIT_WRITE;
482 | return 3000;
483 | case BIND_1 | WAIT_WRITE:
484 | case BIND_3 | WAIT_WRITE:
485 | case BIND_5 | WAIT_WRITE:
486 | case BIND_7 | WAIT_WRITE:
487 | //wait for completion
488 | for(int i = 0; i < 20; i++) {
489 | if(! (A7105_ReadReg(A7105_00_MODE) & 0x01))
490 | break;
491 | }
492 | if (i == 20)
493 | DEBUG_MSG("Failed to complete write\n");
494 | A7105_SetTxRxMode(RX_EN);
495 | A7105_Strobe(A7105_RX);
496 | session->state &= ~WAIT_WRITE;
497 | session->state++;
498 | return 4500; //7.5msec elapsed since last write
499 | case BIND_2:
500 | case BIND_4:
501 | case BIND_6:
502 | A7105_SetTxRxMode(TX_EN);
503 | if(A7105_ReadReg(A7105_00_MODE) & 0x01) {
504 | session->state = BIND_1;
505 | return 4500; //No signal, restart binding procedure. 12msec elapsed since last write
506 | }
507 | A7105_ReadData(session->packet, 16);
508 | session->state++;
509 | if (session->state == BIND_5)
510 | A7105_WriteID((session->packet[2] << 24) | (session->packet[3] << 16) | (session->packet[4] << 8) | session->packet[5]);
511 |
512 | return 500; //8msec elapsed time since last write;
513 | case BIND_8:
514 | A7105_SetTxRxMode(TX_EN);
515 | if(A7105_ReadReg(A7105_00_MODE) & 0x01) {
516 | session->state = BIND_7;
517 | return 15000; //22.5msec elapsed since last write
518 | }
519 | A7105_ReadData(session->packet, 16);
520 | if(session->packet[1] == 9) {
521 | session->state = DATA_1;
522 | A7105_WriteReg(A7105_1F_CODE_I, 0x0F);
523 | bindingSession = NULL;
524 | //PROTOCOL_SetBindState(0);
525 | DEBUG_MSG("another copter bound.");
526 | return 28000; //35.5msec elapsed since last write
527 | } else {
528 | session->state = BIND_7;
529 | return 15000; //22.5 msec elapsed since last write
530 | }
531 | case DATA_1:
532 | A7105_SetPower(Model.tx_power); // keep transmit power in sync
533 | case DATA_2:
534 | case DATA_3:
535 | case DATA_4:
536 | case DATA_5:
537 | if (bindingSession == NULL) {
538 | hubsan_build_packet(session);
539 | A7105_WriteID(session->sessionid);
540 | A7105_WriteData(session->packet, 16, session->state == DATA_5 ? session->channel + 0x23 : session->channel);
541 |
542 | // wait for completion
543 | for(i = 0; i < 20; i++) {
544 | if(!(A7105_ReadReg(A7105_00_MODE) & 0x01))
545 | break;
546 | }
547 | if (i == 20)
548 | DEBUG_MSG("Failed to complete write\n");
549 | }
550 |
551 | if (session->state == DATA_5)
552 | session->state = DATA_1;
553 | else
554 | session->state++;
555 | return 10000;
556 | }
557 | return 0;
558 | }
559 |
560 | void hubsan_initialize()
561 | {
562 | CLOCK_StopTimer();
563 | while(1) {
564 | A7105_Reset();
565 | CLOCK_ResetWatchdog();
566 | DEBUG_MSG("hubsan_init()");
567 | if (hubsan_init())
568 | break;
569 | }
570 | }
571 |
572 | u32 find_free_sessionid(Session* sessions[], int num_sessions)
573 | {
574 | u32 sessionid;
575 | int isfree = false;
576 | while(!isfree) {
577 | sessionid = rand32_r(0, 0);
578 | isfree = true;
579 | for(int i=0; icopterType == HUBSAN_X4) {
581 | if (((HubsanSession*)sessions[i]->copterSession)->sessionid == sessionid) {
582 | isfree = false;
583 | break;
584 | }
585 | }
586 | }
587 | }
588 | return sessionid;
589 | }
590 |
591 | u8 find_free_channel(Session* sessions[], int num_sessions)
592 | {
593 | u8 channel;
594 | int isfree = false;
595 | while(!isfree) {
596 | channel = allowed_channels[rand32_r(0, 0) % sizeof(allowed_channels)];
597 | isfree = true;
598 | for(int i=0; icopterType == HUBSAN_X4) {
600 | if (((HubsanSession*)sessions[i]->copterSession)->channel == channel) {
601 | isfree = false;
602 | break;
603 | }
604 | }
605 | }
606 | }
607 | return channel;
608 | }
609 |
610 | HubsanSession* hubsan_bind(int copterid, Session* sessions[], int num_sessions)
611 | {
612 | HubsanSession *session = (HubsanSession*)malloc(sizeof(HubsanSession));
613 | memset(session, 0, sizeof(HubsanSession));
614 |
615 | // hubsan default values
616 | session->rudder = session->aileron = session->elevator = 0x7F;
617 | session->led = 1;
618 |
619 | session->txid = 0xdb042679 + copterid;
620 | session->sessionid = find_free_sessionid(sessions, num_sessions);
621 | #ifdef FIXED_CHANNEL
622 | session->channel = FIXED_CHANNEL;
623 | #else
624 | session->channel = find_free_channel(sessions, num_sessions);
625 | #endif
626 | //PROTOCOL_SetBindState(0xFFFFFFFF);
627 | session->state = BIND_1;
628 | session->packet_count=0;
629 | //memset(&Telemetry, 0, sizeof(Telemetry));
630 | //TELEMETRY_SetType(TELEM_DEVO);
631 | if( Model.proto_opts[PROTOOPTS_VTX_FREQ] == 0)
632 | Model.proto_opts[PROTOOPTS_VTX_FREQ] = 5885;
633 | CLOCK_StartTimer(10000, hubsan_cb);
634 | return session;
635 | }
636 |
637 | int hubsan_get_binding_state(HubsanSession *session) {
638 | return session->state >= DATA_1 && session->state <= DATA_5;
639 | }
640 |
--------------------------------------------------------------------------------
/coptermanager/hubsan.h:
--------------------------------------------------------------------------------
1 | #ifndef HUBSAN_H
2 | #define HUBSAN_H
3 |
4 | #include "session.h"
5 |
6 | static const u8 allowed_channels[] = {0x14, 0x1e, 0x28, 0x32, 0x3c, 0x46, 0x50, 0x5a, 0x64, 0x6e, 0x78, 0x82};
7 |
8 | void hubsan_initialize();
9 | HubsanSession* hubsan_bind(int copterid, Session* sessions[], int num_sessions);
10 | u16 hubsan_cb(HubsanSession* session);
11 | u16 hubsan_multiple_cb(HubsanSession* session);
12 | int hubsan_get_binding_state(HubsanSession* session);
13 |
14 | #endif
15 |
--------------------------------------------------------------------------------
/coptermanager/macros.h:
--------------------------------------------------------------------------------
1 | #ifndef MACROS_H
2 | #define MACROS_H
3 |
4 | #include
5 |
6 | #define CLOCK_StartTimer(x, y) (void)0
7 | #define CLOCK_StopTimer() (void)0
8 | #define CLOCK_ResetWatchdog() (void)0
9 | #define CLOCK_getms() millis()
10 | #define rand32_r(x, y) rand()
11 | #define usleep(x) delayMicroseconds(x)
12 |
13 | #endif
14 |
--------------------------------------------------------------------------------
/coptermanager/manager.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "manager.h"
3 | #include "common.h"
4 | #include "a7105.h"
5 | #include "hubsan.h"
6 | #include "session.h"
7 |
8 | static Session* sessions[NUM_COPTERS] = {NULL};
9 | static int active_sessions_count = 0;
10 |
11 | void manager_init()
12 | {
13 | Serial.begin(BAUDRATE);
14 | while (!Serial);
15 | A7105_Setup();
16 | hubsan_initialize();
17 | DEBUG_MSG("initialization successful");
18 | }
19 |
20 | static int copter_bind(int type, int *response)
21 | {
22 | for (int copterid=1; copterid <= NUM_COPTERS; copterid++) {
23 | if (sessions[copterid-1] == NULL) {
24 | switch(type) {
25 | case HUBSAN_X4: {
26 | Session* session = (Session*)malloc(sizeof(Session));
27 | session->copterType = HUBSAN_X4;
28 | session->nextRunAt = 0;
29 | session->initTime = millis();
30 | session->bindTime = 0;
31 | session->emergencyFlag = 0;
32 | session->copterSession = hubsan_bind(copterid, sessions, NUM_COPTERS);
33 | active_sessions_count++;
34 | sessions[copterid-1] = session;
35 | }
36 | break;
37 |
38 | default:
39 | return PROTOCOL_INVALID_COPTER_TYPE;
40 | }
41 | response[0] = 1;
42 | response[1] = copterid;
43 | return PROTOCOL_OK;
44 | }
45 | }
46 |
47 | return PROTOCOL_ALL_SLOTS_FULL;
48 | }
49 |
50 | int manager_process_hubsan_command(Session *session, int command, int value, int* response)
51 | {
52 | HubsanSession *hubsanSession = (HubsanSession*)session->copterSession;
53 |
54 | switch(command) {
55 | case COPTER_THROTTLE:
56 | if (value >= 0x00 && value <= 0xFF) {
57 | hubsanSession->throttle = value;
58 | return PROTOCOL_OK;
59 | }
60 | else {
61 | return PROTOCOL_VALUE_OUT_OF_RANGE;
62 | }
63 |
64 | case COPTER_RUDDER:
65 | if (value >= 0x34 && value <= 0xCC) {
66 | hubsanSession->rudder = value;
67 | return PROTOCOL_OK;
68 | }
69 | else {
70 | return PROTOCOL_VALUE_OUT_OF_RANGE;
71 | }
72 |
73 | case COPTER_AILERON:
74 | if (value >= 0x45 && value <= 0xC3) {
75 | hubsanSession->aileron = value;
76 | return PROTOCOL_OK;
77 | }
78 | else {
79 | return PROTOCOL_VALUE_OUT_OF_RANGE;
80 | }
81 |
82 | case COPTER_ELEVATOR:
83 | if (value >= 0x3e && value <= 0xBC) {
84 | hubsanSession->elevator = value;
85 | return PROTOCOL_OK;
86 | }
87 | else {
88 | return PROTOCOL_VALUE_OUT_OF_RANGE;
89 | }
90 |
91 | case COPTER_LED:
92 | if (value >= 0x00 && value <= 0x01) {
93 | hubsanSession->led = value;
94 | return PROTOCOL_OK;
95 | }
96 | else {
97 | return PROTOCOL_VALUE_OUT_OF_RANGE;
98 | }
99 |
100 | case COPTER_FLIP:
101 | if (value >= 0x00 && value <= 0x01) {
102 | hubsanSession->flip = value;
103 | return PROTOCOL_OK;
104 | }
105 | else {
106 | return PROTOCOL_VALUE_OUT_OF_RANGE;
107 | }
108 |
109 | case COPTER_VIDEO:
110 | if (value >= 0x00 && value <= 0x01) {
111 | hubsanSession->video = value;
112 | return PROTOCOL_OK;
113 | }
114 | else {
115 | return PROTOCOL_VALUE_OUT_OF_RANGE;
116 | }
117 |
118 | case COPTER_GETSTATE:
119 | response[0] = 1;
120 | response[1] = hubsan_get_binding_state(hubsanSession);
121 | return PROTOCOL_OK;
122 |
123 | case COPTER_TELEMETRY:
124 | switch(value) {
125 | case TELEMETRY_ALTITUDE:
126 | response[0] = 2;
127 | response[1] = (hubsanSession->estAltitude >> 8) & 0xFF;
128 | response[2] = (hubsanSession->estAltitude >> 0) & 0xFF;
129 | break;
130 |
131 | case TELEMETRY_VOLTAGE:
132 | response[0] = 1;
133 | response[1] = hubsanSession->batteryVolts;
134 | break;
135 |
136 | case TELEMETRY_GYROSCOPE:
137 | response[0] = 6;
138 | response[1] = (hubsanSession->gyroData[0] >> 8) & 0xFF;
139 | response[2] = (hubsanSession->gyroData[0] >> 0) & 0xFF;
140 | response[3] = (hubsanSession->gyroData[1] >> 8) & 0xFF;
141 | response[4] = (hubsanSession->gyroData[1] >> 0) & 0xFF;
142 | response[5] = (hubsanSession->gyroData[2] >> 8) & 0xFF;
143 | response[6] = (hubsanSession->gyroData[2] >> 0) & 0xFF;
144 | break;
145 |
146 | case TELEMETRY_ACCELEROMETER:
147 | response[0] = 6;
148 | response[1] = (hubsanSession->accData[0] >> 8) & 0xFF;
149 | response[2] = (hubsanSession->accData[0] >> 0) & 0xFF;
150 | response[3] = (hubsanSession->accData[1] >> 8) & 0xFF;
151 | response[4] = (hubsanSession->accData[1] >> 0) & 0xFF;
152 | response[5] = (hubsanSession->accData[2] >> 8) & 0xFF;
153 | response[6] = (hubsanSession->accData[2] >> 0) & 0xFF;
154 | break;
155 |
156 | case TELEMETRY_ANGLE:
157 | response[0] = 6;
158 | response[1] = (hubsanSession->angle[0] >> 8) & 0xFF;
159 | response[2] = (hubsanSession->angle[0] >> 0) & 0xFF;
160 | response[3] = (hubsanSession->angle[1] >> 8) & 0xFF;
161 | response[4] = (hubsanSession->angle[1] >> 0) & 0xFF;
162 | response[5] = (hubsanSession->angle[2] >> 8) & 0xFF;
163 | response[6] = (hubsanSession->angle[2] >> 0) & 0xFF;
164 | break;
165 |
166 | default:
167 | return PROTOCOL_INVALID_TELEMETRY_OPTION;
168 | }
169 | return PROTOCOL_OK;
170 |
171 | case COPTER_EMERGENCY:
172 | hubsanSession->throttle = 0;
173 | hubsanSession->rudder = 0x7F;
174 | hubsanSession->aileron = 0x7F;
175 | hubsanSession->elevator = 0x7F;
176 | hubsanSession->led = 1;
177 | hubsanSession->flip = 0;
178 | hubsanSession->video = 0;
179 | return PROTOCOL_OK;
180 |
181 | case COPTER_DISCONNECT:
182 | return PROTOCOL_OK;
183 |
184 | default:
185 | return PROTOCOL_UNKNOWN_COMMAND;
186 | }
187 | }
188 |
189 | // copterid: 1..NUM_COPTERS
190 | int manager_processcommand(int copterid, int command, int value, int checksum, int* response)
191 | {
192 | // no additional response
193 | response[0] = 0;
194 |
195 | #ifdef DEBUG
196 | if (command == 0xFF) {
197 | Serial.println("INFO:");
198 | HubsanSession *hubsanSession = (HubsanSession*)sessions[copterid-1]->copterSession;
199 | Serial.println("channel: "+String(hubsanSession->channel));
200 | Serial.println();
201 | return PROTOCOL_OK;
202 | }
203 | #endif
204 |
205 | if (checksum != calculate_checksum(copterid, command, value)) {
206 | return PROTOCOL_INVALID_CHECKSUM;
207 | }
208 | else if (command == COPTER_BIND) {
209 | return copter_bind(value, response);
210 | }
211 | else if (command == COPTER_LISTCOPTERS) {
212 | int bitmask = 0;
213 | for (int copterid=1; copterid <= NUM_COPTERS; copterid++) {
214 | if (sessions[copterid-1] != NULL)
215 | bitmask |= 1 << (copterid-1);
216 | }
217 | response[0] = 1;
218 | response[1] = bitmask;
219 | return PROTOCOL_OK;
220 | }
221 | else {
222 | if (copterid < 1 || copterid > NUM_COPTERS || sessions[copterid-1] == NULL)
223 | return PROTOCOL_INVALID_SLOT;
224 |
225 | Session *session = sessions[copterid-1];
226 |
227 | // if emergency flag is set only allow disconnect command
228 | if (session->emergencyFlag == 1 && command != COPTER_DISCONNECT)
229 | return PROTOCOL_EMERGENCY_MODE_ON;
230 |
231 | int resultCode;
232 | switch(session->copterType) {
233 | case HUBSAN_X4:
234 | resultCode = manager_process_hubsan_command(session, command, value, response);
235 | break;
236 |
237 | default:
238 | return PROTOCOL_INVALID_COPTER_TYPE;
239 | }
240 |
241 | if (command == COPTER_EMERGENCY) {
242 | session->emergencyFlag = 1;
243 | }
244 | else if (resultCode == PROTOCOL_OK && command == COPTER_DISCONNECT) {
245 | free(session->copterSession);
246 | free(session);
247 | sessions[copterid-1] = NULL;
248 | active_sessions_count--;
249 | }
250 |
251 | return resultCode;
252 | }
253 |
254 | return PROTOCOL_UNKNOWN_COMMAND;
255 | }
256 |
257 | int dummy_response[7];
258 | inline void manager_copterloop(int copterid)
259 | {
260 | Session* session = sessions[copterid-1];
261 |
262 | if (session->bindTime == 0) {
263 | if ((millis() - session->initTime) > MAX_UNBOUND_TIME) {
264 | DEBUG_MSG("removing inactive copter " + String(copterid) + " (max unbound time reached)");
265 | manager_processcommand(copterid, COPTER_DISCONNECT, 0, calculate_checksum(copterid, COPTER_DISCONNECT, 0), dummy_response);
266 | return;
267 | }
268 | }
269 | else {
270 | if ((millis() - session->bindTime) > MAX_BOUND_TIME) {
271 | DEBUG_MSG("removing inactive copter " + String(copterid) + " (max bound time reached)");
272 | manager_processcommand(copterid, COPTER_DISCONNECT, 0, calculate_checksum(copterid, COPTER_DISCONNECT, 0), dummy_response);
273 | return;
274 | }
275 | }
276 |
277 | if (micros() > session->nextRunAt) {
278 | switch(session->copterType) {
279 | case HUBSAN_X4:
280 | HubsanSession *hubsanSession = (HubsanSession*)session->copterSession;
281 | int is_bound = hubsan_get_binding_state(hubsanSession);
282 | int waitTime = active_sessions_count <= 1 ? hubsan_cb(hubsanSession) : hubsan_multiple_cb(hubsanSession);
283 | session->nextRunAt = micros() + waitTime;
284 | if (!is_bound && hubsan_get_binding_state(hubsanSession))
285 | session->bindTime = millis();
286 | break;
287 | }
288 | }
289 | }
290 |
291 | void manager_loop()
292 | {
293 | for (int copterid=1; copterid <= NUM_COPTERS; copterid++) {
294 | if (sessions[copterid-1] != NULL)
295 | manager_copterloop(copterid);
296 | }
297 | }
298 |
--------------------------------------------------------------------------------
/coptermanager/manager.h:
--------------------------------------------------------------------------------
1 | #ifndef MANAGER_H
2 | #define MANAGER_H
3 |
4 | enum CopterType {
5 | HUBSAN_X4 = 0x01
6 | };
7 |
8 | enum TelemetryData {
9 | TELEMETRY_ALTITUDE = 0x01,
10 | TELEMETRY_VOLTAGE = 0x02,
11 | TELEMETRY_GYROSCOPE = 0x03,
12 | TELEMETRY_ACCELEROMETER = 0x04,
13 | TELEMETRY_ANGLE = 0x05
14 | };
15 |
16 | enum Command {
17 | COPTER_BIND = 0x01,
18 | COPTER_THROTTLE = 0x02,
19 | COPTER_RUDDER = 0x03,
20 | COPTER_AILERON = 0x04,
21 | COPTER_ELEVATOR = 0x05,
22 | COPTER_LED = 0x06,
23 | COPTER_FLIP = 0x07,
24 | COPTER_VIDEO = 0x08,
25 | COPTER_GETSTATE = 0x09,
26 | COPTER_TELEMETRY = 0x0A,
27 | COPTER_EMERGENCY = 0x0B,
28 | COPTER_DISCONNECT = 0x0C,
29 | COPTER_LISTCOPTERS = 0x0D
30 | };
31 |
32 | enum ResultCode {
33 | PROTOCOL_OK = 0x00,
34 | PROTOCOL_INVALID_CHECKSUM = 0xF0,
35 | PROTOCOL_INVALID_COPTER_TYPE = 0xF1,
36 | PROTOCOL_ALL_SLOTS_FULL = 0xF2,
37 | PROTOCOL_INVALID_SLOT = 0xF3,
38 | PROTOCOL_VALUE_OUT_OF_RANGE = 0xF4,
39 | PROTOCOL_EMERGENCY_MODE_ON = 0xF5,
40 | PROTOCOL_NO_TELEMETRY = 0xF6,
41 | PROTOCOL_INVALID_TELEMETRY_OPTION = 0xF7,
42 | PROTOCOL_UNKNOWN_COMMAND = 0xF8
43 | };
44 |
45 | inline int calculate_checksum(int copterid, int command, int value)
46 | {
47 | int sum = copterid + command + value;
48 | return (256 - (sum % 256)) & 0xFF;
49 | }
50 |
51 | void manager_init();
52 | int manager_processcommand(int copterid, int command, int value, int checksum, int* response);
53 | void manager_loop();
54 |
55 | #endif
56 |
--------------------------------------------------------------------------------
/coptermanager/session.h:
--------------------------------------------------------------------------------
1 | #ifndef SESSION_H
2 | #define SESSION_H
3 |
4 | #include "common.h"
5 | #include "manager.h"
6 |
7 | enum {
8 | doTx,
9 | waitTx,
10 | pollRx
11 | };
12 |
13 | struct HubsanSession {
14 | u32 txid;
15 | u8 packet[16];
16 | u8 channel;
17 | u32 sessionid;
18 | u8 state;
19 | u8 packet_count;
20 | // telemetry
21 | uint8_t telemetryState = doTx;
22 | uint8_t polls;
23 |
24 | u8 throttle; // ascend/descend, observed range: 0x00 - 0xff (smaller is down)
25 | u8 rudder; // rotate left/right, observed range: 0x34 - 0xcc (smaller is right)
26 | u8 aileron; // drift sideways left/right, observed range: 0x45 - 0xc3 (smaller is right)
27 | u8 elevator; // forward/backward, observed range: 0x3e - 0xbc (smaller is up)
28 | int led;
29 | int flip;
30 | int video;
31 |
32 | int16_t estAltitude;
33 | uint8_t batteryVolts;
34 | int16_t gyroData[3];
35 | int16_t accData[3];
36 | int16_t angle[3];
37 | };
38 |
39 | struct Session {
40 | CopterType copterType;
41 | unsigned long nextRunAt; // us
42 | unsigned long initTime; // ms
43 | unsigned long bindTime; // ms
44 | int emergencyFlag;
45 | void* copterSession;
46 | };
47 |
48 | #endif
49 |
--------------------------------------------------------------------------------