├── .gitignore ├── IMU_calibrator └── imucal ├── LICENSE ├── README.md ├── camera_test ├── CMakeLists.txt ├── Loitor_VISensor_Setups.txt ├── include │ ├── loitorimu.h │ └── loitorusbcam.h ├── loitor-vi-install.sh └── src │ ├── camtest2.cpp │ ├── loitorimu.cpp │ └── loitorusbcam.cpp ├── loitor-vi-install.sh ├── loitor_PCcam_cliper.stl └── loitor_ros_workspace └── src └── loitor_stereo_visensor ├── CMakeLists.txt ├── Loitor_VISensor_Setups.txt ├── cfg ├── Loitor_VISensor_Setups.txt └── backup_settings.txt ├── include └── loitor_stereo_visensor │ ├── loitorimu.h │ └── loitorusbcam.h ├── package.xml └── src ├── loitor_stereo_visensor.cpp ├── loitorimu.cpp └── loitorusbcam.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | # Build folders 35 | build/ 36 | -------------------------------------------------------------------------------- /IMU_calibrator/imucal: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/loitor-vis/vi_sensor_sdk/eb54545ef5eeb681a5271725e8a9d9a21c97367a/IMU_calibrator/imucal -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # vi_sensor_sdk 2 | Loitor VI Sensor SDK, ROS pack and IMU calibration program. 3 | -------------------------------------------------------------------------------- /camera_test/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(camtest) 4 | 5 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread") 6 | set(LIBRARY_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/lib") 7 | 8 | find_package(OpenCV REQUIRED) 9 | 10 | include_directories("${PROJECT_SOURCE_DIR}/include") 11 | 12 | link_directories("${PROJECT_SOURCE_DIR}/lib") 13 | 14 | add_library(loitorusbcam src/loitorusbcam.cpp) 15 | target_link_libraries(loitorusbcam loitorimu) 16 | 17 | add_library(loitorimu src/loitorimu.cpp) 18 | 19 | add_executable(camtest src/camtest2.cpp) 20 | target_link_libraries(camtest loitorusbcam loitorimu usb-1.0 ${OpenCV_LIBS}) 21 | -------------------------------------------------------------------------------- /camera_test/Loitor_VISensor_Setups.txt: -------------------------------------------------------------------------------- 1 | # 2 | Mode 3 | 12 4 | # 5 | HighSpeed Mode 6 | m1,left,150,VGA,54 7 | m2,right,150,VGA,54 8 | m3,left,150,WVGA,54 9 | m4,right,150,WVGA,54 10 | m5,stereo,250,VGA,54 11 | m6,stereo,270,WVGA,54 12 | # 13 | Normal Mode 14 | m7,left,150,VGA,54 15 | m8,right,150,VGA,54 16 | m9,left,150,WVGA,54 17 | m10,right,150,WVGA,54 18 | m11,stereo,274,VGA,27 19 | m12,stereo,162,WVGA,27 20 | # 21 | m13,Manual Mode 22 | 0 23 | 194 24 | VGA 25 | 54 26 | # 27 | EG_mode 28 | 4 29 | manual,50,200 30 | auto,300,5,58 31 | autoexp_manualgain,300,5,58,200 32 | /dev/ttyUSB0,5 33 | # 34 | IMU-acc-bias 35 | Gx,52.000000 36 | Gy,32.00001 37 | Gz,-243.000000 38 | # 39 | -------------------------------------------------------------------------------- /camera_test/include/loitorimu.h: -------------------------------------------------------------------------------- 1 | #ifndef LOITORIMU_H 2 | #define LOITORIMU_H 3 | 4 | #include 5 | 6 | typedef struct 7 | { 8 | float imu_time; // time-stamp 9 | timeval system_time; 10 | unsigned char num; 11 | float rx,ry,rz; 12 | float ax,ay,az; 13 | float qw,qx,qy,qz; 14 | //int timestamp; 15 | }visensor_imudata; 16 | 17 | extern timeval visensor_startTime; 18 | 19 | bool visensor_query_imu_update(); 20 | bool visensor_mark_imu_update(); 21 | bool visensor_erase_imu_update(); 22 | int visensor_set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop); 23 | int visensor_open_port(const char* dev_str); 24 | int visensor_get_imu_frame(int fd, unsigned char* imu_frame); 25 | int visensor_get_imu_data(unsigned char* imu_frame,short int* acc_offset,visensor_imudata *imudata_struct,bool show_data); 26 | int visensor_send_imu_frame(int fd, unsigned char* data, int len); 27 | 28 | int print_imu_datawithtime(int data_count, float* fgyr, float* facc, double timestamp, double camtimes, short int* acc_offset); 29 | int get_imu_datawithtime(int fd, int* data_count, float* gyr, float* acc, double* timestamp, double* camtimes); 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /camera_test/include/loitorusbcam.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include "loitorimu.h" 3 | 4 | #ifndef LOITORUSBCAM_H 5 | #define LOITORUSBCAM_H 6 | #define IMU_FRAME_LEN 32 7 | #define IMG_WIDTH_VGA 640 8 | #define IMG_HEIGHT_VGA 480 9 | #define IMG_SIZE_VGA (IMG_WIDTH_VGA*IMG_HEIGHT_VGA) 10 | #define IMG_BUF_SIZE_VGA (IMG_SIZE_VGA+0x200) 11 | #define IMG_WIDTH_WVGA 752 12 | #define IMG_HEIGHT_WVGA 480 13 | #define IMG_SIZE_WVGA (IMG_WIDTH_WVGA*IMG_HEIGHT_WVGA) 14 | #define IMG_BUF_SIZE_WVGA (IMG_SIZE_WVGA+0x200) 15 | 16 | /* 17 | * camera当前分辨率状态 18 | * 0-代表VGA 19 | * 1-代表WVGA 20 | */ 21 | extern bool visensor_resolution_status; // 0-VGA | 1-WVGA 22 | /* 23 | * camera当前通道选择 24 | * 0-代表开启双目 25 | * 1-代表只开启右眼 26 | * 2-代表只开启左眼 27 | */ 28 | extern int visensor_cam_selection; // 0-stereo | 1-right | 2-left 29 | /* 30 | * imu 数据 31 | */ 32 | extern visensor_imudata visensor_imudata_pack; 33 | 34 | // setters & getters 35 | void visensor_set_auto_EG(int E_AG); // 0-ManEG | 1-AutoEG with limits | 2-AutoE&ManG | 3- fully auto 36 | void visensor_set_exposure(int _man_exp); 37 | void visensor_set_gain(int _man_gain); 38 | void visensor_set_max_autoExp(int max_exp); 39 | void visensor_set_min_autoExp(int min_exp); 40 | void visensor_set_resolution(bool set_wvga); 41 | void visensor_set_fps_mode(bool fps_mode); 42 | void visensor_set_current_HB(int HB); 43 | void visensor_set_desired_bin(int db); 44 | void visensor_set_cam_selection_mode(int _visensor_cam_selection); 45 | void visensor_set_imu_bias(float bx,float by,float bz); 46 | void visensor_set_imu_portname(char* input_name); 47 | void visensor_set_current_mode(int _mode); 48 | 49 | int visensor_get_EG_mode(); 50 | int visensor_get_exposure(); 51 | int visensor_get_gain(); 52 | int visensor_get_max_autoExp(); 53 | int visensor_get_min_autoExp(); 54 | bool visensor_get_resolution(); 55 | int visensor_get_fps(); 56 | int visensor_get_current_HB(); 57 | int visensor_get_desired_bin(); 58 | int visensor_get_cam_selection_mode(); 59 | float visensor_get_imu_G_bias_x(); 60 | float visensor_get_imu_G_bias_y(); 61 | float visensor_get_imu_G_bias_z(); 62 | const char* visensor_get_imu_portname(); 63 | 64 | void visensor_save_current_settings(); 65 | 66 | float visensor_get_hardware_fps(); 67 | 68 | void visensor_load_settings(const char* settings_file); 69 | 70 | bool visensor_is_stereo_good(); 71 | bool visensor_is_left_good(); 72 | bool visensor_is_right_good(); 73 | 74 | bool visensor_is_left_fresh(); 75 | bool visensor_is_right_fresh(); 76 | 77 | /* 78 | * 得到绑定了同步IMU数据之后的图像数据 79 | */ 80 | visensor_imudata visensor_get_stereoImg(char* left_img,char* right_img); 81 | visensor_imudata visensor_get_stereoImg(char* left_img,char* right_img,timeval &left_stamp,timeval &right_stamp); 82 | 83 | visensor_imudata visensor_get_leftImg(char* left_img); 84 | visensor_imudata visensor_get_leftImg(char* left_img,timeval &left_stamp); 85 | 86 | visensor_imudata visensor_get_rightImg(char* right_img); 87 | visensor_imudata visensor_get_rightImg(char* right_img,timeval &right_stamp); 88 | 89 | int visensor_Start_Cameras(); 90 | void visensor_Close_Cameras(); 91 | 92 | bool visensor_imu_have_fresh_data(); 93 | 94 | int visensor_Start_IMU(); 95 | void visensor_Close_IMU(); 96 | 97 | 98 | #endif 99 | -------------------------------------------------------------------------------- /camera_test/loitor-vi-install.sh: -------------------------------------------------------------------------------- 1 | create_udev_rules() { 2 | echo "" > loitor-vi.rules 3 | echo 'KERNEL=="*", SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", ACTION=="add", ATTR{idVendor}=="04b4", MODE="666" ' >> loitor-vi.rules 4 | echo 'KERNEL=="*", SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", ACTION=="remove" ' >> loitor-vi.rules 5 | } 6 | 7 | if [ `whoami` != 'root' ]; then 8 | echo "You have to be root to run this script" 9 | echo "请使用root权限运行此脚本" 10 | exit 1; 11 | fi 12 | 13 | create_udev_rules 14 | cp loitor-vi.rules /etc/udev/rules.d/ 15 | rm loitor-vi.rules 16 | -------------------------------------------------------------------------------- /camera_test/src/camtest2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | #include 10 | #include 11 | 12 | #include "loitorusbcam.h" 13 | #include "loitorimu.h" 14 | 15 | using namespace std; 16 | using namespace cv; 17 | bool close_img_viewer=false; 18 | bool visensor_Close_IMU_viewer=false; 19 | 20 | // 当前左右图像的时间戳 21 | timeval left_stamp,right_stamp; 22 | 23 | // image viewer 24 | void *opencv_showimg(void*) 25 | { 26 | Mat img_left; 27 | Mat img_right; 28 | 29 | if(!visensor_resolution_status) 30 | { 31 | img_left.create(Size(640,480),CV_8U); 32 | img_right.create(Size(640,480),CV_8U); 33 | img_left.data=new unsigned char[IMG_WIDTH_VGA*IMG_HEIGHT_VGA]; 34 | img_right.data=new unsigned char[IMG_WIDTH_VGA*IMG_HEIGHT_VGA]; 35 | } 36 | else 37 | { 38 | img_left.create(Size(752,480),CV_8U); 39 | img_right.create(Size(752,480),CV_8U); 40 | img_left.data=new unsigned char[IMG_WIDTH_WVGA*IMG_HEIGHT_WVGA]; 41 | img_right.data=new unsigned char[IMG_WIDTH_WVGA*IMG_HEIGHT_WVGA]; 42 | } 43 | while(!close_img_viewer) 44 | { 45 | if(visensor_cam_selection==2) 46 | { 47 | visensor_imudata paired_imu=visensor_get_leftImg((char *)img_left.data,left_stamp); 48 | 49 | // 显示同步数据的时间戳(单位微秒) 50 | cout<<"left_time : "< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include "math.h" 12 | 13 | #define IMU_FRAME_LEN 32 14 | #include "loitorimu.h" 15 | 16 | bool shut_down_imu=false; 17 | 18 | timeval visensor_startTime; // time-stamp 19 | struct timeval imu_SysTime; 20 | 21 | float imu_time=0; 22 | bool visensor_imu_updated=false; 23 | 24 | bool visensor_query_imu_update() 25 | { 26 | return visensor_imu_updated; 27 | } 28 | bool visensor_mark_imu_update() 29 | { 30 | visensor_imu_updated=true; 31 | } 32 | bool visensor_erase_imu_update() 33 | { 34 | visensor_imu_updated=false; 35 | } 36 | 37 | int visensor_set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop) 38 | { 39 | struct termios newtio,oldtio; 40 | if ( tcgetattr( fd,&oldtio) != 0) 41 | { 42 | perror("SetupSerial 1"); 43 | return -1; 44 | } 45 | bzero( &newtio, sizeof( newtio ) ); 46 | newtio.c_cflag |= CLOCAL | CREAD; 47 | newtio.c_cflag &= ~CSIZE; 48 | 49 | switch( nBits ) 50 | { 51 | case 7: 52 | newtio.c_cflag |= CS7; 53 | break; 54 | case 8: 55 | newtio.c_cflag |= CS8; 56 | break; 57 | } 58 | 59 | switch( nEvent ) 60 | { 61 | case 'O': 62 | newtio.c_cflag |= PARENB; 63 | newtio.c_cflag |= PARODD; 64 | newtio.c_iflag |= (INPCK | ISTRIP); 65 | break; 66 | case 'E': 67 | newtio.c_iflag |= (INPCK | ISTRIP); 68 | newtio.c_cflag |= PARENB; 69 | newtio.c_cflag &= ~PARODD; 70 | break; 71 | case 'N': 72 | newtio.c_cflag &= ~PARENB; 73 | break; 74 | } 75 | 76 | switch( nSpeed ) 77 | { 78 | case 2400: 79 | cfsetispeed(&newtio, B2400); 80 | cfsetospeed(&newtio, B2400); 81 | break; 82 | case 4800: 83 | cfsetispeed(&newtio, B4800); 84 | cfsetospeed(&newtio, B4800); 85 | break; 86 | case 9600: 87 | cfsetispeed(&newtio, B9600); 88 | cfsetospeed(&newtio, B9600); 89 | break; 90 | case 115200: 91 | cfsetispeed(&newtio, B115200); 92 | cfsetospeed(&newtio, B115200); 93 | break; 94 | default: 95 | cfsetispeed(&newtio, B9600); 96 | cfsetospeed(&newtio, B9600); 97 | break; 98 | } 99 | if( nStop == 1 ) 100 | newtio.c_cflag &= ~CSTOPB; 101 | else if ( nStop == 2 ) 102 | newtio.c_cflag |= CSTOPB; 103 | newtio.c_cc[VTIME] = 0; 104 | newtio.c_cc[VMIN] = 32; 105 | tcflush(fd,TCIFLUSH); 106 | if((tcsetattr(fd,TCSANOW,&newtio))!=0) 107 | { 108 | perror("com set error"); 109 | return -1; 110 | } 111 | printf("set done!\n"); 112 | return 0; 113 | } 114 | 115 | int visensor_open_port(const char* dev_str) 116 | { 117 | long vdisable; 118 | 119 | int fd = open( dev_str, O_RDWR|O_NOCTTY|O_NDELAY); 120 | if (-1 == fd) 121 | { 122 | perror("Can't Open Serial Port"); 123 | return(-1); 124 | } 125 | 126 | if(fcntl(fd, F_SETFL, 0)<0) 127 | printf("fcntl failed!\n"); 128 | else 129 | printf("fcntl=%d\n",fcntl(fd, F_SETFL,0)); 130 | if(isatty(STDIN_FILENO)==0) 131 | printf("standard input is not a terminal device\n"); 132 | else 133 | printf("isatty success!\n"); 134 | printf("fd-open=%d\n",fd); 135 | return fd; 136 | } 137 | 138 | static int find_55aa(unsigned char* buf,int len) 139 | { 140 | int i; 141 | for(i=0; i 159 | 160 | int visensor_get_imu_frame(int fd, unsigned char* imu_frame) 161 | { 162 | 163 | unsigned char imu_frame_buf[2*IMU_FRAME_LEN]; 164 | memset(imu_frame,0,IMU_FRAME_LEN); 165 | memset(imu_frame_buf,0,2*IMU_FRAME_LEN); 166 | int num_get = 0; 167 | 168 | static int flush_count = 0; 169 | flush_count = (flush_count+1)%200; 170 | if(flush_count==0) 171 | tcflush(fd,TCIFLUSH); 172 | static struct timeval getIMUTime; 173 | struct timeval endTime; 174 | float fTimeuse; 175 | while(!shut_down_imu) 176 | { 177 | //读到32个以上字节 178 | while(num_get32) 237 | { 238 | tcflush(fd,TCIFLUSH); 239 | memset(imu_frame_buf,0,2*IMU_FRAME_LEN); 240 | num_get = 0; 241 | continue; 242 | } 243 | //如果校验和正确,那么输出该帧数据 244 | memcpy(imu_frame,&imu_frame_buf[position_55aa],IMU_FRAME_LEN); 245 | static float time_interval=5000; 246 | static float last_time,new_time,new_pred,last_pred; 247 | new_time = getIMUTime.tv_usec; 248 | int newtime_interval; 249 | newtime_interval = new_time-last_time; 250 | if(newtime_interval<4000||newtime_interval>6000) 251 | newtime_interval = 5000; 252 | time_interval = time_interval*0.999+0.001*newtime_interval; 253 | if(new_time-last_predimu_time=imu_time; 305 | imudata_struct->system_time=imu_SysTime; 306 | 307 | static int last_imu_no; 308 | //for(int i=1; i<(imu_frame[2]+200-last_imu_no)%200; i++) 309 | // printf(".................................................................................\r\n"); 310 | last_imu_no = imu_frame[2]; 311 | int imu_data[10]; 312 | imu_data[0] = *(short*)(&imu_frame[3]); 313 | imu_data[1] = *(short*)(&imu_frame[5]); 314 | imu_data[2] = *(short*)(&imu_frame[7]); 315 | 316 | imu_data[3] = *(short*)(&imu_frame[9]); 317 | imu_data[4] = *(short*)(&imu_frame[11]); 318 | imu_data[5] = *(short*)(&imu_frame[13]); 319 | 320 | 321 | imu_data[6] = *(int*)(&imu_frame[15]); 322 | imu_data[7] = *(int*)(&imu_frame[19]); 323 | imu_data[8] = *(int*)(&imu_frame[23]); 324 | imu_data[9] = *(int*)(&imu_frame[27]); 325 | 326 | imudata_struct->rx = 1.0*imu_data[0]/32768*2000; 327 | imudata_struct->ry = 1.0*imu_data[1]/32768*2000; 328 | imudata_struct->rz = 1.0*imu_data[2]/32768*2000; 329 | float facc_nobias[3]= { 330 | (float)(imu_data[3]-acc_offset[0]), 331 | (float)(imu_data[4]-acc_offset[1]), 332 | (float)(imu_data[5]-acc_offset[2])}; 333 | 334 | imudata_struct->ax = 1.0*facc_nobias[0]/16384*9.81; 335 | imudata_struct->ay = 1.0*facc_nobias[1]/16384*9.81; 336 | imudata_struct->az = 1.0*facc_nobias[2]/16384*9.81; 337 | 338 | float quat[4]= { 339 | (float)imu_data[6], 340 | (float)imu_data[7], 341 | (float)imu_data[8], 342 | (float)imu_data[9]}; 343 | float facc[3]= { 344 | (float)imu_data[3], 345 | (float)imu_data[4], 346 | (float)imu_data[5]}; 347 | 348 | static float faccg[3]= {0,0,0}; 349 | q_normalize(quat); 350 | imudata_struct->qw = quat[0]; 351 | imudata_struct->qx = quat[1]; 352 | imudata_struct->qy = quat[2]; 353 | imudata_struct->qz = quat[3]; 354 | 355 | imudata_struct->num = imu_frame[2]; 356 | 357 | q_rotvec(quat,facc_nobias,faccg); 358 | 359 | if(show_data) 360 | { 361 | printf("Num: %03d",imu_frame[2]); 362 | printf(" Gyr: %6d,%6d,%6d",imu_data[0],imu_data[1],imu_data[2]); 363 | printf(" Acc: %6d,%6d,%6d",(int)facc[0],(int)facc[1],(int)facc[2]); 364 | printf(" Quat: %11f,%11f,%11f,%11f\r\n",quat[0],quat[1],quat[2],quat[3]); 365 | } 366 | } 367 | -------------------------------------------------------------------------------- /camera_test/src/loitorusbcam.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | #include 21 | 22 | #include "loitorimu.h" 23 | 24 | typedef struct libusb_device cyusb_device; 25 | typedef struct libusb_device_handle cyusb_handle; 26 | 27 | /* This is the maximum number of 'devices of interest' we are willing to store as default. */ 28 | /* These are the maximum number of devices we will communicate with simultaneously */ 29 | #define MAXDEVICES 10 30 | 31 | /* This is the maximum number of VID/PID pairs that this library will consider. This limits 32 | the number of valid VID/PID entries in the configuration file. 33 | */ 34 | #define MAX_ID_PAIRS 100 35 | 36 | /* This is the maximum length for the description string for a device in the configuration 37 | file. If the actual string in the file is longer, only the first MAX_STR_LEN characters 38 | will be considered. 39 | */ 40 | #define MAX_STR_LEN 30 41 | 42 | #define LOG printf 43 | #define DEBUGLOG printf 44 | #define CODELOG printf("File:%s\r\nLine:%d \r\n",__FILE__,__LINE__); 45 | 46 | /*PARAMETER MACRO*/ 47 | #define IMG_WIDTH_VGA 640 48 | #define IMG_HEIGHT_VGA 480 49 | #define IMG_SIZE_VGA (IMG_WIDTH_VGA*IMG_HEIGHT_VGA) 50 | #define IMG_BUF_SIZE_VGA (IMG_SIZE_VGA+0x200) 51 | #define IMG_WIDTH_WVGA 752 52 | #define IMG_HEIGHT_WVGA 480 53 | #define IMG_SIZE_WVGA (IMG_WIDTH_WVGA*IMG_HEIGHT_WVGA) 54 | #define IMG_BUF_SIZE_WVGA (IMG_SIZE_WVGA+0x200) 55 | #define FRAME_CLUST 216 56 | 57 | /*CAMERA CONTROL INSTRUCTION MACRO*/ 58 | #define CAPTURE_27 0xA1 59 | #define CAPTURE_54 0xA2 60 | #define CAPTURE_108 0xA6 61 | #define STANDBY_SHORT 0xA3 62 | #define STANDBY_LONG 0xA4 63 | #define GET_CAM_LR 0xA5 64 | #define CAM_I2C_R 0xA7 65 | #define CAM_I2C_W 0xA8 66 | #define auto_expo 1 67 | #define EXP_VAL 50 68 | #define GAI_VAL 100 69 | 70 | #define CAM_I2C_ADDR 0x5C //0x5C 71 | 72 | /* Maximum length of a string read from the Configuration file (/etc/cyusb.conf) for the library. */ 73 | #define MAX_CFG_LINE_LENGTH (120) 74 | 75 | #define IMU_FRAME_LEN 32 76 | pthread_t imu_thread; 77 | int imu_fd=0; 78 | bool imu_close=false; 79 | 80 | using namespace std; 81 | 82 | 83 | visensor_imudata IMU_FIFO[200]; 84 | int imu_fifo_ct=0; 85 | 86 | bool shut_down_tag=false; 87 | 88 | bool left_fresh=false; 89 | bool right_fresh=false; 90 | 91 | bool allow_settings_change=true; 92 | 93 | /****************************************************/ 94 | int data_mode; 95 | int modes_settings[13][4]; 96 | int EG_mode; 97 | int man_exp; 98 | int man_gain; 99 | int auto_EG_top; 100 | int auto_EG_bottom; 101 | int auto_EG_des; 102 | int auto_E_man_G_Etop; 103 | int auto_E_man_G_Ebottom; 104 | int auto_E_man_G; 105 | int agc_aec_skip_frame; 106 | int VI_FIFO_matcher; 107 | 108 | double imu_acc_bias_X; 109 | double imu_acc_bias_Y; 110 | double imu_acc_bias_Z; 111 | 112 | string imu_port_name; 113 | visensor_imudata visensor_imudata_pack; 114 | /****************************************************/ 115 | 116 | cyusb_handle *pcam1_handle; 117 | cyusb_handle *pcam2_handle; 118 | int gFPS = 27; 119 | int gSelectCam = 0; 120 | int HORIZ_BLK=150; 121 | 122 | pthread_t cam1_capture_thread; 123 | pthread_t cam2_capture_thread; 124 | 125 | pthread_t cam1_capture_detect_thread; 126 | pthread_t cam2_capture_detect_thread; 127 | 128 | int gFrameCam1=0; 129 | int gFrameCam2=0; 130 | int gImg1Pass[FRAME_CLUST]; 131 | int gImg2Pass[FRAME_CLUST]; 132 | float gImg1Time[FRAME_CLUST]; // time-stamp 133 | float gImg2Time[FRAME_CLUST]; 134 | timeval gImg1_SysTime[FRAME_CLUST]; 135 | timeval gImg2_SysTime[FRAME_CLUST]; 136 | 137 | unsigned char gImg1_VGA[FRAME_CLUST][IMG_BUF_SIZE_VGA]; 138 | unsigned char gImg2_VGA[FRAME_CLUST][IMG_BUF_SIZE_VGA]; 139 | 140 | unsigned char gImg1_WVGA[FRAME_CLUST][IMG_BUF_SIZE_WVGA]; 141 | unsigned char gImg2_WVGA[FRAME_CLUST][IMG_BUF_SIZE_WVGA]; 142 | 143 | bool last_is_good=false; 144 | bool save_img=false; 145 | bool save_img1=false; 146 | 147 | bool visensor_resolution_status=false; 148 | int current_HB=194; 149 | int visensor_cam_selection=0; 150 | int current_FPS=27; 151 | 152 | string visensor_settings_file_name; 153 | 154 | 155 | struct cydev 156 | { 157 | cyusb_device *dev; 158 | cyusb_handle *handle; 159 | unsigned short vid; 160 | unsigned short pid; /* Product ID */ 161 | unsigned char is_open; /* When device is opened, val = 1 */ 162 | unsigned char busnum; /* The bus number of this device */ 163 | unsigned char devaddr; /* The device address*/ 164 | unsigned char filler; /* Padding to make struct = 16 bytes */ 165 | }; 166 | 167 | static struct cydev cydev[MAXDEVICES]; 168 | static int number_of_cameras; /* Number of Interesting Devices */ 169 | 170 | struct VPD 171 | { 172 | unsigned short vid; 173 | unsigned short pid; 174 | char desc[MAX_STR_LEN]; 175 | }; 176 | 177 | static struct VPD vpd[MAX_ID_PAIRS]; 178 | 179 | static libusb_device **libusb_device_list; 180 | static unsigned int checksum = 0; 181 | 182 | char pidfile[256]; 183 | char logfile[256]; 184 | int logfd; 185 | int pidfd; 186 | 187 | float visensor_get_hardware_fps(); 188 | 189 | 190 | void visensor_load_settings(const char* settings_file) 191 | { 192 | /******************************* Load Settings File ***************************************/ 193 | 194 | LOG("\r\n**********************************************\r\n"); 195 | LOG("Loading Settings File...\r\n"); 196 | 197 | visensor_settings_file_name=settings_file; 198 | 199 | ifstream ifs_settings(visensor_settings_file_name.c_str());//,ios::in|ios::binary|ios::ate); 200 | if(!ifs_settings ) 201 | { 202 | cerr << "Error opening file: "< settingsList; 218 | int linecount=0; 219 | while(std::getline(ifs_settings,line_settings)) 220 | { 221 | linecount++; 222 | if(line_settings.at(0)!='#') 223 | settingsList.push_back(line_settings); 224 | } 225 | //cout <<"line get end............"<::iterator iter = settingsList.begin(); iter != settingsList.end(); ++iter) 227 | { 228 | cout << *iter << endl; 229 | } 230 | ifs_settings.close(); 231 | 232 | 233 | std::string token; 234 | 235 | // convert to File_Settings Object 236 | istringstream(settingsList.at(1))>>data_mode; 237 | //cout << "data_mode: "<>_HB; 257 | 258 | //WVGA or VGA? 259 | std::getline(iss_line, token, ','); 260 | res=(token[0]=='V'?0:1); 261 | 262 | //FPS 263 | std::getline(iss_line, token, ','); 264 | istringstream(token)>>fps; 265 | 266 | // fill data 267 | if(i<9)// “HighSpeed Pre-Set” 268 | { 269 | modes_settings[i-3][0]=visensor_cam_selection; 270 | modes_settings[i-3][1]=_HB; 271 | modes_settings[i-3][2]=res; 272 | modes_settings[i-3][3]=fps; 273 | } 274 | else// “Normal Pre-Set” 275 | { 276 | modes_settings[i-4][0]=visensor_cam_selection; 277 | modes_settings[i-4][1]=_HB; 278 | modes_settings[i-4][2]=res; 279 | modes_settings[i-4][3]=fps; 280 | } 281 | 282 | } 283 | 284 | // Manual Mode 285 | { 286 | int visensor_cam_selection=0,_HB=0,res=0,fps=0; 287 | 288 | istringstream(settingsList.at(17))>>visensor_cam_selection; 289 | istringstream(settingsList.at(18))>>_HB; 290 | token = settingsList.at(19); 291 | res=(token[0]=='V'?0:1); 292 | istringstream(settingsList.at(20))>>fps; 293 | 294 | modes_settings[12][0]=visensor_cam_selection; 295 | modes_settings[12][1]=_HB; 296 | modes_settings[12][2]=res; 297 | modes_settings[12][3]=fps; 298 | } 299 | 300 | // EXP&GAIN 301 | istringstream(settingsList.at(22))>>EG_mode; 302 | 303 | // manual E&G 304 | { 305 | int _exp=0,_gain=0; 306 | 307 | istringstream iss_line(settingsList.at(23)); 308 | //The first word, ignore 309 | std::getline(iss_line, token, ','); 310 | 311 | std::getline(iss_line, token, ','); 312 | istringstream(token)>>_exp; 313 | std::getline(iss_line, token, ','); 314 | istringstream(token)>>_gain; 315 | 316 | // fill data 317 | man_exp=_exp; 318 | man_gain=_gain; 319 | } 320 | 321 | // auto 322 | { 323 | int _top=0,_bottom=0,_des=0; 324 | 325 | istringstream iss_line(settingsList.at(24)); 326 | //The first word, ignore 327 | std::getline(iss_line, token, ','); 328 | 329 | std::getline(iss_line, token, ','); 330 | istringstream(token)>>_top; 331 | 332 | std::getline(iss_line, token, ','); 333 | istringstream(token)>>_bottom; 334 | 335 | std::getline(iss_line, token, ','); 336 | istringstream(token)>>_des; 337 | // fill data 338 | auto_EG_top=_top; 339 | auto_EG_bottom=_bottom; 340 | auto_EG_des=_des; 341 | } 342 | 343 | // AE_MG 344 | { 345 | int _top=0,_bottom=0,_des=0,_gain=0; 346 | 347 | istringstream iss_line(settingsList.at(25)); 348 | 349 | //The first word, ignore 350 | std::getline(iss_line, token, ','); 351 | 352 | std::getline(iss_line, token, ','); 353 | istringstream(token)>>_top; 354 | std::getline(iss_line, token, ','); 355 | istringstream(token)>>_bottom; 356 | std::getline(iss_line, token, ','); 357 | istringstream(token)>>_des; 358 | std::getline(iss_line, token, ','); 359 | istringstream(token)>>_gain; 360 | // fill data 361 | auto_E_man_G_Etop=_top; 362 | auto_E_man_G_Ebottom=_bottom; 363 | auto_E_man_G=_gain; 364 | } 365 | // imu_port_name 366 | { 367 | istringstream iss_line(settingsList.at(26)); 368 | std::getline(iss_line, token, ','); 369 | 370 | imu_port_name = token.c_str(); 371 | 372 | std::getline(iss_line, token, ','); 373 | istringstream(token)>>VI_FIFO_matcher; 374 | } 375 | // imu acc bias 376 | { 377 | istringstream iss_line(settingsList.at(28)); 378 | //The first word, ignore 379 | std::getline(iss_line, token, ','); 380 | std::getline(iss_line, token, ','); 381 | istringstream(token)>>imu_acc_bias_X; 382 | } 383 | { 384 | istringstream iss_line(settingsList.at(29)); 385 | //The first word, ignore 386 | std::getline(iss_line, token, ','); 387 | std::getline(iss_line, token, ','); 388 | istringstream(token)>>imu_acc_bias_Y; 389 | } 390 | { 391 | istringstream iss_line(settingsList.at(30)); 392 | //The first word, ignore 393 | std::getline(iss_line, token, ','); 394 | std::getline(iss_line, token, ','); 395 | istringstream(token)>>imu_acc_bias_Z; 396 | } 397 | 398 | // show settings 399 | cout<13||_mode<=0) 565 | { 566 | cout<<"settings FIXED !"<=MAXDEVICES) 730 | break; 731 | } 732 | return number_of_cameras; 733 | } 734 | 735 | cyusb_handle * cyusb_gethandle(int index) 736 | { 737 | return cydev[index].handle; 738 | } 739 | 740 | int cyusb_control_transfer(cyusb_handle *h, unsigned char bmRequestType, unsigned char bRequest, 741 | unsigned short wValue, unsigned short wIndex, unsigned char *data, unsigned short wLength, 742 | unsigned int timeout) 743 | { 744 | return ( libusb_control_transfer(h, bmRequestType, bRequest, wValue, wIndex, data, wLength, timeout) ); 745 | } 746 | 747 | int cyusb_bulk_transfer(cyusb_handle *h, unsigned char endpoint, unsigned char *data, int length, 748 | int *transferred, int timeout) 749 | { 750 | return ( libusb_bulk_transfer(h, endpoint, data, length, transferred, timeout) ); 751 | } 752 | 753 | void cyusb_close(void) 754 | { 755 | for (int i = 0; i < number_of_cameras; ++i ) 756 | { 757 | libusb_close(cydev[i].handle); 758 | } 759 | libusb_free_device_list(libusb_device_list, 1); 760 | libusb_exit(NULL); 761 | } 762 | 763 | /*****************************************************************************/ 764 | 765 | int fps_control() 766 | { 767 | switch(gFPS) 768 | { 769 | case 27: 770 | return CAPTURE_27; 771 | break; 772 | case 54: 773 | return CAPTURE_54; 774 | break; 775 | case 108: 776 | return CAPTURE_108; 777 | break; 778 | default: 779 | return CAPTURE_27; 780 | break; 781 | } 782 | return CAPTURE_27; 783 | } 784 | 785 | cyusb_handle* get_cam_no_handle(int cam_no) 786 | { 787 | if(cam_no==1) 788 | return pcam1_handle; 789 | else if(cam_no==2) 790 | return pcam2_handle; 791 | else 792 | { 793 | printf("Wrong cam number!\r\n"); 794 | return NULL; 795 | } 796 | } 797 | 798 | int control_camera(int cam_no, unsigned char instruction) 799 | { 800 | unsigned char buf[1]; 801 | int r = cyusb_control_transfer(get_cam_no_handle(cam_no),0xC0,instruction,0,0,buf,0,1000); 802 | if(r != 0) 803 | { 804 | //printf("cam%d,i:0x%02X,r:%d\r\n",cam_no,instruction,r); 805 | } 806 | return r; 807 | } 808 | 809 | int camera_i2c_read(int cam_no, unsigned char reg,int* value) 810 | { 811 | unsigned char buf[3]; 812 | int r = cyusb_control_transfer(get_cam_no_handle(cam_no),0xC0,CAM_I2C_R,(CAM_I2C_ADDR<<8)+reg,0,buf,3,1000); 813 | if(r != 3) 814 | { 815 | //printf("cam%d,i:0x%02X,r:%d\r\n",cam_no,CAM_I2C_R,r); 816 | return r; 817 | } 818 | if(buf[0]!=0) 819 | { 820 | //printf("I2C Read failed...Cam:%d, Addr:0x%02X, Reg:0x%02X, I2C_return:%d\r\n",cam_no,CAM_I2C_ADDR,reg,buf[0]); 821 | return r; 822 | } 823 | *value = buf[1]+buf[2]<<8; 824 | //printf("I2C Read Success...Cam:%d, Addr:0x%02X, Reg:0x%02X, Result:0x%04X\r\n",cam_no,CAM_I2C_ADDR,reg,*value); 825 | return r; 826 | } 827 | 828 | int camera_i2c_write(int cam_no, unsigned char reg,int value) 829 | { 830 | unsigned char buf[1]; 831 | int r = cyusb_control_transfer(get_cam_no_handle(cam_no),0xC0,CAM_I2C_W,(CAM_I2C_ADDR<<8)+reg,value,buf,1,1000); 832 | if(r != 1) 833 | { 834 | //printf("cam%d,i:0x%02X,r:%d\r\n",cam_no,CAM_I2C_W,r); 835 | return r; 836 | } 837 | if(buf[0]!=0) 838 | { 839 | //printf("I2C Write failed...Cam:%d, Addr:0x%02X, Reg:0x%02X, Write:0x%04X, I2C_return:%d\r\n",cam_no,CAM_I2C_ADDR,reg,value,buf[0]); 840 | return r; 841 | } 842 | //printf("I2C Write Success...Cam:%d, Addr:0x%02X, Reg:0x%02X, Write:0x%04X\r\n",cam_no,CAM_I2C_ADDR,reg,value); 843 | return r; 844 | } 845 | 846 | int check_img(int cam_no,unsigned char *pImg,int *pImgPass) 847 | { 848 | if(!visensor_resolution_status)*pImgPass = (pImg[IMG_SIZE_VGA+0]==0xFF&&pImg[IMG_SIZE_VGA+1]==0x00&&pImg[IMG_SIZE_VGA+2]==0xFE&&pImg[IMG_SIZE_VGA+3]==0x01); 849 | else *pImgPass = (pImg[IMG_SIZE_WVGA+0]==0xFF&&pImg[IMG_SIZE_WVGA+1]==0x00&&pImg[IMG_SIZE_WVGA+2]==0xFE&&pImg[IMG_SIZE_WVGA+3]==0x01); 850 | if(*pImgPass == 0) 851 | { 852 | control_camera(cam_no,fps_control()); 853 | //printf("Img%d check error!\r\n",cam_no); 854 | } 855 | } 856 | 857 | float visensor_get_hardware_fps() 858 | { 859 | if(gFPS==54) 860 | { 861 | if(!visensor_resolution_status)return 45700.0f/(current_HB+640.0f); 862 | else return 45700.0f/(current_HB+752.0f); 863 | } 864 | else if(gFPS==27) 865 | { 866 | if(!visensor_resolution_status)return 0.5f*45700.0f/(current_HB+640.0f); 867 | else return 0.5f*45700.0f/(current_HB+752.0f); 868 | } 869 | } 870 | static void set_camreg_default(int cam_no) 871 | { 872 | camera_i2c_write(cam_no,0x00,0x1324); 873 | camera_i2c_write(cam_no,0x01,0x0001); 874 | camera_i2c_write(cam_no,0x02,0x0004); 875 | camera_i2c_write(cam_no,0x03,0x01E0); 876 | 877 | camera_i2c_write(cam_no,0x04,0x02F0); 878 | camera_i2c_write(cam_no,0x05,0x005E); 879 | camera_i2c_write(cam_no,0x06,0x002D); 880 | camera_i2c_write(cam_no,0x07,0x0388); 881 | 882 | camera_i2c_write(cam_no,0x0B,0x01E0); 883 | camera_i2c_write(cam_no,0x35,0x0010); 884 | camera_i2c_write(cam_no,0xA5,0x003A); 885 | camera_i2c_write(cam_no,0xA6,0x0002); 886 | 887 | camera_i2c_write(cam_no,0xA8,0x0000); 888 | camera_i2c_write(cam_no,0xA9,0x0002); 889 | camera_i2c_write(cam_no,0xAA,0x0002); 890 | camera_i2c_write(cam_no,0xAB,0x0040); 891 | 892 | camera_i2c_write(cam_no,0xAC,0x0001); 893 | camera_i2c_write(cam_no,0xAD,0x01E0); 894 | camera_i2c_write(cam_no,0xAE,0x0014); 895 | camera_i2c_write(cam_no,0xAF,0x0003); 896 | } 897 | 898 | void *cam1_capture(void*) 899 | { 900 | set_camreg_default(1); 901 | int r,transferred = 0; 902 | camera_i2c_write(1,0x07,0x0188);//Normal 903 | camera_i2c_write(1,0x06,0x002D);//VB 904 | // cmos设置 905 | camera_i2c_write(1,0x05,current_HB);//HB 906 | if(!visensor_resolution_status)camera_i2c_write(1,0x04,IMG_WIDTH_VGA);//HW 907 | else camera_i2c_write(1,0x04,IMG_WIDTH_WVGA);//HW 908 | switch (EG_mode) { 909 | case 0: 910 | camera_i2c_write(1,0xAF,0x00);//AEC 911 | camera_i2c_write(1,0x0B,man_exp);//Exposure Time 912 | camera_i2c_write(1,0x35,man_gain);//Gain 913 | break; 914 | case 1: 915 | camera_i2c_write(1,0xAF,0x03);//AEC 916 | camera_i2c_write(1,0xA5,auto_EG_des);//AEC 917 | camera_i2c_write(1,0xA6,0x01);//AEC 918 | camera_i2c_write(1,0xA8,0x00);//AEC 919 | camera_i2c_write(1,0xAC,auto_EG_bottom);//AEC 920 | camera_i2c_write(1,0xAD,auto_EG_top);//AEC 921 | camera_i2c_write(1,0xAE,2);//AEC 922 | break; 923 | case 2: 924 | camera_i2c_write(1,0xAF,0x01);//AEC 925 | camera_i2c_write(1,0xA5,auto_EG_des);//AEC 926 | camera_i2c_write(1,0xA6,0x01);//AEC 927 | camera_i2c_write(1,0xA8,0x00);//AEC 928 | camera_i2c_write(1,0xAC,auto_E_man_G_Ebottom);//AEC 929 | camera_i2c_write(1,0xAD,auto_E_man_G_Etop);//AEC 930 | camera_i2c_write(1,0xAE,2);//AEC 931 | camera_i2c_write(1,0x35,auto_E_man_G);//Gain 932 | break; 933 | case 3: 934 | break; 935 | case 4: 936 | camera_i2c_write(1,0xA6,agc_aec_skip_frame); 937 | camera_i2c_write(1,0xA8,2); 938 | camera_i2c_write(1,0xA9,agc_aec_skip_frame); 939 | camera_i2c_write(1,0xAA,2); 940 | break; 941 | default: 942 | break; 943 | } 944 | 945 | control_camera(1,fps_control()); 946 | // usleep(100); 947 | 948 | static int sync_ct=0; 949 | 950 | struct timeval endTime; 951 | float fTimeuse=0; 952 | 953 | int ctt=0; 954 | 955 | float time_offset=1.0f/visensor_get_hardware_fps(); 956 | control_camera(1,STANDBY_SHORT); 957 | while(!shut_down_tag) 958 | { 959 | ctt++; 960 | if(ctt>1) //该数值越大,则帧同步的周期越长,同步频率越小 961 | { 962 | control_camera(1,STANDBY_SHORT); 963 | ctt = 0; 964 | } 965 | for(gFrameCam1=0; !shut_down_tag&&gFrameCam1=abs(imu_usec-left_stamp.tv_usec)) 1189 | { 1190 | min_dist=abs(imu_usec-left_stamp.tv_usec); 1191 | min_id=i; 1192 | } 1193 | } 1194 | visensor_imudata bind_imuData=IMU_FIFO[min_id]; 1195 | return bind_imuData; 1196 | 1197 | } 1198 | else if(gImg2Pass[img2_pointer]&&gImg1Pass[img1_pointer]&&(visensor_resolution_status)) 1199 | { 1200 | memcpy(left_img,(char *)(gImg1_WVGA[img1_pointer]),IMG_BUF_SIZE_WVGA); 1201 | memcpy(right_img,(char *)(gImg2_WVGA[img2_pointer]),IMG_BUF_SIZE_WVGA); 1202 | gImg2Pass[img2_pointer]=0; 1203 | gImg1Pass[img1_pointer]=0; 1204 | 1205 | // 绑定最近时间戳的IMU数据 1206 | timeval left_stamp=gImg1_SysTime[img1_pointer]; 1207 | int min_id=0; 1208 | int min_dist=10000; 1209 | for(int i=0; i<200; i++) 1210 | { 1211 | int imu_usec=IMU_FIFO[i].system_time.tv_usec; 1212 | if(IMU_FIFO[i].imu_time!=0&&min_dist>=abs(imu_usec-left_stamp.tv_usec)) 1213 | { 1214 | min_dist=abs(imu_usec-left_stamp.tv_usec); 1215 | min_id=i; 1216 | } 1217 | } 1218 | visensor_imudata bind_imuData=IMU_FIFO[min_id]; 1219 | return bind_imuData; 1220 | } 1221 | 1222 | 1223 | } 1224 | visensor_imudata visensor_get_stereoImg(char* left_img,char* right_img,timeval &left_stamp,timeval &right_stamp) 1225 | { 1226 | 1227 | while(!visensor_is_stereo_good())usleep(200); 1228 | 1229 | int img1_pointer = (gFrameCam1 + FRAME_CLUST - 1) % FRAME_CLUST; 1230 | int img2_pointer = (gFrameCam2 + FRAME_CLUST - 1) % FRAME_CLUST; 1231 | if(gImg2Pass[img2_pointer]&&gImg1Pass[img1_pointer]&&(!visensor_resolution_status)) 1232 | { 1233 | memcpy(left_img,(char *)(gImg1_VGA[img1_pointer]),IMG_BUF_SIZE_VGA); 1234 | memcpy(right_img,(char *)(gImg2_VGA[img2_pointer]),IMG_BUF_SIZE_VGA); 1235 | left_stamp=gImg1_SysTime[img1_pointer]; 1236 | right_stamp=gImg2_SysTime[img2_pointer]; 1237 | gImg2Pass[img2_pointer]=0; 1238 | gImg1Pass[img1_pointer]=0; 1239 | 1240 | // 绑定最近时间戳的IMU数据 1241 | int min_id=0; 1242 | int min_dist=10000; 1243 | for(int i=0; i<200; i++) 1244 | { 1245 | int imu_usec=IMU_FIFO[i].system_time.tv_usec; 1246 | if(IMU_FIFO[i].imu_time!=0&&min_dist>=abs(imu_usec-left_stamp.tv_usec)) 1247 | { 1248 | min_dist=abs(imu_usec-left_stamp.tv_usec); 1249 | min_id=i; 1250 | } 1251 | } 1252 | visensor_imudata bind_imuData=IMU_FIFO[min_id]; 1253 | return bind_imuData; 1254 | } 1255 | else if(gImg2Pass[img2_pointer]&&gImg1Pass[img1_pointer]&&(visensor_resolution_status)) 1256 | { 1257 | memcpy(left_img,(char *)(gImg1_WVGA[img1_pointer]),IMG_BUF_SIZE_WVGA); 1258 | memcpy(right_img,(char *)(gImg2_WVGA[img2_pointer]),IMG_BUF_SIZE_WVGA); 1259 | left_stamp=gImg1_SysTime[img1_pointer]; 1260 | right_stamp=gImg2_SysTime[img2_pointer]; 1261 | gImg2Pass[img2_pointer]=0; 1262 | gImg1Pass[img1_pointer]=0; 1263 | 1264 | // 绑定最近时间戳的IMU数据 1265 | int min_id=0; 1266 | int min_dist=10000; 1267 | for(int i=0; i<200; i++) 1268 | { 1269 | int imu_usec=IMU_FIFO[i].system_time.tv_usec; 1270 | if(IMU_FIFO[i].imu_time!=0&&min_dist>=abs(imu_usec-left_stamp.tv_usec)) 1271 | { 1272 | min_dist=abs(imu_usec-left_stamp.tv_usec); 1273 | min_id=i; 1274 | } 1275 | } 1276 | visensor_imudata bind_imuData=IMU_FIFO[min_id]; 1277 | return bind_imuData; 1278 | } 1279 | } 1280 | 1281 | visensor_imudata visensor_get_leftImg(char* left_img) 1282 | { 1283 | 1284 | while(!visensor_is_left_good())usleep(200); 1285 | 1286 | int img1_pointer = (gFrameCam1 + FRAME_CLUST - 1) % FRAME_CLUST; 1287 | if(gImg1Pass[img1_pointer]&&(!visensor_resolution_status)) 1288 | { 1289 | memcpy(left_img,(char *)(gImg1_VGA[img1_pointer]),IMG_BUF_SIZE_VGA); 1290 | gImg1Pass[img1_pointer]=0; 1291 | 1292 | // 绑定最近时间戳的IMU数据 1293 | timeval left_stamp=gImg1_SysTime[img1_pointer]; 1294 | int min_id=0; 1295 | int min_dist=10000; 1296 | for(int i=0; i<200; i++) 1297 | { 1298 | int imu_usec=IMU_FIFO[i].system_time.tv_usec; 1299 | if(IMU_FIFO[i].imu_time!=0&&min_dist>=abs(imu_usec-left_stamp.tv_usec)) 1300 | { 1301 | min_dist=abs(imu_usec-left_stamp.tv_usec); 1302 | min_id=i; 1303 | } 1304 | } 1305 | visensor_imudata bind_imuData=IMU_FIFO[min_id]; 1306 | return bind_imuData; 1307 | } 1308 | else if(gImg1Pass[img1_pointer]&&(visensor_resolution_status)) 1309 | { 1310 | memcpy(left_img,(char *)(gImg1_WVGA[img1_pointer]),IMG_BUF_SIZE_WVGA); 1311 | gImg1Pass[img1_pointer]=0; 1312 | 1313 | // 绑定最近时间戳的IMU数据 1314 | timeval left_stamp=gImg1_SysTime[img1_pointer]; 1315 | int min_id=0; 1316 | int min_dist=10000; 1317 | for(int i=0; i<200; i++) 1318 | { 1319 | int imu_usec=IMU_FIFO[i].system_time.tv_usec; 1320 | if(IMU_FIFO[i].imu_time!=0&&min_dist>=abs(imu_usec-left_stamp.tv_usec)) 1321 | { 1322 | min_dist=abs(imu_usec-left_stamp.tv_usec); 1323 | min_id=i; 1324 | } 1325 | } 1326 | visensor_imudata bind_imuData=IMU_FIFO[min_id]; 1327 | return bind_imuData; 1328 | } 1329 | } 1330 | 1331 | visensor_imudata visensor_get_leftImg(char* left_img,timeval &left_stamp) 1332 | { 1333 | 1334 | while(!visensor_is_left_good())usleep(200); 1335 | 1336 | int img1_pointer = (gFrameCam1 + FRAME_CLUST - 1) % FRAME_CLUST; 1337 | if(gImg1Pass[img1_pointer]&&(!visensor_resolution_status)) 1338 | { 1339 | memcpy(left_img,(char *)(gImg1_VGA[img1_pointer]),IMG_BUF_SIZE_VGA); 1340 | left_stamp=gImg1_SysTime[img1_pointer]; 1341 | gImg1Pass[img1_pointer]=0; 1342 | 1343 | // 绑定最近时间戳的IMU数据 1344 | int min_id=0; 1345 | int min_dist=10000; 1346 | for(int i=0; i<200; i++) 1347 | { 1348 | int imu_usec=IMU_FIFO[i].system_time.tv_usec; 1349 | if(IMU_FIFO[i].imu_time!=0&&min_dist>=abs(imu_usec-left_stamp.tv_usec)) 1350 | { 1351 | min_dist=abs(imu_usec-left_stamp.tv_usec); 1352 | min_id=i; 1353 | } 1354 | } 1355 | visensor_imudata bind_imuData=IMU_FIFO[min_id]; 1356 | return bind_imuData; 1357 | } 1358 | else if(gImg1Pass[img1_pointer]&&(visensor_resolution_status)) 1359 | { 1360 | memcpy(left_img,(char *)(gImg1_WVGA[img1_pointer]),IMG_BUF_SIZE_WVGA); 1361 | left_stamp=gImg1_SysTime[img1_pointer]; 1362 | gImg1Pass[img1_pointer]=0; 1363 | 1364 | // 绑定最近时间戳的IMU数据 1365 | int min_id=0; 1366 | int min_dist=10000; 1367 | for(int i=0; i<200; i++) 1368 | { 1369 | int imu_usec=IMU_FIFO[i].system_time.tv_usec; 1370 | if(IMU_FIFO[i].imu_time!=0&&min_dist>=abs(imu_usec-left_stamp.tv_usec)) 1371 | { 1372 | min_dist=abs(imu_usec-left_stamp.tv_usec); 1373 | min_id=i; 1374 | } 1375 | } 1376 | visensor_imudata bind_imuData=IMU_FIFO[min_id]; 1377 | return bind_imuData; 1378 | } 1379 | } 1380 | 1381 | visensor_imudata visensor_get_rightImg(char* right_img) 1382 | { 1383 | 1384 | while(!visensor_is_right_good())usleep(200); 1385 | 1386 | int img2_pointer = (gFrameCam2 + FRAME_CLUST - 1) % FRAME_CLUST; 1387 | 1388 | if(gImg2Pass[img2_pointer]&&(!visensor_resolution_status)) 1389 | { 1390 | memcpy(right_img,(char *)(gImg2_VGA[img2_pointer]),IMG_BUF_SIZE_VGA); 1391 | gImg2Pass[img2_pointer]=0; 1392 | 1393 | // 绑定最近时间戳的IMU数据 1394 | timeval right_stamp=gImg2_SysTime[img2_pointer]; 1395 | int min_id=0; 1396 | int min_dist=10000; 1397 | for(int i=0; i<200; i++) 1398 | { 1399 | int imu_usec=IMU_FIFO[i].system_time.tv_usec; 1400 | if(IMU_FIFO[i].imu_time!=0&&min_dist>=abs(imu_usec-right_stamp.tv_usec)) 1401 | { 1402 | min_dist=abs(imu_usec-right_stamp.tv_usec); 1403 | min_id=i; 1404 | } 1405 | } 1406 | visensor_imudata bind_imuData=IMU_FIFO[min_id]; 1407 | return bind_imuData; 1408 | } 1409 | else if(gImg2Pass[img2_pointer]&&(visensor_resolution_status)) 1410 | { 1411 | memcpy(right_img,(char *)(gImg2_WVGA[img2_pointer]),IMG_BUF_SIZE_WVGA); 1412 | gImg2Pass[img2_pointer]=0; 1413 | 1414 | // 绑定最近时间戳的IMU数据 1415 | timeval right_stamp=gImg2_SysTime[img2_pointer]; 1416 | int min_id=0; 1417 | int min_dist=10000; 1418 | for(int i=0; i<200; i++) 1419 | { 1420 | int imu_usec=IMU_FIFO[i].system_time.tv_usec; 1421 | if(IMU_FIFO[i].imu_time!=0&&min_dist>=abs(imu_usec-right_stamp.tv_usec)) 1422 | { 1423 | min_dist=abs(imu_usec-right_stamp.tv_usec); 1424 | min_id=i; 1425 | } 1426 | } 1427 | visensor_imudata bind_imuData=IMU_FIFO[min_id]; 1428 | return bind_imuData; 1429 | } 1430 | } 1431 | visensor_imudata visensor_get_rightImg(char* right_img,timeval &right_stamp) 1432 | { 1433 | 1434 | while(!visensor_is_right_good())usleep(200); 1435 | 1436 | int img2_pointer = (gFrameCam2 + FRAME_CLUST - 1) % FRAME_CLUST; 1437 | 1438 | if(gImg2Pass[img2_pointer]&&(!visensor_resolution_status)) 1439 | { 1440 | memcpy(right_img,(char *)(gImg2_VGA[img2_pointer]),IMG_BUF_SIZE_VGA); 1441 | right_stamp=gImg2_SysTime[img2_pointer]; 1442 | gImg2Pass[img2_pointer]=0; 1443 | 1444 | // 绑定最近时间戳的IMU数据 1445 | int min_id=0; 1446 | int min_dist=10000; 1447 | for(int i=0; i<200; i++) 1448 | { 1449 | int imu_usec=IMU_FIFO[i].system_time.tv_usec; 1450 | if(IMU_FIFO[i].imu_time!=0&&min_dist>=abs(imu_usec-right_stamp.tv_usec)) 1451 | { 1452 | min_dist=abs(imu_usec-right_stamp.tv_usec); 1453 | min_id=i; 1454 | } 1455 | } 1456 | visensor_imudata bind_imuData=IMU_FIFO[min_id]; 1457 | return bind_imuData; 1458 | } 1459 | else if(gImg2Pass[img2_pointer]&&(visensor_resolution_status)) 1460 | { 1461 | memcpy(right_img,(char *)(gImg2_WVGA[img2_pointer]),IMG_BUF_SIZE_WVGA); 1462 | right_stamp=gImg2_SysTime[img2_pointer]; 1463 | gImg2Pass[img2_pointer]=0; 1464 | 1465 | // 绑定最近时间戳的IMU数据 1466 | int min_id=0; 1467 | int min_dist=10000; 1468 | for(int i=0; i<200; i++) 1469 | { 1470 | int imu_usec=IMU_FIFO[i].system_time.tv_usec; 1471 | if(IMU_FIFO[i].imu_time!=0&&min_dist>=abs(imu_usec-right_stamp.tv_usec)) 1472 | { 1473 | min_dist=abs(imu_usec-right_stamp.tv_usec); 1474 | min_id=i; 1475 | } 1476 | } 1477 | visensor_imudata bind_imuData=IMU_FIFO[min_id]; 1478 | return bind_imuData; 1479 | } 1480 | } 1481 | 1482 | int visensor_Start_Cameras() 1483 | { 1484 | gSelectCam = 3; 1485 | allow_settings_change=false; 1486 | 1487 | shut_down_tag=false; 1488 | 1489 | int ret_val=0; 1490 | 1491 | DEBUGLOG("Now opening cameras...\r\n"); 1492 | //Open usb devices 1493 | int r; 1494 | r = cyusb_open(); 1495 | if(r<1) 1496 | { 1497 | LOG("Error: No cameras found! r = %d\r\n",r); 1498 | return -2; 1499 | } 1500 | else 1501 | { 1502 | printf("Number of device of interest found: %d\r\n",r); 1503 | } 1504 | //Get camera position 1505 | cyusb_handle *pusb_handle; 1506 | for(int i=0; i loitor-vi.rules 3 | echo 'KERNEL=="*", SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", ACTION=="add", ATTR{idVendor}=="04b4", MODE="666" ' >> loitor-vi.rules 4 | echo 'KERNEL=="*", SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", ACTION=="remove" ' >> loitor-vi.rules 5 | } 6 | 7 | if [ `whoami` != 'root' ]; then 8 | echo "You have to be root to run this script" 9 | exit 1; 10 | fi 11 | 12 | create_udev_rules 13 | cp loitor-vi.rules /etc/udev/rules.d/ 14 | -------------------------------------------------------------------------------- /loitor_PCcam_cliper.stl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/loitor-vis/vi_sensor_sdk/eb54545ef5eeb681a5271725e8a9d9a21c97367a/loitor_PCcam_cliper.stl -------------------------------------------------------------------------------- /loitor_ros_workspace/src/loitor_stereo_visensor/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8.3) 2 | project(loitor_stereo_visensor) 3 | 4 | find_package(catkin REQUIRED COMPONENTS 5 | roscpp 6 | rospy 7 | std_msgs 8 | cv_bridge 9 | image_transport 10 | message_generation 11 | sensor_msgs 12 | ) 13 | 14 | catkin_package( 15 | CATKIN_DEPENDS 16 | cv_bridge 17 | image_transport 18 | message_runtime 19 | sensor_msgs 20 | ) 21 | 22 | include_directories( 23 | ${catkin_INCLUDE_DIRS} 24 | ) 25 | 26 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread") 27 | 28 | find_package(OpenCV REQUIRED) 29 | 30 | include_directories(include 31 | ${catkin_INCLUDE_DIRS} 32 | ${OpenCV_INCLUDE_DIRS}) 33 | 34 | include_directories("./include/loitor_stereo_visensor") 35 | 36 | add_library(loitorusbcam src/loitorusbcam.cpp) 37 | target_link_libraries(loitorusbcam loitorimu) 38 | 39 | add_library(loitorimu src/loitorimu.cpp) 40 | 41 | add_executable(loitor_stereo_visensor src/loitor_stereo_visensor.cpp) 42 | target_link_libraries(loitor_stereo_visensor ${catkin_LIBRARIES}) 43 | target_link_libraries(loitor_stereo_visensor loitorusbcam loitorimu usb-1.0 ${OpenCV_LIBS}) 44 | add_dependencies(loitor_stereo_visensor loitor_vis_generate_messages_cpp) 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /loitor_ros_workspace/src/loitor_stereo_visensor/Loitor_VISensor_Setups.txt: -------------------------------------------------------------------------------- 1 | # 2 | Mode 3 | 12 4 | # 5 | HighSpeed Pre-Set 6 | m1,left,200,VGA,54 7 | m2,right,250,VGA,54 8 | m3,left,70,WVGA,54 9 | m4,right,70,WVGA,54 10 | m5,stereo,280,VGA,54 11 | m6,stereo,281,WVGA,54 12 | # 13 | Normal Pre-Set 14 | m7,left,150,VGA,54 15 | m8,right,150,VGA,54 16 | m9,left,150,WVGA,54 17 | m10,right,150,WVGA,54 18 | m11,stereo,183,VGA,27 19 | m12,stereo,250,WVGA,27 20 | # 21 | m13,Manual Mode 22 | 0 23 | 194 24 | VGA 25 | 54 26 | # 27 | EG_mode 28 | 3 29 | manual,50,200 30 | auto,300,5,58 31 | autoexp_manualgain,300,5,58,200 32 | /dev/ttyUSB0,5 33 | # 34 | IMU-acc-bias 35 | Gx,758 36 | Gy,-2 37 | Gz,-2608 38 | # 39 | -------------------------------------------------------------------------------- /loitor_ros_workspace/src/loitor_stereo_visensor/cfg/Loitor_VISensor_Setups.txt: -------------------------------------------------------------------------------- 1 | # 2 | Mode 3 | 6 4 | # 5 | HighSpeed Pre-Set 6 | m1,left,70,VGA,54 7 | m2,right,70,VGA,54 8 | m3,left,70,WVGA,54 9 | m4,right,70,WVGA,54 10 | m5,stereo,280,VGA,54 11 | m6,stereo,281,WVGA,54 12 | # 13 | Normal Pre-Set 14 | m7,left,150,VGA,54 15 | m8,right,150,VGA,54 16 | m9,left,150,WVGA,54 17 | m10,right,150,WVGA,54 18 | m11,stereo,183,VGA,27 19 | m12,stereo,194,WVGA,27 20 | # 21 | m13,Manual Mode 22 | 0 23 | 194 24 | VGA 25 | 54 26 | # 27 | EG_mode 28 | 3 29 | manual,100,100 30 | auto,300,5,58 31 | autoexp_manualgain,300,5,58,100 32 | /dev/ttyUSB0,5 33 | # 34 | IMU-acc-bias 35 | Gx,346 36 | Gy,129 37 | Gz,-1616 38 | # 39 | -------------------------------------------------------------------------------- /loitor_ros_workspace/src/loitor_stereo_visensor/cfg/backup_settings.txt: -------------------------------------------------------------------------------- 1 | # 2 | Mode 3 | 6 4 | # 5 | HighSpeed Pre-Set 6 | m1,left,70,VGA,108 7 | m2,right,70,VGA,108 8 | m3,left,70,WVGA,108 9 | m4,right,70,WVGA,108 10 | m5,stereo,280,VGA,54 11 | m6,stereo,281,WVGA,54 12 | # 13 | Normal Pre-Set 14 | m7,left,150,VGA,54 15 | m8,right,150,VGA,54 16 | m9,left,150,WVGA,54 17 | m10,right,150,WVGA,54 18 | m11,stereo,183,VGA,27 19 | m12,stereo,194,WVGA,27 20 | # 21 | m13,Manual Mode 22 | 0 23 | 194 24 | VGA 25 | 54 26 | # 27 | EG_mode 28 | 3 29 | manual,100,100 30 | auto,300,5,58 31 | autoexp_manualgain,300,5,58,100 32 | /dev/ttyUSB0,5 33 | # 34 | IMU-acc-bias 35 | Gx,346.000000 36 | Gy,129.000000 37 | Gz,-1616.000000 38 | # 39 | -------------------------------------------------------------------------------- /loitor_ros_workspace/src/loitor_stereo_visensor/include/loitor_stereo_visensor/loitorimu.h: -------------------------------------------------------------------------------- 1 | #ifndef LOITORIMU_H 2 | #define LOITORIMU_H 3 | 4 | #include 5 | 6 | typedef struct 7 | { 8 | float imu_time; // time-stamp 9 | timeval system_time; 10 | unsigned char num; 11 | float rx,ry,rz; 12 | float ax,ay,az; 13 | float qw,qx,qy,qz; 14 | 15 | }visensor_imudata; 16 | 17 | extern timeval visensor_startTime; 18 | 19 | bool visensor_query_imu_update(); 20 | bool visensor_mark_imu_update(); 21 | bool visensor_erase_imu_update(); 22 | int visensor_set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop); 23 | int visensor_open_port(const char* dev_str); 24 | int visensor_get_imu_frame(int fd, unsigned char* imu_frame); 25 | int visensor_get_imu_data(unsigned char* imu_frame,short int* acc_offset,visensor_imudata *imudata_struct,bool show_data); 26 | int visensor_send_imu_frame(int fd, unsigned char* data, int len); 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /loitor_ros_workspace/src/loitor_stereo_visensor/include/loitor_stereo_visensor/loitorusbcam.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include "loitorimu.h" 3 | 4 | #ifndef LOITORUSBCAM_H 5 | #define LOITORUSBCAM_H 6 | #define IMU_FRAME_LEN 32 7 | #define IMG_WIDTH_VGA 640 8 | #define IMG_HEIGHT_VGA 480 9 | #define IMG_SIZE_VGA (IMG_WIDTH_VGA*IMG_HEIGHT_VGA) 10 | #define IMG_BUF_SIZE_VGA (IMG_SIZE_VGA+0x200) 11 | #define IMG_WIDTH_WVGA 752 12 | #define IMG_HEIGHT_WVGA 480 13 | #define IMG_SIZE_WVGA (IMG_WIDTH_WVGA*IMG_HEIGHT_WVGA) 14 | #define IMG_BUF_SIZE_WVGA (IMG_SIZE_WVGA+0x200) 15 | 16 | /* 17 | * camera当前分辨率状态 18 | * 0-代表VGA 19 | * 1-代表WVGA 20 | */ 21 | extern bool visensor_resolution_status; // 0-VGA | 1-WVGA 22 | /* 23 | * camera当前通道选择 24 | * 0-代表开启双目 25 | * 1-代表只开启右眼 26 | * 2-代表只开启左眼 27 | */ 28 | extern int visensor_cam_selection; // 0-stereo | 1-right | 2-left 29 | /* 30 | * imu 数据 31 | */ 32 | extern visensor_imudata visensor_imudata_pack; 33 | 34 | // setters & getters 35 | void visensor_set_auto_EG(int E_AG); // 0-ManEG | 1-AutoEG with limits | 2-AutoE&ManG | 3- fully auto 36 | void visensor_set_exposure(int _man_exp); 37 | void visensor_set_gain(int _man_gain); 38 | void visensor_set_max_autoExp(int max_exp); 39 | void visensor_set_min_autoExp(int min_exp); 40 | void visensor_set_resolution(bool set_wvga); 41 | void visensor_set_fps_mode(bool fps_mode); 42 | void visensor_set_current_HB(int HB); 43 | void visensor_set_desired_bin(int db); 44 | void visensor_set_cam_selection_mode(int _visensor_cam_selection); 45 | void visensor_set_imu_bias(float bx,float by,float bz); 46 | void visensor_set_imu_portname(char* input_name); 47 | void visensor_set_current_mode(int _mode); 48 | 49 | int visensor_get_EG_mode(); 50 | int visensor_get_exposure(); 51 | int visensor_get_gain(); 52 | int visensor_get_max_autoExp(); 53 | int visensor_get_min_autoExp(); 54 | bool visensor_get_resolution(); 55 | int visensor_get_fps(); 56 | int visensor_get_current_HB(); 57 | int visensor_get_desired_bin(); 58 | int visensor_get_cam_selection_mode(); 59 | float visensor_get_imu_G_bias_x(); 60 | float visensor_get_imu_G_bias_y(); 61 | float visensor_get_imu_G_bias_z(); 62 | const char* visensor_get_imu_portname(); 63 | 64 | void visensor_save_current_settings(); 65 | 66 | float visensor_get_hardware_fps(); 67 | 68 | void visensor_load_settings(const char* settings_file); 69 | 70 | bool visensor_is_stereo_good(); 71 | bool visensor_is_left_good(); 72 | bool visensor_is_right_good(); 73 | 74 | bool visensor_is_left_fresh(); 75 | bool visensor_is_right_fresh(); 76 | 77 | /* 78 | * 得到绑定了同步IMU数据之后的图像数据 79 | */ 80 | visensor_imudata visensor_get_stereoImg(char* left_img,char* right_img); 81 | visensor_imudata visensor_get_stereoImg(char* left_img,char* right_img,timeval &left_stamp,timeval &right_stamp); 82 | 83 | visensor_imudata visensor_get_leftImg(char* left_img); 84 | visensor_imudata visensor_get_leftImg(char* left_img,timeval &left_stamp); 85 | 86 | visensor_imudata visensor_get_rightImg(char* right_img); 87 | visensor_imudata visensor_get_rightImg(char* right_img,timeval &right_stamp); 88 | 89 | int visensor_Start_Cameras(); 90 | void visensor_Close_Cameras(); 91 | 92 | bool visensor_imu_have_fresh_data(); 93 | 94 | int visensor_Start_IMU(); 95 | void visensor_Close_IMU(); 96 | 97 | 98 | #endif 99 | -------------------------------------------------------------------------------- /loitor_ros_workspace/src/loitor_stereo_visensor/package.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | loitor_stereo_visensor 4 | 0.0.0 5 | The loitor_stereo_visensor package 6 | 7 | 8 | 9 | 10 | root 11 | 12 | 13 | 14 | 15 | 16 | TODO 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | catkin 43 | cv_bridge 44 | image_transport 45 | opencv2 46 | roscpp 47 | rospy 48 | sensor_msgs 49 | std_msgs 50 | cv_bridge 51 | image_transport 52 | message_runtime 53 | opencv2 54 | roscpp 55 | rospy 56 | sensor_msgs 57 | std_msgs 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /loitor_ros_workspace/src/loitor_stereo_visensor/src/loitor_stereo_visensor.cpp: -------------------------------------------------------------------------------- 1 | #include "ros/ros.h" 2 | #include "std_msgs/String.h" 3 | #include 4 | #include 5 | #include "sensor_msgs/Imu.h" 6 | 7 | #include 8 | #include 9 | #include "cxcore.hpp" 10 | #include 11 | #include 12 | #include 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | #include "loitorusbcam.h" 19 | #include "loitorimu.h" 20 | 21 | #include 22 | 23 | 24 | using namespace std; 25 | using namespace cv; 26 | 27 | 28 | ros::Publisher pub_imu; 29 | 30 | /* 31 | * 用于构造cv::Mat 的左右眼图像 32 | */ 33 | cv::Mat img_left; 34 | cv::Mat img_right; 35 | 36 | /* 37 | * 当前左右图像的时间戳 38 | */ 39 | timeval left_stamp,right_stamp; 40 | 41 | /* 42 | * imu viewer 43 | */ 44 | bool visensor_Close_IMU_viewer=false; 45 | bool imu_start_transfer=false; 46 | void* imu_data_stream(void *) 47 | { 48 | int counter=0; 49 | imu_start_transfer=false; 50 | 51 | 52 | while((!visensor_Close_IMU_viewer)&&!imu_start_transfer)usleep(1000); 53 | while(!visensor_Close_IMU_viewer) 54 | { 55 | if(visensor_imu_have_fresh_data()) 56 | { 57 | counter++; 58 | // 每隔20帧显示一次imu数据 59 | if(counter>=20) 60 | { 61 | //cout<<"visensor_imudata_pack->a : "<a : "<("imu0", 200); 161 | 162 | // publish 到这两个 topic 163 | image_transport::ImageTransport it(n); 164 | image_transport::Publisher pub = it.advertise("/cam0/image_raw", 1); 165 | sensor_msgs::ImagePtr msg; 166 | 167 | image_transport::ImageTransport it1(n); 168 | image_transport::Publisher pub1 = it1.advertise("/cam1/image_raw", 1); 169 | sensor_msgs::ImagePtr msg1; 170 | 171 | // 使用camera硬件帧率设置发布频率 172 | ros::Rate loop_rate((int)hardware_fps); 173 | 174 | int static_ct=0; 175 | 176 | timeval img_time_test,img_time_offset; 177 | img_time_test.tv_usec=0; 178 | img_time_test.tv_sec=0; 179 | img_time_offset.tv_usec=50021; 180 | img_time_offset.tv_sec=0; 181 | 182 | while (ros::ok()) 183 | { 184 | imu_start_transfer=true; 185 | 186 | //cout<<"visensor_get_hardware_fps() ==== "<=5) 275 | { 276 | pub.publish(msg); 277 | static_ct=0; 278 | } 279 | } 280 | 281 | ros::spinOnce(); 282 | 283 | loop_rate.sleep(); 284 | 285 | } 286 | 287 | /* shut-down viewers */ 288 | visensor_Close_IMU_viewer=true; 289 | if(imu_data_thread !=0) 290 | { 291 | pthread_join(imu_data_thread,NULL); 292 | } 293 | 294 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include "math.h" 12 | 13 | #define IMU_FRAME_LEN 32 14 | #include "loitorimu.h" 15 | 16 | bool shut_down_imu=false; 17 | 18 | timeval visensor_startTime; // time-stamp 19 | struct timeval imu_SysTime; 20 | 21 | float imu_time=0; 22 | bool visensor_imu_updated=false; 23 | 24 | bool visensor_query_imu_update() 25 | { 26 | return visensor_imu_updated; 27 | } 28 | bool visensor_mark_imu_update() 29 | { 30 | visensor_imu_updated=true; 31 | } 32 | bool visensor_erase_imu_update() 33 | { 34 | visensor_imu_updated=false; 35 | } 36 | 37 | int visensor_set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop) 38 | { 39 | struct termios newtio,oldtio; 40 | if ( tcgetattr( fd,&oldtio) != 0) 41 | { 42 | perror("SetupSerial 1"); 43 | return -1; 44 | } 45 | bzero( &newtio, sizeof( newtio ) ); 46 | newtio.c_cflag |= CLOCAL | CREAD; 47 | newtio.c_cflag &= ~CSIZE; 48 | 49 | switch( nBits ) 50 | { 51 | case 7: 52 | newtio.c_cflag |= CS7; 53 | break; 54 | case 8: 55 | newtio.c_cflag |= CS8; 56 | break; 57 | } 58 | 59 | switch( nEvent ) 60 | { 61 | case 'O': 62 | newtio.c_cflag |= PARENB; 63 | newtio.c_cflag |= PARODD; 64 | newtio.c_iflag |= (INPCK | ISTRIP); 65 | break; 66 | case 'E': 67 | newtio.c_iflag |= (INPCK | ISTRIP); 68 | newtio.c_cflag |= PARENB; 69 | newtio.c_cflag &= ~PARODD; 70 | break; 71 | case 'N': 72 | newtio.c_cflag &= ~PARENB; 73 | break; 74 | } 75 | 76 | switch( nSpeed ) 77 | { 78 | case 2400: 79 | cfsetispeed(&newtio, B2400); 80 | cfsetospeed(&newtio, B2400); 81 | break; 82 | case 4800: 83 | cfsetispeed(&newtio, B4800); 84 | cfsetospeed(&newtio, B4800); 85 | break; 86 | case 9600: 87 | cfsetispeed(&newtio, B9600); 88 | cfsetospeed(&newtio, B9600); 89 | break; 90 | case 115200: 91 | cfsetispeed(&newtio, B115200); 92 | cfsetospeed(&newtio, B115200); 93 | break; 94 | default: 95 | cfsetispeed(&newtio, B9600); 96 | cfsetospeed(&newtio, B9600); 97 | break; 98 | } 99 | if( nStop == 1 ) 100 | newtio.c_cflag &= ~CSTOPB; 101 | else if ( nStop == 2 ) 102 | newtio.c_cflag |= CSTOPB; 103 | newtio.c_cc[VTIME] = 0; 104 | newtio.c_cc[VMIN] = 32; 105 | tcflush(fd,TCIFLUSH); 106 | if((tcsetattr(fd,TCSANOW,&newtio))!=0) 107 | { 108 | perror("com set error"); 109 | return -1; 110 | } 111 | printf("set done!\n"); 112 | return 0; 113 | } 114 | 115 | int visensor_open_port(const char* dev_str) 116 | { 117 | long vdisable; 118 | 119 | int fd = open( dev_str, O_RDWR|O_NOCTTY|O_NDELAY); 120 | if (-1 == fd) 121 | { 122 | perror("Can't Open Serial Port"); 123 | return(-1); 124 | } 125 | 126 | if(fcntl(fd, F_SETFL, 0)<0) 127 | printf("fcntl failed!\n"); 128 | else 129 | printf("fcntl=%d\n",fcntl(fd, F_SETFL,0)); 130 | if(isatty(STDIN_FILENO)==0) 131 | printf("standard input is not a terminal device\n"); 132 | else 133 | printf("isatty success!\n"); 134 | printf("fd-open=%d\n",fd); 135 | return fd; 136 | } 137 | 138 | static int find_55aa(unsigned char* buf,int len) 139 | { 140 | int i; 141 | for(i=0; i 159 | 160 | int visensor_get_imu_frame(int fd, unsigned char* imu_frame) 161 | { 162 | 163 | unsigned char imu_frame_buf[2*IMU_FRAME_LEN]; 164 | memset(imu_frame,0,IMU_FRAME_LEN); 165 | memset(imu_frame_buf,0,2*IMU_FRAME_LEN); 166 | int num_get = 0; 167 | 168 | static int flush_count = 0; 169 | flush_count = (flush_count+1)%200; 170 | if(flush_count==0) 171 | tcflush(fd,TCIFLUSH); 172 | static struct timeval getIMUTime; 173 | struct timeval endTime; 174 | float fTimeuse; 175 | while(!shut_down_imu) 176 | { 177 | //读到32个以上字节 178 | while(num_get32) 237 | { 238 | tcflush(fd,TCIFLUSH); 239 | memset(imu_frame_buf,0,2*IMU_FRAME_LEN); 240 | num_get = 0; 241 | continue; 242 | } 243 | //如果校验和正确,那么输出该帧数据 244 | memcpy(imu_frame,&imu_frame_buf[position_55aa],IMU_FRAME_LEN); 245 | static float time_interval=5000; 246 | static float last_time,new_time,new_pred,last_pred; 247 | new_time = getIMUTime.tv_usec; 248 | int newtime_interval; 249 | newtime_interval = new_time-last_time; 250 | if(newtime_interval<4000||newtime_interval>6000) 251 | newtime_interval = 5000; 252 | time_interval = time_interval*0.999+0.001*newtime_interval; 253 | if(new_time-last_predimu_time=imu_time; 305 | imudata_struct->system_time=imu_SysTime; 306 | 307 | static int last_imu_no; 308 | //for(int i=1; i<(imu_frame[2]+200-last_imu_no)%200; i++) 309 | // printf(".................................................................................\r\n"); 310 | last_imu_no = imu_frame[2]; 311 | int imu_data[10]; 312 | imu_data[0] = *(short*)(&imu_frame[3]); 313 | imu_data[1] = *(short*)(&imu_frame[5]); 314 | imu_data[2] = *(short*)(&imu_frame[7]); 315 | 316 | imu_data[3] = *(short*)(&imu_frame[9]); 317 | imu_data[4] = *(short*)(&imu_frame[11]); 318 | imu_data[5] = *(short*)(&imu_frame[13]); 319 | 320 | 321 | imu_data[6] = *(int*)(&imu_frame[15]); 322 | imu_data[7] = *(int*)(&imu_frame[19]); 323 | imu_data[8] = *(int*)(&imu_frame[23]); 324 | imu_data[9] = *(int*)(&imu_frame[27]); 325 | 326 | imudata_struct->rx = 1.0*imu_data[0]/32768*2000; 327 | imudata_struct->ry = 1.0*imu_data[1]/32768*2000; 328 | imudata_struct->rz = 1.0*imu_data[2]/32768*2000; 329 | float facc_nobias[3]= { 330 | (float)(imu_data[3]-acc_offset[0]), 331 | (float)(imu_data[4]-acc_offset[1]), 332 | (float)(imu_data[5]-acc_offset[2])}; 333 | 334 | imudata_struct->ax = 1.0*facc_nobias[0]/16384*9.81; 335 | imudata_struct->ay = 1.0*facc_nobias[1]/16384*9.81; 336 | imudata_struct->az = 1.0*facc_nobias[2]/16384*9.81; 337 | 338 | float quat[4]= { 339 | (float)imu_data[6], 340 | (float)imu_data[7], 341 | (float)imu_data[8], 342 | (float)imu_data[9]}; 343 | float facc[3]= { 344 | (float)imu_data[3], 345 | (float)imu_data[4], 346 | (float)imu_data[5]}; 347 | 348 | static float faccg[3]= {0,0,0}; 349 | q_normalize(quat); 350 | imudata_struct->qw = quat[0]; 351 | imudata_struct->qx = quat[1]; 352 | imudata_struct->qy = quat[2]; 353 | imudata_struct->qz = quat[3]; 354 | 355 | imudata_struct->num = imu_frame[2]; 356 | 357 | q_rotvec(quat,facc_nobias,faccg); 358 | 359 | if(show_data) 360 | { 361 | printf("Num: %03d",imu_frame[2]); 362 | printf(" Gyr: %6d,%6d,%6d",imu_data[0],imu_data[1],imu_data[2]); 363 | printf(" Acc: %6d,%6d,%6d",(int)facc[0],(int)facc[1],(int)facc[2]); 364 | printf(" Quat: %11f,%11f,%11f,%11f\r\n",quat[0],quat[1],quat[2],quat[3]); 365 | } 366 | } 367 | -------------------------------------------------------------------------------- /loitor_ros_workspace/src/loitor_stereo_visensor/src/loitorusbcam.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | #include 21 | 22 | #include "loitorimu.h" 23 | 24 | typedef struct libusb_device cyusb_device; 25 | typedef struct libusb_device_handle cyusb_handle; 26 | 27 | /* This is the maximum number of 'devices of interest' we are willing to store as default. */ 28 | /* These are the maximum number of devices we will communicate with simultaneously */ 29 | #define MAXDEVICES 10 30 | 31 | /* This is the maximum number of VID/PID pairs that this library will consider. This limits 32 | the number of valid VID/PID entries in the configuration file. 33 | */ 34 | #define MAX_ID_PAIRS 100 35 | 36 | /* This is the maximum length for the description string for a device in the configuration 37 | file. If the actual string in the file is longer, only the first MAX_STR_LEN characters 38 | will be considered. 39 | */ 40 | #define MAX_STR_LEN 30 41 | 42 | #define LOG printf 43 | #define DEBUGLOG printf 44 | #define CODELOG printf("File:%s\r\nLine:%d \r\n",__FILE__,__LINE__); 45 | 46 | /*PARAMETER MACRO*/ 47 | #define IMG_WIDTH_VGA 640 48 | #define IMG_HEIGHT_VGA 480 49 | #define IMG_SIZE_VGA (IMG_WIDTH_VGA*IMG_HEIGHT_VGA) 50 | #define IMG_BUF_SIZE_VGA (IMG_SIZE_VGA+0x200) 51 | #define IMG_WIDTH_WVGA 752 52 | #define IMG_HEIGHT_WVGA 480 53 | #define IMG_SIZE_WVGA (IMG_WIDTH_WVGA*IMG_HEIGHT_WVGA) 54 | #define IMG_BUF_SIZE_WVGA (IMG_SIZE_WVGA+0x200) 55 | #define FRAME_CLUST 216 56 | 57 | /*CAMERA CONTROL INSTRUCTION MACRO*/ 58 | #define CAPTURE_27 0xA1 59 | #define CAPTURE_54 0xA2 60 | #define CAPTURE_108 0xA6 61 | #define STANDBY_SHORT 0xA3 62 | #define STANDBY_LONG 0xA4 63 | #define GET_CAM_LR 0xA5 64 | #define CAM_I2C_R 0xA7 65 | #define CAM_I2C_W 0xA8 66 | #define auto_expo 1 67 | #define EXP_VAL 50 68 | #define GAI_VAL 100 69 | 70 | #define CAM_I2C_ADDR 0x5C //0x5C 71 | 72 | /* Maximum length of a string read from the Configuration file (/etc/cyusb.conf) for the library. */ 73 | #define MAX_CFG_LINE_LENGTH (120) 74 | 75 | #define IMU_FRAME_LEN 32 76 | pthread_t imu_thread; 77 | int imu_fd=0; 78 | bool imu_close=false; 79 | 80 | using namespace std; 81 | 82 | 83 | visensor_imudata IMU_FIFO[200]; 84 | int imu_fifo_ct=0; 85 | 86 | bool shut_down_tag=false; 87 | 88 | bool left_fresh=false; 89 | bool right_fresh=false; 90 | 91 | bool allow_settings_change=true; 92 | 93 | /****************************************************/ 94 | int data_mode; 95 | int modes_settings[13][4]; 96 | int EG_mode; 97 | int man_exp; 98 | int man_gain; 99 | int auto_EG_top; 100 | int auto_EG_bottom; 101 | int auto_EG_des; 102 | int auto_E_man_G_Etop; 103 | int auto_E_man_G_Ebottom; 104 | int auto_E_man_G; 105 | int agc_aec_skip_frame; 106 | int VI_FIFO_matcher; 107 | 108 | double imu_acc_bias_X; 109 | double imu_acc_bias_Y; 110 | double imu_acc_bias_Z; 111 | 112 | string imu_port_name; 113 | visensor_imudata visensor_imudata_pack; 114 | /****************************************************/ 115 | 116 | cyusb_handle *pcam1_handle; 117 | cyusb_handle *pcam2_handle; 118 | int gFPS = 27; 119 | int gSelectCam = 0; 120 | int HORIZ_BLK=150; 121 | 122 | pthread_t cam1_capture_thread; 123 | pthread_t cam2_capture_thread; 124 | 125 | pthread_t cam1_capture_detect_thread; 126 | pthread_t cam2_capture_detect_thread; 127 | 128 | int gFrameCam1=0; 129 | int gFrameCam2=0; 130 | int gImg1Pass[FRAME_CLUST]; 131 | int gImg2Pass[FRAME_CLUST]; 132 | float gImg1Time[FRAME_CLUST]; // time-stamp 133 | float gImg2Time[FRAME_CLUST]; 134 | timeval gImg1_SysTime[FRAME_CLUST]; 135 | timeval gImg2_SysTime[FRAME_CLUST]; 136 | 137 | unsigned char gImg1_VGA[FRAME_CLUST][IMG_BUF_SIZE_VGA]; 138 | unsigned char gImg2_VGA[FRAME_CLUST][IMG_BUF_SIZE_VGA]; 139 | 140 | unsigned char gImg1_WVGA[FRAME_CLUST][IMG_BUF_SIZE_WVGA]; 141 | unsigned char gImg2_WVGA[FRAME_CLUST][IMG_BUF_SIZE_WVGA]; 142 | 143 | bool last_is_good=false; 144 | bool save_img=false; 145 | bool save_img1=false; 146 | 147 | bool visensor_resolution_status=false; 148 | int current_HB=194; 149 | int visensor_cam_selection=0; 150 | int current_FPS=27; 151 | 152 | string visensor_settings_file_name; 153 | 154 | 155 | struct cydev 156 | { 157 | cyusb_device *dev; 158 | cyusb_handle *handle; 159 | unsigned short vid; 160 | unsigned short pid; /* Product ID */ 161 | unsigned char is_open; /* When device is opened, val = 1 */ 162 | unsigned char busnum; /* The bus number of this device */ 163 | unsigned char devaddr; /* The device address*/ 164 | unsigned char filler; /* Padding to make struct = 16 bytes */ 165 | }; 166 | 167 | static struct cydev cydev[MAXDEVICES]; 168 | static int number_of_cameras; /* Number of Interesting Devices */ 169 | 170 | struct VPD 171 | { 172 | unsigned short vid; 173 | unsigned short pid; 174 | char desc[MAX_STR_LEN]; 175 | }; 176 | 177 | static struct VPD vpd[MAX_ID_PAIRS]; 178 | 179 | static libusb_device **libusb_device_list; 180 | static unsigned int checksum = 0; 181 | 182 | char pidfile[256]; 183 | char logfile[256]; 184 | int logfd; 185 | int pidfd; 186 | 187 | float visensor_get_hardware_fps(); 188 | 189 | 190 | void visensor_load_settings(const char* settings_file) 191 | { 192 | /******************************* Load Settings File ***************************************/ 193 | 194 | LOG("\r\n**********************************************\r\n"); 195 | LOG("Loading Settings File...\r\n"); 196 | 197 | visensor_settings_file_name=settings_file; 198 | 199 | ifstream ifs_settings(visensor_settings_file_name.c_str());//,ios::in|ios::binary|ios::ate); 200 | if(!ifs_settings ) 201 | { 202 | cerr << "Error opening file: "< settingsList; 218 | int linecount=0; 219 | while(std::getline(ifs_settings,line_settings)) 220 | { 221 | linecount++; 222 | if(line_settings.at(0)!='#') 223 | settingsList.push_back(line_settings); 224 | } 225 | //cout <<"line get end............"<::iterator iter = settingsList.begin(); iter != settingsList.end(); ++iter) 227 | { 228 | cout << *iter << endl; 229 | } 230 | ifs_settings.close(); 231 | 232 | 233 | std::string token; 234 | 235 | // convert to File_Settings Object 236 | istringstream(settingsList.at(1))>>data_mode; 237 | //cout << "data_mode: "<>_HB; 257 | 258 | //WVGA or VGA? 259 | std::getline(iss_line, token, ','); 260 | res=(token[0]=='V'?0:1); 261 | 262 | //FPS 263 | std::getline(iss_line, token, ','); 264 | istringstream(token)>>fps; 265 | 266 | // fill data 267 | if(i<9)// “HighSpeed Pre-Set” 268 | { 269 | modes_settings[i-3][0]=visensor_cam_selection; 270 | modes_settings[i-3][1]=_HB; 271 | modes_settings[i-3][2]=res; 272 | modes_settings[i-3][3]=fps; 273 | } 274 | else// “Normal Pre-Set” 275 | { 276 | modes_settings[i-4][0]=visensor_cam_selection; 277 | modes_settings[i-4][1]=_HB; 278 | modes_settings[i-4][2]=res; 279 | modes_settings[i-4][3]=fps; 280 | } 281 | 282 | } 283 | 284 | // Manual Mode 285 | { 286 | int visensor_cam_selection=0,_HB=0,res=0,fps=0; 287 | 288 | istringstream(settingsList.at(17))>>visensor_cam_selection; 289 | istringstream(settingsList.at(18))>>_HB; 290 | token = settingsList.at(19); 291 | res=(token[0]=='V'?0:1); 292 | istringstream(settingsList.at(20))>>fps; 293 | 294 | modes_settings[12][0]=visensor_cam_selection; 295 | modes_settings[12][1]=_HB; 296 | modes_settings[12][2]=res; 297 | modes_settings[12][3]=fps; 298 | } 299 | 300 | // EXP&GAIN 301 | istringstream(settingsList.at(22))>>EG_mode; 302 | 303 | // manual E&G 304 | { 305 | int _exp=0,_gain=0; 306 | 307 | istringstream iss_line(settingsList.at(23)); 308 | //The first word, ignore 309 | std::getline(iss_line, token, ','); 310 | 311 | std::getline(iss_line, token, ','); 312 | istringstream(token)>>_exp; 313 | std::getline(iss_line, token, ','); 314 | istringstream(token)>>_gain; 315 | 316 | // fill data 317 | man_exp=_exp; 318 | man_gain=_gain; 319 | } 320 | 321 | // auto 322 | { 323 | int _top=0,_bottom=0,_des=0; 324 | 325 | istringstream iss_line(settingsList.at(24)); 326 | //The first word, ignore 327 | std::getline(iss_line, token, ','); 328 | 329 | std::getline(iss_line, token, ','); 330 | istringstream(token)>>_top; 331 | 332 | std::getline(iss_line, token, ','); 333 | istringstream(token)>>_bottom; 334 | 335 | std::getline(iss_line, token, ','); 336 | istringstream(token)>>_des; 337 | // fill data 338 | auto_EG_top=_top; 339 | auto_EG_bottom=_bottom; 340 | auto_EG_des=_des; 341 | } 342 | 343 | // AE_MG 344 | { 345 | int _top=0,_bottom=0,_des=0,_gain=0; 346 | 347 | istringstream iss_line(settingsList.at(25)); 348 | 349 | //The first word, ignore 350 | std::getline(iss_line, token, ','); 351 | 352 | std::getline(iss_line, token, ','); 353 | istringstream(token)>>_top; 354 | std::getline(iss_line, token, ','); 355 | istringstream(token)>>_bottom; 356 | std::getline(iss_line, token, ','); 357 | istringstream(token)>>_des; 358 | std::getline(iss_line, token, ','); 359 | istringstream(token)>>_gain; 360 | // fill data 361 | auto_E_man_G_Etop=_top; 362 | auto_E_man_G_Ebottom=_bottom; 363 | auto_E_man_G=_gain; 364 | } 365 | // imu_port_name 366 | { 367 | istringstream iss_line(settingsList.at(26)); 368 | std::getline(iss_line, token, ','); 369 | 370 | imu_port_name = token.c_str(); 371 | 372 | std::getline(iss_line, token, ','); 373 | istringstream(token)>>VI_FIFO_matcher; 374 | } 375 | // imu acc bias 376 | { 377 | istringstream iss_line(settingsList.at(28)); 378 | //The first word, ignore 379 | std::getline(iss_line, token, ','); 380 | std::getline(iss_line, token, ','); 381 | istringstream(token)>>imu_acc_bias_X; 382 | } 383 | { 384 | istringstream iss_line(settingsList.at(29)); 385 | //The first word, ignore 386 | std::getline(iss_line, token, ','); 387 | std::getline(iss_line, token, ','); 388 | istringstream(token)>>imu_acc_bias_Y; 389 | } 390 | { 391 | istringstream iss_line(settingsList.at(30)); 392 | //The first word, ignore 393 | std::getline(iss_line, token, ','); 394 | std::getline(iss_line, token, ','); 395 | istringstream(token)>>imu_acc_bias_Z; 396 | } 397 | 398 | // show settings 399 | cout<13||_mode<=0) 565 | { 566 | cout<<"settings FIXED !"<=MAXDEVICES) 730 | break; 731 | } 732 | return number_of_cameras; 733 | } 734 | 735 | cyusb_handle * cyusb_gethandle(int index) 736 | { 737 | return cydev[index].handle; 738 | } 739 | 740 | int cyusb_control_transfer(cyusb_handle *h, unsigned char bmRequestType, unsigned char bRequest, 741 | unsigned short wValue, unsigned short wIndex, unsigned char *data, unsigned short wLength, 742 | unsigned int timeout) 743 | { 744 | return ( libusb_control_transfer(h, bmRequestType, bRequest, wValue, wIndex, data, wLength, timeout) ); 745 | } 746 | 747 | int cyusb_bulk_transfer(cyusb_handle *h, unsigned char endpoint, unsigned char *data, int length, 748 | int *transferred, int timeout) 749 | { 750 | return ( libusb_bulk_transfer(h, endpoint, data, length, transferred, timeout) ); 751 | } 752 | 753 | void cyusb_close(void) 754 | { 755 | for (int i = 0; i < number_of_cameras; ++i ) 756 | { 757 | libusb_close(cydev[i].handle); 758 | } 759 | libusb_free_device_list(libusb_device_list, 1); 760 | libusb_exit(NULL); 761 | } 762 | 763 | /*****************************************************************************/ 764 | 765 | int fps_control() 766 | { 767 | switch(gFPS) 768 | { 769 | case 27: 770 | return CAPTURE_27; 771 | break; 772 | case 54: 773 | return CAPTURE_54; 774 | break; 775 | case 108: 776 | return CAPTURE_108; 777 | break; 778 | default: 779 | return CAPTURE_27; 780 | break; 781 | } 782 | return CAPTURE_27; 783 | } 784 | 785 | cyusb_handle* get_cam_no_handle(int cam_no) 786 | { 787 | if(cam_no==1) 788 | return pcam1_handle; 789 | else if(cam_no==2) 790 | return pcam2_handle; 791 | else 792 | { 793 | printf("Wrong cam number!\r\n"); 794 | return NULL; 795 | } 796 | } 797 | 798 | int control_camera(int cam_no, unsigned char instruction) 799 | { 800 | unsigned char buf[1]; 801 | int r = cyusb_control_transfer(get_cam_no_handle(cam_no),0xC0,instruction,0,0,buf,0,1000); 802 | if(r != 0) 803 | { 804 | //printf("cam%d,i:0x%02X,r:%d\r\n",cam_no,instruction,r); 805 | } 806 | return r; 807 | } 808 | 809 | int camera_i2c_read(int cam_no, unsigned char reg,int* value) 810 | { 811 | unsigned char buf[3]; 812 | int r = cyusb_control_transfer(get_cam_no_handle(cam_no),0xC0,CAM_I2C_R,(CAM_I2C_ADDR<<8)+reg,0,buf,3,1000); 813 | if(r != 3) 814 | { 815 | //printf("cam%d,i:0x%02X,r:%d\r\n",cam_no,CAM_I2C_R,r); 816 | return r; 817 | } 818 | if(buf[0]!=0) 819 | { 820 | //printf("I2C Read failed...Cam:%d, Addr:0x%02X, Reg:0x%02X, I2C_return:%d\r\n",cam_no,CAM_I2C_ADDR,reg,buf[0]); 821 | return r; 822 | } 823 | *value = buf[1]+buf[2]<<8; 824 | //printf("I2C Read Success...Cam:%d, Addr:0x%02X, Reg:0x%02X, Result:0x%04X\r\n",cam_no,CAM_I2C_ADDR,reg,*value); 825 | return r; 826 | } 827 | 828 | int camera_i2c_write(int cam_no, unsigned char reg,int value) 829 | { 830 | unsigned char buf[1]; 831 | int r = cyusb_control_transfer(get_cam_no_handle(cam_no),0xC0,CAM_I2C_W,(CAM_I2C_ADDR<<8)+reg,value,buf,1,1000); 832 | if(r != 1) 833 | { 834 | //printf("cam%d,i:0x%02X,r:%d\r\n",cam_no,CAM_I2C_W,r); 835 | return r; 836 | } 837 | if(buf[0]!=0) 838 | { 839 | //printf("I2C Write failed...Cam:%d, Addr:0x%02X, Reg:0x%02X, Write:0x%04X, I2C_return:%d\r\n",cam_no,CAM_I2C_ADDR,reg,value,buf[0]); 840 | return r; 841 | } 842 | //printf("I2C Write Success...Cam:%d, Addr:0x%02X, Reg:0x%02X, Write:0x%04X\r\n",cam_no,CAM_I2C_ADDR,reg,value); 843 | return r; 844 | } 845 | 846 | int check_img(int cam_no,unsigned char *pImg,int *pImgPass) 847 | { 848 | if(!visensor_resolution_status)*pImgPass = (pImg[IMG_SIZE_VGA+0]==0xFF&&pImg[IMG_SIZE_VGA+1]==0x00&&pImg[IMG_SIZE_VGA+2]==0xFE&&pImg[IMG_SIZE_VGA+3]==0x01); 849 | else *pImgPass = (pImg[IMG_SIZE_WVGA+0]==0xFF&&pImg[IMG_SIZE_WVGA+1]==0x00&&pImg[IMG_SIZE_WVGA+2]==0xFE&&pImg[IMG_SIZE_WVGA+3]==0x01); 850 | if(*pImgPass == 0) 851 | { 852 | control_camera(cam_no,fps_control()); 853 | //printf("Img%d check error!\r\n",cam_no); 854 | } 855 | } 856 | 857 | float visensor_get_hardware_fps() 858 | { 859 | if(gFPS==54) 860 | { 861 | if(!visensor_resolution_status)return 45700.0f/(current_HB+640.0f); 862 | else return 45700.0f/(current_HB+752.0f); 863 | } 864 | else if(gFPS==27) 865 | { 866 | if(!visensor_resolution_status)return 0.5f*45700.0f/(current_HB+640.0f); 867 | else return 0.5f*45700.0f/(current_HB+752.0f); 868 | } 869 | } 870 | static void set_camreg_default(int cam_no) 871 | { 872 | camera_i2c_write(cam_no,0x00,0x1324); 873 | camera_i2c_write(cam_no,0x01,0x0001); 874 | camera_i2c_write(cam_no,0x02,0x0004); 875 | camera_i2c_write(cam_no,0x03,0x01E0); 876 | 877 | camera_i2c_write(cam_no,0x04,0x02F0); 878 | camera_i2c_write(cam_no,0x05,0x005E); 879 | camera_i2c_write(cam_no,0x06,0x002D); 880 | camera_i2c_write(cam_no,0x07,0x0388); 881 | 882 | camera_i2c_write(cam_no,0x0B,0x01E0); 883 | camera_i2c_write(cam_no,0x35,0x0010); 884 | camera_i2c_write(cam_no,0xA5,0x003A); 885 | camera_i2c_write(cam_no,0xA6,0x0002); 886 | 887 | camera_i2c_write(cam_no,0xA8,0x0000); 888 | camera_i2c_write(cam_no,0xA9,0x0002); 889 | camera_i2c_write(cam_no,0xAA,0x0002); 890 | camera_i2c_write(cam_no,0xAB,0x0040); 891 | 892 | camera_i2c_write(cam_no,0xAC,0x0001); 893 | camera_i2c_write(cam_no,0xAD,0x01E0); 894 | camera_i2c_write(cam_no,0xAE,0x0014); 895 | camera_i2c_write(cam_no,0xAF,0x0003); 896 | } 897 | 898 | void *cam1_capture(void*) 899 | { 900 | set_camreg_default(1); 901 | int r,transferred = 0; 902 | camera_i2c_write(1,0x07,0x0188);//Normal 903 | camera_i2c_write(1,0x06,0x002D);//VB 904 | // cmos设置 905 | camera_i2c_write(1,0x05,current_HB);//HB 906 | if(!visensor_resolution_status)camera_i2c_write(1,0x04,IMG_WIDTH_VGA);//HW 907 | else camera_i2c_write(1,0x04,IMG_WIDTH_WVGA);//HW 908 | switch (EG_mode) { 909 | case 0: 910 | camera_i2c_write(1,0xAF,0x00);//AEC 911 | camera_i2c_write(1,0x0B,man_exp);//Exposure Time 912 | camera_i2c_write(1,0x35,man_gain);//Gain 913 | break; 914 | case 1: 915 | camera_i2c_write(1,0xAF,0x03);//AEC 916 | camera_i2c_write(1,0xA5,auto_EG_des);//AEC 917 | camera_i2c_write(1,0xA6,0x01);//AEC 918 | camera_i2c_write(1,0xA8,0x00);//AEC 919 | camera_i2c_write(1,0xAC,auto_EG_bottom);//AEC 920 | camera_i2c_write(1,0xAD,auto_EG_top);//AEC 921 | camera_i2c_write(1,0xAE,2);//AEC 922 | break; 923 | case 2: 924 | camera_i2c_write(1,0xAF,0x01);//AEC 925 | camera_i2c_write(1,0xA5,auto_EG_des);//AEC 926 | camera_i2c_write(1,0xA6,0x01);//AEC 927 | camera_i2c_write(1,0xA8,0x00);//AEC 928 | camera_i2c_write(1,0xAC,auto_E_man_G_Ebottom);//AEC 929 | camera_i2c_write(1,0xAD,auto_E_man_G_Etop);//AEC 930 | camera_i2c_write(1,0xAE,2);//AEC 931 | camera_i2c_write(1,0x35,auto_E_man_G);//Gain 932 | break; 933 | case 3: 934 | break; 935 | case 4: 936 | camera_i2c_write(1,0xA6,agc_aec_skip_frame); 937 | camera_i2c_write(1,0xA8,2); 938 | camera_i2c_write(1,0xA9,agc_aec_skip_frame); 939 | camera_i2c_write(1,0xAA,2); 940 | break; 941 | default: 942 | break; 943 | } 944 | 945 | control_camera(1,fps_control()); 946 | // usleep(100); 947 | 948 | static int sync_ct=0; 949 | 950 | struct timeval endTime; 951 | float fTimeuse=0; 952 | 953 | int ctt=0; 954 | 955 | float time_offset=1.0f/visensor_get_hardware_fps(); 956 | control_camera(1,STANDBY_SHORT); 957 | while(!shut_down_tag) 958 | { 959 | ctt++; 960 | if(ctt>1) //该数值越大,则帧同步的周期越长,同步频率越小 961 | { 962 | control_camera(1,STANDBY_SHORT); 963 | ctt = 0; 964 | } 965 | for(gFrameCam1=0; !shut_down_tag&&gFrameCam1=abs(imu_usec-left_stamp.tv_usec)) 1189 | { 1190 | min_dist=abs(imu_usec-left_stamp.tv_usec); 1191 | min_id=i; 1192 | } 1193 | } 1194 | visensor_imudata bind_imuData=IMU_FIFO[min_id]; 1195 | return bind_imuData; 1196 | 1197 | } 1198 | else if(gImg2Pass[img2_pointer]&&gImg1Pass[img1_pointer]&&(visensor_resolution_status)) 1199 | { 1200 | memcpy(left_img,(char *)(gImg1_WVGA[img1_pointer]),IMG_BUF_SIZE_WVGA); 1201 | memcpy(right_img,(char *)(gImg2_WVGA[img2_pointer]),IMG_BUF_SIZE_WVGA); 1202 | gImg2Pass[img2_pointer]=0; 1203 | gImg1Pass[img1_pointer]=0; 1204 | 1205 | // 绑定最近时间戳的IMU数据 1206 | timeval left_stamp=gImg1_SysTime[img1_pointer]; 1207 | int min_id=0; 1208 | int min_dist=10000; 1209 | for(int i=0; i<200; i++) 1210 | { 1211 | int imu_usec=IMU_FIFO[i].system_time.tv_usec; 1212 | if(IMU_FIFO[i].imu_time!=0&&min_dist>=abs(imu_usec-left_stamp.tv_usec)) 1213 | { 1214 | min_dist=abs(imu_usec-left_stamp.tv_usec); 1215 | min_id=i; 1216 | } 1217 | } 1218 | visensor_imudata bind_imuData=IMU_FIFO[min_id]; 1219 | return bind_imuData; 1220 | } 1221 | 1222 | 1223 | } 1224 | visensor_imudata visensor_get_stereoImg(char* left_img,char* right_img,timeval &left_stamp,timeval &right_stamp) 1225 | { 1226 | 1227 | while(!visensor_is_stereo_good())usleep(200); 1228 | 1229 | int img1_pointer = (gFrameCam1 + FRAME_CLUST - 1) % FRAME_CLUST; 1230 | int img2_pointer = (gFrameCam2 + FRAME_CLUST - 1) % FRAME_CLUST; 1231 | if(gImg2Pass[img2_pointer]&&gImg1Pass[img1_pointer]&&(!visensor_resolution_status)) 1232 | { 1233 | memcpy(left_img,(char *)(gImg1_VGA[img1_pointer]),IMG_BUF_SIZE_VGA); 1234 | memcpy(right_img,(char *)(gImg2_VGA[img2_pointer]),IMG_BUF_SIZE_VGA); 1235 | left_stamp=gImg1_SysTime[img1_pointer]; 1236 | right_stamp=gImg2_SysTime[img2_pointer]; 1237 | gImg2Pass[img2_pointer]=0; 1238 | gImg1Pass[img1_pointer]=0; 1239 | 1240 | // 绑定最近时间戳的IMU数据 1241 | int min_id=0; 1242 | int min_dist=10000; 1243 | for(int i=0; i<200; i++) 1244 | { 1245 | int imu_usec=IMU_FIFO[i].system_time.tv_usec; 1246 | if(IMU_FIFO[i].imu_time!=0&&min_dist>=abs(imu_usec-left_stamp.tv_usec)) 1247 | { 1248 | min_dist=abs(imu_usec-left_stamp.tv_usec); 1249 | min_id=i; 1250 | } 1251 | } 1252 | visensor_imudata bind_imuData=IMU_FIFO[min_id]; 1253 | return bind_imuData; 1254 | } 1255 | else if(gImg2Pass[img2_pointer]&&gImg1Pass[img1_pointer]&&(visensor_resolution_status)) 1256 | { 1257 | memcpy(left_img,(char *)(gImg1_WVGA[img1_pointer]),IMG_BUF_SIZE_WVGA); 1258 | memcpy(right_img,(char *)(gImg2_WVGA[img2_pointer]),IMG_BUF_SIZE_WVGA); 1259 | left_stamp=gImg1_SysTime[img1_pointer]; 1260 | right_stamp=gImg2_SysTime[img2_pointer]; 1261 | gImg2Pass[img2_pointer]=0; 1262 | gImg1Pass[img1_pointer]=0; 1263 | 1264 | // 绑定最近时间戳的IMU数据 1265 | int min_id=0; 1266 | int min_dist=10000; 1267 | for(int i=0; i<200; i++) 1268 | { 1269 | int imu_usec=IMU_FIFO[i].system_time.tv_usec; 1270 | if(IMU_FIFO[i].imu_time!=0&&min_dist>=abs(imu_usec-left_stamp.tv_usec)) 1271 | { 1272 | min_dist=abs(imu_usec-left_stamp.tv_usec); 1273 | min_id=i; 1274 | } 1275 | } 1276 | visensor_imudata bind_imuData=IMU_FIFO[min_id]; 1277 | return bind_imuData; 1278 | } 1279 | } 1280 | 1281 | visensor_imudata visensor_get_leftImg(char* left_img) 1282 | { 1283 | 1284 | while(!visensor_is_left_good())usleep(200); 1285 | 1286 | int img1_pointer = (gFrameCam1 + FRAME_CLUST - 1) % FRAME_CLUST; 1287 | if(gImg1Pass[img1_pointer]&&(!visensor_resolution_status)) 1288 | { 1289 | memcpy(left_img,(char *)(gImg1_VGA[img1_pointer]),IMG_BUF_SIZE_VGA); 1290 | gImg1Pass[img1_pointer]=0; 1291 | 1292 | // 绑定最近时间戳的IMU数据 1293 | timeval left_stamp=gImg1_SysTime[img1_pointer]; 1294 | int min_id=0; 1295 | int min_dist=10000; 1296 | for(int i=0; i<200; i++) 1297 | { 1298 | int imu_usec=IMU_FIFO[i].system_time.tv_usec; 1299 | if(IMU_FIFO[i].imu_time!=0&&min_dist>=abs(imu_usec-left_stamp.tv_usec)) 1300 | { 1301 | min_dist=abs(imu_usec-left_stamp.tv_usec); 1302 | min_id=i; 1303 | } 1304 | } 1305 | visensor_imudata bind_imuData=IMU_FIFO[min_id]; 1306 | return bind_imuData; 1307 | } 1308 | else if(gImg1Pass[img1_pointer]&&(visensor_resolution_status)) 1309 | { 1310 | memcpy(left_img,(char *)(gImg1_WVGA[img1_pointer]),IMG_BUF_SIZE_WVGA); 1311 | gImg1Pass[img1_pointer]=0; 1312 | 1313 | // 绑定最近时间戳的IMU数据 1314 | timeval left_stamp=gImg1_SysTime[img1_pointer]; 1315 | int min_id=0; 1316 | int min_dist=10000; 1317 | for(int i=0; i<200; i++) 1318 | { 1319 | int imu_usec=IMU_FIFO[i].system_time.tv_usec; 1320 | if(IMU_FIFO[i].imu_time!=0&&min_dist>=abs(imu_usec-left_stamp.tv_usec)) 1321 | { 1322 | min_dist=abs(imu_usec-left_stamp.tv_usec); 1323 | min_id=i; 1324 | } 1325 | } 1326 | visensor_imudata bind_imuData=IMU_FIFO[min_id]; 1327 | return bind_imuData; 1328 | } 1329 | } 1330 | 1331 | visensor_imudata visensor_get_leftImg(char* left_img,timeval &left_stamp) 1332 | { 1333 | 1334 | while(!visensor_is_left_good())usleep(200); 1335 | 1336 | int img1_pointer = (gFrameCam1 + FRAME_CLUST - 1) % FRAME_CLUST; 1337 | if(gImg1Pass[img1_pointer]&&(!visensor_resolution_status)) 1338 | { 1339 | memcpy(left_img,(char *)(gImg1_VGA[img1_pointer]),IMG_BUF_SIZE_VGA); 1340 | left_stamp=gImg1_SysTime[img1_pointer]; 1341 | gImg1Pass[img1_pointer]=0; 1342 | 1343 | // 绑定最近时间戳的IMU数据 1344 | int min_id=0; 1345 | int min_dist=10000; 1346 | for(int i=0; i<200; i++) 1347 | { 1348 | int imu_usec=IMU_FIFO[i].system_time.tv_usec; 1349 | if(IMU_FIFO[i].imu_time!=0&&min_dist>=abs(imu_usec-left_stamp.tv_usec)) 1350 | { 1351 | min_dist=abs(imu_usec-left_stamp.tv_usec); 1352 | min_id=i; 1353 | } 1354 | } 1355 | visensor_imudata bind_imuData=IMU_FIFO[min_id]; 1356 | return bind_imuData; 1357 | } 1358 | else if(gImg1Pass[img1_pointer]&&(visensor_resolution_status)) 1359 | { 1360 | memcpy(left_img,(char *)(gImg1_WVGA[img1_pointer]),IMG_BUF_SIZE_WVGA); 1361 | left_stamp=gImg1_SysTime[img1_pointer]; 1362 | gImg1Pass[img1_pointer]=0; 1363 | 1364 | // 绑定最近时间戳的IMU数据 1365 | int min_id=0; 1366 | int min_dist=10000; 1367 | for(int i=0; i<200; i++) 1368 | { 1369 | int imu_usec=IMU_FIFO[i].system_time.tv_usec; 1370 | if(IMU_FIFO[i].imu_time!=0&&min_dist>=abs(imu_usec-left_stamp.tv_usec)) 1371 | { 1372 | min_dist=abs(imu_usec-left_stamp.tv_usec); 1373 | min_id=i; 1374 | } 1375 | } 1376 | visensor_imudata bind_imuData=IMU_FIFO[min_id]; 1377 | return bind_imuData; 1378 | } 1379 | } 1380 | 1381 | visensor_imudata visensor_get_rightImg(char* right_img) 1382 | { 1383 | 1384 | while(!visensor_is_right_good())usleep(200); 1385 | 1386 | int img2_pointer = (gFrameCam2 + FRAME_CLUST - 1) % FRAME_CLUST; 1387 | 1388 | if(gImg2Pass[img2_pointer]&&(!visensor_resolution_status)) 1389 | { 1390 | memcpy(right_img,(char *)(gImg2_VGA[img2_pointer]),IMG_BUF_SIZE_VGA); 1391 | gImg2Pass[img2_pointer]=0; 1392 | 1393 | // 绑定最近时间戳的IMU数据 1394 | timeval right_stamp=gImg2_SysTime[img2_pointer]; 1395 | int min_id=0; 1396 | int min_dist=10000; 1397 | for(int i=0; i<200; i++) 1398 | { 1399 | int imu_usec=IMU_FIFO[i].system_time.tv_usec; 1400 | if(IMU_FIFO[i].imu_time!=0&&min_dist>=abs(imu_usec-right_stamp.tv_usec)) 1401 | { 1402 | min_dist=abs(imu_usec-right_stamp.tv_usec); 1403 | min_id=i; 1404 | } 1405 | } 1406 | visensor_imudata bind_imuData=IMU_FIFO[min_id]; 1407 | return bind_imuData; 1408 | } 1409 | else if(gImg2Pass[img2_pointer]&&(visensor_resolution_status)) 1410 | { 1411 | memcpy(right_img,(char *)(gImg2_WVGA[img2_pointer]),IMG_BUF_SIZE_WVGA); 1412 | gImg2Pass[img2_pointer]=0; 1413 | 1414 | // 绑定最近时间戳的IMU数据 1415 | timeval right_stamp=gImg2_SysTime[img2_pointer]; 1416 | int min_id=0; 1417 | int min_dist=10000; 1418 | for(int i=0; i<200; i++) 1419 | { 1420 | int imu_usec=IMU_FIFO[i].system_time.tv_usec; 1421 | if(IMU_FIFO[i].imu_time!=0&&min_dist>=abs(imu_usec-right_stamp.tv_usec)) 1422 | { 1423 | min_dist=abs(imu_usec-right_stamp.tv_usec); 1424 | min_id=i; 1425 | } 1426 | } 1427 | visensor_imudata bind_imuData=IMU_FIFO[min_id]; 1428 | return bind_imuData; 1429 | } 1430 | } 1431 | visensor_imudata visensor_get_rightImg(char* right_img,timeval &right_stamp) 1432 | { 1433 | 1434 | while(!visensor_is_right_good())usleep(200); 1435 | 1436 | int img2_pointer = (gFrameCam2 + FRAME_CLUST - 1) % FRAME_CLUST; 1437 | 1438 | if(gImg2Pass[img2_pointer]&&(!visensor_resolution_status)) 1439 | { 1440 | memcpy(right_img,(char *)(gImg2_VGA[img2_pointer]),IMG_BUF_SIZE_VGA); 1441 | right_stamp=gImg2_SysTime[img2_pointer]; 1442 | gImg2Pass[img2_pointer]=0; 1443 | 1444 | // 绑定最近时间戳的IMU数据 1445 | int min_id=0; 1446 | int min_dist=10000; 1447 | for(int i=0; i<200; i++) 1448 | { 1449 | int imu_usec=IMU_FIFO[i].system_time.tv_usec; 1450 | if(IMU_FIFO[i].imu_time!=0&&min_dist>=abs(imu_usec-right_stamp.tv_usec)) 1451 | { 1452 | min_dist=abs(imu_usec-right_stamp.tv_usec); 1453 | min_id=i; 1454 | } 1455 | } 1456 | visensor_imudata bind_imuData=IMU_FIFO[min_id]; 1457 | return bind_imuData; 1458 | } 1459 | else if(gImg2Pass[img2_pointer]&&(visensor_resolution_status)) 1460 | { 1461 | memcpy(right_img,(char *)(gImg2_WVGA[img2_pointer]),IMG_BUF_SIZE_WVGA); 1462 | right_stamp=gImg2_SysTime[img2_pointer]; 1463 | gImg2Pass[img2_pointer]=0; 1464 | 1465 | // 绑定最近时间戳的IMU数据 1466 | int min_id=0; 1467 | int min_dist=10000; 1468 | for(int i=0; i<200; i++) 1469 | { 1470 | int imu_usec=IMU_FIFO[i].system_time.tv_usec; 1471 | if(IMU_FIFO[i].imu_time!=0&&min_dist>=abs(imu_usec-right_stamp.tv_usec)) 1472 | { 1473 | min_dist=abs(imu_usec-right_stamp.tv_usec); 1474 | min_id=i; 1475 | } 1476 | } 1477 | visensor_imudata bind_imuData=IMU_FIFO[min_id]; 1478 | return bind_imuData; 1479 | } 1480 | } 1481 | 1482 | int visensor_Start_Cameras() 1483 | { 1484 | gSelectCam = 3; 1485 | allow_settings_change=false; 1486 | 1487 | shut_down_tag=false; 1488 | 1489 | int ret_val=0; 1490 | 1491 | DEBUGLOG("Now opening cameras...\r\n"); 1492 | //Open usb devices 1493 | int r; 1494 | r = cyusb_open(); 1495 | if(r<1) 1496 | { 1497 | LOG("Error: No cameras found! r = %d\r\n",r); 1498 | return -2; 1499 | } 1500 | else 1501 | { 1502 | printf("Number of device of interest found: %d\r\n",r); 1503 | } 1504 | //Get camera position 1505 | cyusb_handle *pusb_handle; 1506 | for(int i=0; i