├── .gitignore ├── README.md ├── apps └── ios │ └── src │ └── RobotCalibration │ ├── RobotCalibration.xcodeproj │ ├── project.pbxproj │ ├── project.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcuserdata │ │ │ └── danc.xcuserdatad │ │ │ └── UserInterfaceState.xcuserstate │ └── xcuserdata │ │ └── danc.xcuserdatad │ │ └── xcschemes │ │ ├── RobotCalibration.xcscheme │ │ └── xcschememanagement.plist │ ├── RobotCalibration │ ├── Default-568h@2x.png │ ├── Default.png │ ├── Default@2x.png │ ├── RCAppDelegate.h │ ├── RCAppDelegate.m │ ├── RCViewController.h │ ├── RCViewController.m │ ├── RobotCalibration-Info.plist │ ├── RobotCalibration-Prefix.pch │ ├── en.lproj │ │ ├── InfoPlist.strings │ │ ├── MainStoryboard_iPad.storyboard │ │ └── MainStoryboard_iPhone.storyboard │ └── main.m │ └── RobotCalibrationTests │ ├── RobotCalibrationTests-Info.plist │ ├── RobotCalibrationTests.h │ ├── RobotCalibrationTests.m │ └── en.lproj │ └── InfoPlist.strings ├── bots ├── bitbeambot │ ├── calibrate.js │ ├── client.js │ ├── kinematics.js │ ├── sample-calibration.json │ └── server.js └── tapster │ ├── calibrate.js │ ├── client.js │ ├── kinematics.js │ ├── sample-calibration.json │ └── server.js ├── lib └── sylvester.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .idea 3 | node_modules 4 | .DS_Store 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Appium Robots 2 | ====== 3 | 4 | Robots are an add-on for appium that will send all touch actions through a robot instead of to the automation framework. 5 | 6 | 1.) To launch a robot server choose a robot in the bots folder, for this example we will use [Bitbeambot](http://bitbeam.org/). 7 | 8 | `cd bots/bitbeambot` 9 | 10 | `node server.js` 11 | 12 | 2.) Calibrate the robot 13 | 14 | `appium --udid [udid here] --app RobotCalibration.app --pre-launch` 15 | 16 | `node calibrate.js -o /path/to/calibration.json` 17 | 18 | 3.) Launch the Robot Server With Calibration 19 | 20 | `node server.js -c /path/to/calibration.json` 21 | 22 | 4.) Launch Appium Server in Robot Mode 23 | 24 | `appium --udid [udid here] --robot-address 0.0.0.0 --robot-port:4242` 25 | -------------------------------------------------------------------------------- /apps/ios/src/RobotCalibration/RobotCalibration.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 3E166C6B1683954A008893AA /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3E166C6A1683954A008893AA /* UIKit.framework */; }; 11 | 3E166C6D1683954A008893AA /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3E166C6C1683954A008893AA /* Foundation.framework */; }; 12 | 3E166C6F1683954A008893AA /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3E166C6E1683954A008893AA /* CoreGraphics.framework */; }; 13 | 3E166C751683954A008893AA /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 3E166C731683954A008893AA /* InfoPlist.strings */; }; 14 | 3E166C771683954A008893AA /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 3E166C761683954A008893AA /* main.m */; }; 15 | 3E166C7B1683954A008893AA /* RCAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 3E166C7A1683954A008893AA /* RCAppDelegate.m */; }; 16 | 3E166C7D1683954A008893AA /* Default.png in Resources */ = {isa = PBXBuildFile; fileRef = 3E166C7C1683954A008893AA /* Default.png */; }; 17 | 3E166C7F1683954A008893AA /* Default@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 3E166C7E1683954A008893AA /* Default@2x.png */; }; 18 | 3E166C811683954A008893AA /* Default-568h@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 3E166C801683954A008893AA /* Default-568h@2x.png */; }; 19 | 3E166C841683954A008893AA /* MainStoryboard_iPhone.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 3E166C821683954A008893AA /* MainStoryboard_iPhone.storyboard */; }; 20 | 3E166C871683954A008893AA /* MainStoryboard_iPad.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 3E166C851683954A008893AA /* MainStoryboard_iPad.storyboard */; }; 21 | 3E166C8A1683954A008893AA /* RCViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 3E166C891683954A008893AA /* RCViewController.m */; }; 22 | 3E166C921683954A008893AA /* SenTestingKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3E166C911683954A008893AA /* SenTestingKit.framework */; }; 23 | 3E166C931683954A008893AA /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3E166C6A1683954A008893AA /* UIKit.framework */; }; 24 | 3E166C941683954A008893AA /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3E166C6C1683954A008893AA /* Foundation.framework */; }; 25 | 3E166C9C1683954A008893AA /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 3E166C9A1683954A008893AA /* InfoPlist.strings */; }; 26 | 3E166C9F1683954A008893AA /* RobotCalibrationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3E166C9E1683954A008893AA /* RobotCalibrationTests.m */; }; 27 | /* End PBXBuildFile section */ 28 | 29 | /* Begin PBXContainerItemProxy section */ 30 | 3E166C951683954A008893AA /* PBXContainerItemProxy */ = { 31 | isa = PBXContainerItemProxy; 32 | containerPortal = 3E166C5F1683954A008893AA /* Project object */; 33 | proxyType = 1; 34 | remoteGlobalIDString = 3E166C661683954A008893AA; 35 | remoteInfo = RobotCalibration; 36 | }; 37 | /* End PBXContainerItemProxy section */ 38 | 39 | /* Begin PBXFileReference section */ 40 | 3E166C671683954A008893AA /* RobotCalibration.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = RobotCalibration.app; sourceTree = BUILT_PRODUCTS_DIR; }; 41 | 3E166C6A1683954A008893AA /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; }; 42 | 3E166C6C1683954A008893AA /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; 43 | 3E166C6E1683954A008893AA /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; }; 44 | 3E166C721683954A008893AA /* RobotCalibration-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "RobotCalibration-Info.plist"; sourceTree = ""; }; 45 | 3E166C741683954A008893AA /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = ""; }; 46 | 3E166C761683954A008893AA /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 47 | 3E166C781683954A008893AA /* RobotCalibration-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "RobotCalibration-Prefix.pch"; sourceTree = ""; }; 48 | 3E166C791683954A008893AA /* RCAppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RCAppDelegate.h; sourceTree = ""; }; 49 | 3E166C7A1683954A008893AA /* RCAppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RCAppDelegate.m; sourceTree = ""; }; 50 | 3E166C7C1683954A008893AA /* Default.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Default.png; sourceTree = ""; }; 51 | 3E166C7E1683954A008893AA /* Default@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Default@2x.png"; sourceTree = ""; }; 52 | 3E166C801683954A008893AA /* Default-568h@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Default-568h@2x.png"; sourceTree = ""; }; 53 | 3E166C831683954A008893AA /* en */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = en; path = en.lproj/MainStoryboard_iPhone.storyboard; sourceTree = ""; }; 54 | 3E166C861683954A008893AA /* en */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = en; path = en.lproj/MainStoryboard_iPad.storyboard; sourceTree = ""; }; 55 | 3E166C881683954A008893AA /* RCViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RCViewController.h; sourceTree = ""; }; 56 | 3E166C891683954A008893AA /* RCViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RCViewController.m; sourceTree = ""; }; 57 | 3E166C901683954A008893AA /* RobotCalibrationTests.octest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = RobotCalibrationTests.octest; sourceTree = BUILT_PRODUCTS_DIR; }; 58 | 3E166C911683954A008893AA /* SenTestingKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SenTestingKit.framework; path = Library/Frameworks/SenTestingKit.framework; sourceTree = DEVELOPER_DIR; }; 59 | 3E166C991683954A008893AA /* RobotCalibrationTests-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "RobotCalibrationTests-Info.plist"; sourceTree = ""; }; 60 | 3E166C9B1683954A008893AA /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = ""; }; 61 | 3E166C9D1683954A008893AA /* RobotCalibrationTests.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RobotCalibrationTests.h; sourceTree = ""; }; 62 | 3E166C9E1683954A008893AA /* RobotCalibrationTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RobotCalibrationTests.m; sourceTree = ""; }; 63 | /* End PBXFileReference section */ 64 | 65 | /* Begin PBXFrameworksBuildPhase section */ 66 | 3E166C641683954A008893AA /* Frameworks */ = { 67 | isa = PBXFrameworksBuildPhase; 68 | buildActionMask = 2147483647; 69 | files = ( 70 | 3E166C6B1683954A008893AA /* UIKit.framework in Frameworks */, 71 | 3E166C6D1683954A008893AA /* Foundation.framework in Frameworks */, 72 | 3E166C6F1683954A008893AA /* CoreGraphics.framework in Frameworks */, 73 | ); 74 | runOnlyForDeploymentPostprocessing = 0; 75 | }; 76 | 3E166C8C1683954A008893AA /* Frameworks */ = { 77 | isa = PBXFrameworksBuildPhase; 78 | buildActionMask = 2147483647; 79 | files = ( 80 | 3E166C921683954A008893AA /* SenTestingKit.framework in Frameworks */, 81 | 3E166C931683954A008893AA /* UIKit.framework in Frameworks */, 82 | 3E166C941683954A008893AA /* Foundation.framework in Frameworks */, 83 | ); 84 | runOnlyForDeploymentPostprocessing = 0; 85 | }; 86 | /* End PBXFrameworksBuildPhase section */ 87 | 88 | /* Begin PBXGroup section */ 89 | 3E166C5E1683954A008893AA = { 90 | isa = PBXGroup; 91 | children = ( 92 | 3E166C701683954A008893AA /* RobotCalibration */, 93 | 3E166C971683954A008893AA /* RobotCalibrationTests */, 94 | 3E166C691683954A008893AA /* Frameworks */, 95 | 3E166C681683954A008893AA /* Products */, 96 | ); 97 | sourceTree = ""; 98 | }; 99 | 3E166C681683954A008893AA /* Products */ = { 100 | isa = PBXGroup; 101 | children = ( 102 | 3E166C671683954A008893AA /* RobotCalibration.app */, 103 | 3E166C901683954A008893AA /* RobotCalibrationTests.octest */, 104 | ); 105 | name = Products; 106 | sourceTree = ""; 107 | }; 108 | 3E166C691683954A008893AA /* Frameworks */ = { 109 | isa = PBXGroup; 110 | children = ( 111 | 3E166C6A1683954A008893AA /* UIKit.framework */, 112 | 3E166C6C1683954A008893AA /* Foundation.framework */, 113 | 3E166C6E1683954A008893AA /* CoreGraphics.framework */, 114 | 3E166C911683954A008893AA /* SenTestingKit.framework */, 115 | ); 116 | name = Frameworks; 117 | sourceTree = ""; 118 | }; 119 | 3E166C701683954A008893AA /* RobotCalibration */ = { 120 | isa = PBXGroup; 121 | children = ( 122 | 3E166C791683954A008893AA /* RCAppDelegate.h */, 123 | 3E166C7A1683954A008893AA /* RCAppDelegate.m */, 124 | 3E166C821683954A008893AA /* MainStoryboard_iPhone.storyboard */, 125 | 3E166C851683954A008893AA /* MainStoryboard_iPad.storyboard */, 126 | 3E166C881683954A008893AA /* RCViewController.h */, 127 | 3E166C891683954A008893AA /* RCViewController.m */, 128 | 3E166C711683954A008893AA /* Supporting Files */, 129 | ); 130 | path = RobotCalibration; 131 | sourceTree = ""; 132 | }; 133 | 3E166C711683954A008893AA /* Supporting Files */ = { 134 | isa = PBXGroup; 135 | children = ( 136 | 3E166C721683954A008893AA /* RobotCalibration-Info.plist */, 137 | 3E166C731683954A008893AA /* InfoPlist.strings */, 138 | 3E166C761683954A008893AA /* main.m */, 139 | 3E166C781683954A008893AA /* RobotCalibration-Prefix.pch */, 140 | 3E166C7C1683954A008893AA /* Default.png */, 141 | 3E166C7E1683954A008893AA /* Default@2x.png */, 142 | 3E166C801683954A008893AA /* Default-568h@2x.png */, 143 | ); 144 | name = "Supporting Files"; 145 | sourceTree = ""; 146 | }; 147 | 3E166C971683954A008893AA /* RobotCalibrationTests */ = { 148 | isa = PBXGroup; 149 | children = ( 150 | 3E166C9D1683954A008893AA /* RobotCalibrationTests.h */, 151 | 3E166C9E1683954A008893AA /* RobotCalibrationTests.m */, 152 | 3E166C981683954A008893AA /* Supporting Files */, 153 | ); 154 | path = RobotCalibrationTests; 155 | sourceTree = ""; 156 | }; 157 | 3E166C981683954A008893AA /* Supporting Files */ = { 158 | isa = PBXGroup; 159 | children = ( 160 | 3E166C991683954A008893AA /* RobotCalibrationTests-Info.plist */, 161 | 3E166C9A1683954A008893AA /* InfoPlist.strings */, 162 | ); 163 | name = "Supporting Files"; 164 | sourceTree = ""; 165 | }; 166 | /* End PBXGroup section */ 167 | 168 | /* Begin PBXNativeTarget section */ 169 | 3E166C661683954A008893AA /* RobotCalibration */ = { 170 | isa = PBXNativeTarget; 171 | buildConfigurationList = 3E166CA21683954A008893AA /* Build configuration list for PBXNativeTarget "RobotCalibration" */; 172 | buildPhases = ( 173 | 3E166C631683954A008893AA /* Sources */, 174 | 3E166C641683954A008893AA /* Frameworks */, 175 | 3E166C651683954A008893AA /* Resources */, 176 | ); 177 | buildRules = ( 178 | ); 179 | dependencies = ( 180 | ); 181 | name = RobotCalibration; 182 | productName = RobotCalibration; 183 | productReference = 3E166C671683954A008893AA /* RobotCalibration.app */; 184 | productType = "com.apple.product-type.application"; 185 | }; 186 | 3E166C8F1683954A008893AA /* RobotCalibrationTests */ = { 187 | isa = PBXNativeTarget; 188 | buildConfigurationList = 3E166CA51683954A008893AA /* Build configuration list for PBXNativeTarget "RobotCalibrationTests" */; 189 | buildPhases = ( 190 | 3E166C8B1683954A008893AA /* Sources */, 191 | 3E166C8C1683954A008893AA /* Frameworks */, 192 | 3E166C8D1683954A008893AA /* Resources */, 193 | 3E166C8E1683954A008893AA /* ShellScript */, 194 | ); 195 | buildRules = ( 196 | ); 197 | dependencies = ( 198 | 3E166C961683954A008893AA /* PBXTargetDependency */, 199 | ); 200 | name = RobotCalibrationTests; 201 | productName = RobotCalibrationTests; 202 | productReference = 3E166C901683954A008893AA /* RobotCalibrationTests.octest */; 203 | productType = "com.apple.product-type.bundle"; 204 | }; 205 | /* End PBXNativeTarget section */ 206 | 207 | /* Begin PBXProject section */ 208 | 3E166C5F1683954A008893AA /* Project object */ = { 209 | isa = PBXProject; 210 | attributes = { 211 | CLASSPREFIX = RC; 212 | LastUpgradeCheck = 0460; 213 | ORGANIZATIONNAME = "Appium Contributors"; 214 | }; 215 | buildConfigurationList = 3E166C621683954A008893AA /* Build configuration list for PBXProject "RobotCalibration" */; 216 | compatibilityVersion = "Xcode 3.2"; 217 | developmentRegion = English; 218 | hasScannedForEncodings = 0; 219 | knownRegions = ( 220 | en, 221 | ); 222 | mainGroup = 3E166C5E1683954A008893AA; 223 | productRefGroup = 3E166C681683954A008893AA /* Products */; 224 | projectDirPath = ""; 225 | projectRoot = ""; 226 | targets = ( 227 | 3E166C661683954A008893AA /* RobotCalibration */, 228 | 3E166C8F1683954A008893AA /* RobotCalibrationTests */, 229 | ); 230 | }; 231 | /* End PBXProject section */ 232 | 233 | /* Begin PBXResourcesBuildPhase section */ 234 | 3E166C651683954A008893AA /* Resources */ = { 235 | isa = PBXResourcesBuildPhase; 236 | buildActionMask = 2147483647; 237 | files = ( 238 | 3E166C751683954A008893AA /* InfoPlist.strings in Resources */, 239 | 3E166C7D1683954A008893AA /* Default.png in Resources */, 240 | 3E166C7F1683954A008893AA /* Default@2x.png in Resources */, 241 | 3E166C811683954A008893AA /* Default-568h@2x.png in Resources */, 242 | 3E166C841683954A008893AA /* MainStoryboard_iPhone.storyboard in Resources */, 243 | 3E166C871683954A008893AA /* MainStoryboard_iPad.storyboard in Resources */, 244 | ); 245 | runOnlyForDeploymentPostprocessing = 0; 246 | }; 247 | 3E166C8D1683954A008893AA /* Resources */ = { 248 | isa = PBXResourcesBuildPhase; 249 | buildActionMask = 2147483647; 250 | files = ( 251 | 3E166C9C1683954A008893AA /* InfoPlist.strings in Resources */, 252 | ); 253 | runOnlyForDeploymentPostprocessing = 0; 254 | }; 255 | /* End PBXResourcesBuildPhase section */ 256 | 257 | /* Begin PBXShellScriptBuildPhase section */ 258 | 3E166C8E1683954A008893AA /* ShellScript */ = { 259 | isa = PBXShellScriptBuildPhase; 260 | buildActionMask = 2147483647; 261 | files = ( 262 | ); 263 | inputPaths = ( 264 | ); 265 | outputPaths = ( 266 | ); 267 | runOnlyForDeploymentPostprocessing = 0; 268 | shellPath = /bin/sh; 269 | shellScript = "# Run the unit tests in this test bundle.\n\"${SYSTEM_DEVELOPER_DIR}/Tools/RunUnitTests\"\n"; 270 | }; 271 | /* End PBXShellScriptBuildPhase section */ 272 | 273 | /* Begin PBXSourcesBuildPhase section */ 274 | 3E166C631683954A008893AA /* Sources */ = { 275 | isa = PBXSourcesBuildPhase; 276 | buildActionMask = 2147483647; 277 | files = ( 278 | 3E166C771683954A008893AA /* main.m in Sources */, 279 | 3E166C7B1683954A008893AA /* RCAppDelegate.m in Sources */, 280 | 3E166C8A1683954A008893AA /* RCViewController.m in Sources */, 281 | ); 282 | runOnlyForDeploymentPostprocessing = 0; 283 | }; 284 | 3E166C8B1683954A008893AA /* Sources */ = { 285 | isa = PBXSourcesBuildPhase; 286 | buildActionMask = 2147483647; 287 | files = ( 288 | 3E166C9F1683954A008893AA /* RobotCalibrationTests.m in Sources */, 289 | ); 290 | runOnlyForDeploymentPostprocessing = 0; 291 | }; 292 | /* End PBXSourcesBuildPhase section */ 293 | 294 | /* Begin PBXTargetDependency section */ 295 | 3E166C961683954A008893AA /* PBXTargetDependency */ = { 296 | isa = PBXTargetDependency; 297 | target = 3E166C661683954A008893AA /* RobotCalibration */; 298 | targetProxy = 3E166C951683954A008893AA /* PBXContainerItemProxy */; 299 | }; 300 | /* End PBXTargetDependency section */ 301 | 302 | /* Begin PBXVariantGroup section */ 303 | 3E166C731683954A008893AA /* InfoPlist.strings */ = { 304 | isa = PBXVariantGroup; 305 | children = ( 306 | 3E166C741683954A008893AA /* en */, 307 | ); 308 | name = InfoPlist.strings; 309 | sourceTree = ""; 310 | }; 311 | 3E166C821683954A008893AA /* MainStoryboard_iPhone.storyboard */ = { 312 | isa = PBXVariantGroup; 313 | children = ( 314 | 3E166C831683954A008893AA /* en */, 315 | ); 316 | name = MainStoryboard_iPhone.storyboard; 317 | sourceTree = ""; 318 | }; 319 | 3E166C851683954A008893AA /* MainStoryboard_iPad.storyboard */ = { 320 | isa = PBXVariantGroup; 321 | children = ( 322 | 3E166C861683954A008893AA /* en */, 323 | ); 324 | name = MainStoryboard_iPad.storyboard; 325 | sourceTree = ""; 326 | }; 327 | 3E166C9A1683954A008893AA /* InfoPlist.strings */ = { 328 | isa = PBXVariantGroup; 329 | children = ( 330 | 3E166C9B1683954A008893AA /* en */, 331 | ); 332 | name = InfoPlist.strings; 333 | sourceTree = ""; 334 | }; 335 | /* End PBXVariantGroup section */ 336 | 337 | /* Begin XCBuildConfiguration section */ 338 | 3E166CA01683954A008893AA /* Debug */ = { 339 | isa = XCBuildConfiguration; 340 | buildSettings = { 341 | ALWAYS_SEARCH_USER_PATHS = NO; 342 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 343 | CLANG_CXX_LIBRARY = "libc++"; 344 | CLANG_ENABLE_OBJC_ARC = YES; 345 | CLANG_WARN_CONSTANT_CONVERSION = YES; 346 | CLANG_WARN_EMPTY_BODY = YES; 347 | CLANG_WARN_ENUM_CONVERSION = YES; 348 | CLANG_WARN_INT_CONVERSION = YES; 349 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 350 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 351 | COPY_PHASE_STRIP = NO; 352 | GCC_C_LANGUAGE_STANDARD = gnu99; 353 | GCC_DYNAMIC_NO_PIC = NO; 354 | GCC_OPTIMIZATION_LEVEL = 0; 355 | GCC_PREPROCESSOR_DEFINITIONS = ( 356 | "DEBUG=1", 357 | "$(inherited)", 358 | ); 359 | GCC_SYMBOLS_PRIVATE_EXTERN = NO; 360 | GCC_WARN_ABOUT_RETURN_TYPE = YES; 361 | GCC_WARN_UNINITIALIZED_AUTOS = YES; 362 | GCC_WARN_UNUSED_VARIABLE = YES; 363 | IPHONEOS_DEPLOYMENT_TARGET = 5.0; 364 | ONLY_ACTIVE_ARCH = YES; 365 | SDKROOT = iphoneos; 366 | TARGETED_DEVICE_FAMILY = "1,2"; 367 | }; 368 | name = Debug; 369 | }; 370 | 3E166CA11683954A008893AA /* Release */ = { 371 | isa = XCBuildConfiguration; 372 | buildSettings = { 373 | ALWAYS_SEARCH_USER_PATHS = NO; 374 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 375 | CLANG_CXX_LIBRARY = "libc++"; 376 | CLANG_ENABLE_OBJC_ARC = YES; 377 | CLANG_WARN_CONSTANT_CONVERSION = YES; 378 | CLANG_WARN_EMPTY_BODY = YES; 379 | CLANG_WARN_ENUM_CONVERSION = YES; 380 | CLANG_WARN_INT_CONVERSION = YES; 381 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 382 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 383 | COPY_PHASE_STRIP = YES; 384 | GCC_C_LANGUAGE_STANDARD = gnu99; 385 | GCC_WARN_ABOUT_RETURN_TYPE = YES; 386 | GCC_WARN_UNINITIALIZED_AUTOS = YES; 387 | GCC_WARN_UNUSED_VARIABLE = YES; 388 | IPHONEOS_DEPLOYMENT_TARGET = 5.0; 389 | OTHER_CFLAGS = "-DNS_BLOCK_ASSERTIONS=1"; 390 | SDKROOT = iphoneos; 391 | TARGETED_DEVICE_FAMILY = "1,2"; 392 | VALIDATE_PRODUCT = YES; 393 | }; 394 | name = Release; 395 | }; 396 | 3E166CA31683954A008893AA /* Debug */ = { 397 | isa = XCBuildConfiguration; 398 | buildSettings = { 399 | GCC_PRECOMPILE_PREFIX_HEADER = YES; 400 | GCC_PREFIX_HEADER = "RobotCalibration/RobotCalibration-Prefix.pch"; 401 | INFOPLIST_FILE = "RobotCalibration/RobotCalibration-Info.plist"; 402 | IPHONEOS_DEPLOYMENT_TARGET = 5.0; 403 | PRODUCT_NAME = "$(TARGET_NAME)"; 404 | WRAPPER_EXTENSION = app; 405 | }; 406 | name = Debug; 407 | }; 408 | 3E166CA41683954A008893AA /* Release */ = { 409 | isa = XCBuildConfiguration; 410 | buildSettings = { 411 | GCC_PRECOMPILE_PREFIX_HEADER = YES; 412 | GCC_PREFIX_HEADER = "RobotCalibration/RobotCalibration-Prefix.pch"; 413 | INFOPLIST_FILE = "RobotCalibration/RobotCalibration-Info.plist"; 414 | IPHONEOS_DEPLOYMENT_TARGET = 5.0; 415 | PRODUCT_NAME = "$(TARGET_NAME)"; 416 | WRAPPER_EXTENSION = app; 417 | }; 418 | name = Release; 419 | }; 420 | 3E166CA61683954A008893AA /* Debug */ = { 421 | isa = XCBuildConfiguration; 422 | buildSettings = { 423 | BUNDLE_LOADER = "$(BUILT_PRODUCTS_DIR)/RobotCalibration.app/RobotCalibration"; 424 | FRAMEWORK_SEARCH_PATHS = ( 425 | "\"$(SDKROOT)/Developer/Library/Frameworks\"", 426 | "\"$(DEVELOPER_LIBRARY_DIR)/Frameworks\"", 427 | ); 428 | GCC_PRECOMPILE_PREFIX_HEADER = YES; 429 | GCC_PREFIX_HEADER = "RobotCalibration/RobotCalibration-Prefix.pch"; 430 | INFOPLIST_FILE = "RobotCalibrationTests/RobotCalibrationTests-Info.plist"; 431 | PRODUCT_NAME = "$(TARGET_NAME)"; 432 | TEST_HOST = "$(BUNDLE_LOADER)"; 433 | WRAPPER_EXTENSION = octest; 434 | }; 435 | name = Debug; 436 | }; 437 | 3E166CA71683954A008893AA /* Release */ = { 438 | isa = XCBuildConfiguration; 439 | buildSettings = { 440 | BUNDLE_LOADER = "$(BUILT_PRODUCTS_DIR)/RobotCalibration.app/RobotCalibration"; 441 | FRAMEWORK_SEARCH_PATHS = ( 442 | "\"$(SDKROOT)/Developer/Library/Frameworks\"", 443 | "\"$(DEVELOPER_LIBRARY_DIR)/Frameworks\"", 444 | ); 445 | GCC_PRECOMPILE_PREFIX_HEADER = YES; 446 | GCC_PREFIX_HEADER = "RobotCalibration/RobotCalibration-Prefix.pch"; 447 | INFOPLIST_FILE = "RobotCalibrationTests/RobotCalibrationTests-Info.plist"; 448 | PRODUCT_NAME = "$(TARGET_NAME)"; 449 | TEST_HOST = "$(BUNDLE_LOADER)"; 450 | WRAPPER_EXTENSION = octest; 451 | }; 452 | name = Release; 453 | }; 454 | /* End XCBuildConfiguration section */ 455 | 456 | /* Begin XCConfigurationList section */ 457 | 3E166C621683954A008893AA /* Build configuration list for PBXProject "RobotCalibration" */ = { 458 | isa = XCConfigurationList; 459 | buildConfigurations = ( 460 | 3E166CA01683954A008893AA /* Debug */, 461 | 3E166CA11683954A008893AA /* Release */, 462 | ); 463 | defaultConfigurationIsVisible = 0; 464 | defaultConfigurationName = Release; 465 | }; 466 | 3E166CA21683954A008893AA /* Build configuration list for PBXNativeTarget "RobotCalibration" */ = { 467 | isa = XCConfigurationList; 468 | buildConfigurations = ( 469 | 3E166CA31683954A008893AA /* Debug */, 470 | 3E166CA41683954A008893AA /* Release */, 471 | ); 472 | defaultConfigurationIsVisible = 0; 473 | defaultConfigurationName = Release; 474 | }; 475 | 3E166CA51683954A008893AA /* Build configuration list for PBXNativeTarget "RobotCalibrationTests" */ = { 476 | isa = XCConfigurationList; 477 | buildConfigurations = ( 478 | 3E166CA61683954A008893AA /* Debug */, 479 | 3E166CA71683954A008893AA /* Release */, 480 | ); 481 | defaultConfigurationIsVisible = 0; 482 | defaultConfigurationName = Release; 483 | }; 484 | /* End XCConfigurationList section */ 485 | }; 486 | rootObject = 3E166C5F1683954A008893AA /* Project object */; 487 | } 488 | -------------------------------------------------------------------------------- /apps/ios/src/RobotCalibration/RobotCalibration.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /apps/ios/src/RobotCalibration/RobotCalibration.xcodeproj/project.xcworkspace/xcuserdata/danc.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/appium-boneyard/robots/1f0b94cadda179616c71c26453fb05f80d2f53c8/apps/ios/src/RobotCalibration/RobotCalibration.xcodeproj/project.xcworkspace/xcuserdata/danc.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /apps/ios/src/RobotCalibration/RobotCalibration.xcodeproj/xcuserdata/danc.xcuserdatad/xcschemes/RobotCalibration.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 33 | 39 | 40 | 41 | 42 | 43 | 49 | 50 | 51 | 52 | 61 | 62 | 68 | 69 | 70 | 71 | 72 | 73 | 79 | 80 | 86 | 87 | 88 | 89 | 91 | 92 | 95 | 96 | 97 | -------------------------------------------------------------------------------- /apps/ios/src/RobotCalibration/RobotCalibration.xcodeproj/xcuserdata/danc.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | RobotCalibration.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 3E166C661683954A008893AA 16 | 17 | primary 18 | 19 | 20 | 3E166C8F1683954A008893AA 21 | 22 | primary 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /apps/ios/src/RobotCalibration/RobotCalibration/Default-568h@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/appium-boneyard/robots/1f0b94cadda179616c71c26453fb05f80d2f53c8/apps/ios/src/RobotCalibration/RobotCalibration/Default-568h@2x.png -------------------------------------------------------------------------------- /apps/ios/src/RobotCalibration/RobotCalibration/Default.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/appium-boneyard/robots/1f0b94cadda179616c71c26453fb05f80d2f53c8/apps/ios/src/RobotCalibration/RobotCalibration/Default.png -------------------------------------------------------------------------------- /apps/ios/src/RobotCalibration/RobotCalibration/Default@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/appium-boneyard/robots/1f0b94cadda179616c71c26453fb05f80d2f53c8/apps/ios/src/RobotCalibration/RobotCalibration/Default@2x.png -------------------------------------------------------------------------------- /apps/ios/src/RobotCalibration/RobotCalibration/RCAppDelegate.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2012 Appium Committers 3 | * 4 | * Licensed to the Apache Software Foundation (ASF) under one 5 | * or more contributor license agreements. See the NOTICE file 6 | * distributed with this work for additional information 7 | * regarding copyright ownership. The ASF licenses this file 8 | * to you under the Apache License, Version 2.0 (the 9 | * "License"); you may not use this file except in compliance 10 | * with the License. You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, 15 | * software distributed under the License is distributed on an 16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 | * KIND, either express or implied. See the License for the 18 | * specific language governing permissions and limitations 19 | * under the License. 20 | */ 21 | 22 | #import 23 | 24 | @interface RCAppDelegate : UIResponder 25 | 26 | @property (strong, nonatomic) UIWindow *window; 27 | 28 | @end 29 | -------------------------------------------------------------------------------- /apps/ios/src/RobotCalibration/RobotCalibration/RCAppDelegate.m: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2012 Appium Committers 3 | * 4 | * Licensed to the Apache Software Foundation (ASF) under one 5 | * or more contributor license agreements. See the NOTICE file 6 | * distributed with this work for additional information 7 | * regarding copyright ownership. The ASF licenses this file 8 | * to you under the Apache License, Version 2.0 (the 9 | * "License"); you may not use this file except in compliance 10 | * with the License. You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, 15 | * software distributed under the License is distributed on an 16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 | * KIND, either express or implied. See the License for the 18 | * specific language governing permissions and limitations 19 | * under the License. 20 | */ 21 | 22 | #import "RCAppDelegate.h" 23 | 24 | @implementation RCAppDelegate 25 | 26 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 27 | { 28 | // Override point for customization after application launch. 29 | return YES; 30 | } 31 | 32 | - (void)applicationWillResignActive:(UIApplication *)application 33 | { 34 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 35 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 36 | } 37 | 38 | - (void)applicationDidEnterBackground:(UIApplication *)application 39 | { 40 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 41 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 42 | } 43 | 44 | - (void)applicationWillEnterForeground:(UIApplication *)application 45 | { 46 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 47 | } 48 | 49 | - (void)applicationDidBecomeActive:(UIApplication *)application 50 | { 51 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 52 | } 53 | 54 | - (void)applicationWillTerminate:(UIApplication *)application 55 | { 56 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 57 | } 58 | 59 | @end 60 | -------------------------------------------------------------------------------- /apps/ios/src/RobotCalibration/RobotCalibration/RCViewController.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2012 Appium Committers 3 | * 4 | * Licensed to the Apache Software Foundation (ASF) under one 5 | * or more contributor license agreements. See the NOTICE file 6 | * distributed with this work for additional information 7 | * regarding copyright ownership. The ASF licenses this file 8 | * to you under the Apache License, Version 2.0 (the 9 | * "License"); you may not use this file except in compliance 10 | * with the License. You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, 15 | * software distributed under the License is distributed on an 16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 | * KIND, either express or implied. See the License for the 18 | * specific language governing permissions and limitations 19 | * under the License. 20 | */ 21 | 22 | #import 23 | 24 | @interface RCViewController : UIViewController 25 | @property (strong, nonatomic) IBOutlet UITapGestureRecognizer *tapRecognizer; 26 | @property (strong, nonatomic) IBOutlet UILabel *coordinateLabel; 27 | @property (strong, nonatomic) IBOutlet UISwipeGestureRecognizer *swipeRecognizer; 28 | 29 | @end 30 | -------------------------------------------------------------------------------- /apps/ios/src/RobotCalibration/RobotCalibration/RCViewController.m: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2012 Appium Committers 3 | * 4 | * Licensed to the Apache Software Foundation (ASF) under one 5 | * or more contributor license agreements. See the NOTICE file 6 | * distributed with this work for additional information 7 | * regarding copyright ownership. The ASF licenses this file 8 | * to you under the Apache License, Version 2.0 (the 9 | * "License"); you may not use this file except in compliance 10 | * with the License. You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, 15 | * software distributed under the License is distributed on an 16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 | * KIND, either express or implied. See the License for the 18 | * specific language governing permissions and limitations 19 | * under the License. 20 | */ 21 | 22 | #import "RCViewController.h" 23 | 24 | @interface RCViewController () 25 | 26 | @end 27 | 28 | @implementation RCViewController 29 | 30 | - (void)viewDidLoad 31 | { 32 | [super viewDidLoad]; 33 | // Do any additional setup after loading the view, typically from a nib. 34 | [self becomeFirstResponder]; 35 | 36 | } 37 | 38 | - (void)didReceiveMemoryWarning 39 | { 40 | [super didReceiveMemoryWarning]; 41 | // Dispose of any resources that can be recreated. 42 | } 43 | 44 | - (NSUInteger)supportedInterfaceOrientations 45 | { 46 | return UIInterfaceOrientationMaskAllButUpsideDown; 47 | } 48 | 49 | - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation 50 | { 51 | if ((orientation == UIInterfaceOrientationPortrait) || 52 | (orientation == UIInterfaceOrientationLandscapeLeft) || 53 | (orientation == UIInterfaceOrientationLandscapeRight)) 54 | return YES; 55 | 56 | return NO; 57 | } 58 | 59 | - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { 60 | [self.view setNeedsLayout]; 61 | [self.view setNeedsDisplay]; 62 | } 63 | 64 | 65 | - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 66 | //UITouch *touch = [touches anyObject]; 67 | for (UIView *view in self.view.subviews) { 68 | [view removeFromSuperview]; 69 | } 70 | for (UITouch *touch in [[event allTouches] allObjects]) { 71 | [self displayCoordinatesForTouch:touch]; 72 | } 73 | } 74 | 75 | - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 76 | //UITouch *touch = [touches anyObject]; 77 | for (UIView *view in self.view.subviews) { 78 | [view removeFromSuperview]; 79 | } 80 | for (UITouch *touch in [[event allTouches] allObjects]) { 81 | [self displayCoordinatesForTouch:touch]; 82 | } 83 | 84 | } 85 | 86 | - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 87 | for (UIView *view in self.view.subviews) { 88 | [view removeFromSuperview]; 89 | } 90 | } 91 | 92 | - (void)displayCoordinatesForTouch:(UITouch *)touch { 93 | CGPoint locationInView = [touch locationInView:self.view]; 94 | int viewWidth = self.view.frame.size.width; 95 | int viewHeight = self.view.frame.size.height; 96 | 97 | if (([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft) || 98 | ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight)) { 99 | viewWidth = self.view.frame.size.height; 100 | viewHeight = self.view.frame.size.width; 101 | } 102 | // Calculate the Coordinate Label Frame 103 | CGRect coordinateLabelFrame = CGRectMake(0, 0, 0, 0); 104 | coordinateLabelFrame.size.height = 20; 105 | coordinateLabelFrame.size.width = 100; 106 | // We want the label to stay 40pts from the edges 107 | coordinateLabelFrame.origin.x = MIN(MAX(locationInView.x - 40, 40), viewWidth - coordinateLabelFrame.size.width - 40); 108 | coordinateLabelFrame.origin.y = MIN(MAX(locationInView.y - 30, 40), viewHeight - coordinateLabelFrame.size.height - 40); 109 | 110 | // Set the frame and test of the label 111 | UILabel *coordinateLabel = [[UILabel alloc] initWithFrame:coordinateLabelFrame]; 112 | coordinateLabel.text = [NSString stringWithFormat:@"(%d, %d)", (int)locationInView.x, (int)locationInView.y]; 113 | coordinateLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18.0f]; 114 | [self.view addSubview:coordinateLabel]; 115 | } 116 | 117 | @end 118 | -------------------------------------------------------------------------------- /apps/ios/src/RobotCalibration/RobotCalibration/RobotCalibration-Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleDisplayName 8 | ${PRODUCT_NAME} 9 | CFBundleExecutable 10 | ${EXECUTABLE_NAME} 11 | CFBundleIdentifier 12 | Appium.${PRODUCT_NAME:rfc1034identifier} 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | ${PRODUCT_NAME} 17 | CFBundlePackageType 18 | APPL 19 | CFBundleShortVersionString 20 | 1.0 21 | CFBundleSignature 22 | ???? 23 | CFBundleVersion 24 | 1.0 25 | LSRequiresIPhoneOS 26 | 27 | UIMainStoryboardFile 28 | MainStoryboard_iPhone 29 | UIMainStoryboardFile~ipad 30 | MainStoryboard_iPad 31 | UIRequiredDeviceCapabilities 32 | 33 | armv7 34 | 35 | UISupportedInterfaceOrientations 36 | 37 | UIInterfaceOrientationPortrait 38 | UIInterfaceOrientationLandscapeLeft 39 | UIInterfaceOrientationLandscapeRight 40 | UIInterfaceOrientationPortraitUpsideDown 41 | 42 | UISupportedInterfaceOrientations~ipad 43 | 44 | UIInterfaceOrientationPortrait 45 | UIInterfaceOrientationPortraitUpsideDown 46 | UIInterfaceOrientationLandscapeLeft 47 | UIInterfaceOrientationLandscapeRight 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /apps/ios/src/RobotCalibration/RobotCalibration/RobotCalibration-Prefix.pch: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2012 Appium Committers 3 | * 4 | * Licensed to the Apache Software Foundation (ASF) under one 5 | * or more contributor license agreements. See the NOTICE file 6 | * distributed with this work for additional information 7 | * regarding copyright ownership. The ASF licenses this file 8 | * to you under the Apache License, Version 2.0 (the 9 | * "License"); you may not use this file except in compliance 10 | * with the License. You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, 15 | * software distributed under the License is distributed on an 16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 | * KIND, either express or implied. See the License for the 18 | * specific language governing permissions and limitations 19 | * under the License. 20 | */ 21 | 22 | #import 23 | 24 | #ifndef __IPHONE_5_0 25 | #warning "This project uses features only available in iOS SDK 5.0 and later." 26 | #endif 27 | 28 | #ifdef __OBJC__ 29 | #import 30 | #import 31 | #endif 32 | -------------------------------------------------------------------------------- /apps/ios/src/RobotCalibration/RobotCalibration/en.lproj/InfoPlist.strings: -------------------------------------------------------------------------------- 1 | /* Localized versions of Info.plist keys */ 2 | 3 | -------------------------------------------------------------------------------- /apps/ios/src/RobotCalibration/RobotCalibration/en.lproj/MainStoryboard_iPad.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /apps/ios/src/RobotCalibration/RobotCalibration/en.lproj/MainStoryboard_iPhone.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /apps/ios/src/RobotCalibration/RobotCalibration/main.m: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2012 Appium Committers 3 | * 4 | * Licensed to the Apache Software Foundation (ASF) under one 5 | * or more contributor license agreements. See the NOTICE file 6 | * distributed with this work for additional information 7 | * regarding copyright ownership. The ASF licenses this file 8 | * to you under the Apache License, Version 2.0 (the 9 | * "License"); you may not use this file except in compliance 10 | * with the License. You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, 15 | * software distributed under the License is distributed on an 16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 | * KIND, either express or implied. See the License for the 18 | * specific language governing permissions and limitations 19 | * under the License. 20 | */ 21 | 22 | #import 23 | 24 | #import "RCAppDelegate.h" 25 | 26 | int main(int argc, char *argv[]) 27 | { 28 | @autoreleasepool { 29 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([RCAppDelegate class])); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /apps/ios/src/RobotCalibration/RobotCalibrationTests/RobotCalibrationTests-Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIdentifier 10 | Appium.${PRODUCT_NAME:rfc1034identifier} 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundlePackageType 14 | BNDL 15 | CFBundleShortVersionString 16 | 1.0 17 | CFBundleSignature 18 | ???? 19 | CFBundleVersion 20 | 1 21 | 22 | 23 | -------------------------------------------------------------------------------- /apps/ios/src/RobotCalibration/RobotCalibrationTests/RobotCalibrationTests.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2012 Appium Committers 3 | * 4 | * Licensed to the Apache Software Foundation (ASF) under one 5 | * or more contributor license agreements. See the NOTICE file 6 | * distributed with this work for additional information 7 | * regarding copyright ownership. The ASF licenses this file 8 | * to you under the Apache License, Version 2.0 (the 9 | * "License"); you may not use this file except in compliance 10 | * with the License. You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, 15 | * software distributed under the License is distributed on an 16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 | * KIND, either express or implied. See the License for the 18 | * specific language governing permissions and limitations 19 | * under the License. 20 | */ 21 | 22 | #import 23 | 24 | @interface RobotCalibrationTests : SenTestCase 25 | 26 | @end 27 | -------------------------------------------------------------------------------- /apps/ios/src/RobotCalibration/RobotCalibrationTests/RobotCalibrationTests.m: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2012 Appium Committers 3 | * 4 | * Licensed to the Apache Software Foundation (ASF) under one 5 | * or more contributor license agreements. See the NOTICE file 6 | * distributed with this work for additional information 7 | * regarding copyright ownership. The ASF licenses this file 8 | * to you under the Apache License, Version 2.0 (the 9 | * "License"); you may not use this file except in compliance 10 | * with the License. You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, 15 | * software distributed under the License is distributed on an 16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 | * KIND, either express or implied. See the License for the 18 | * specific language governing permissions and limitations 19 | * under the License. 20 | */ 21 | 22 | #import "RobotCalibrationTests.h" 23 | 24 | @implementation RobotCalibrationTests 25 | 26 | - (void)setUp 27 | { 28 | [super setUp]; 29 | 30 | // Set-up code here. 31 | } 32 | 33 | - (void)tearDown 34 | { 35 | // Tear-down code here. 36 | 37 | [super tearDown]; 38 | } 39 | 40 | - (void)testExample 41 | { 42 | STFail(@"Unit tests are not implemented yet in RobotCalibrationTests"); 43 | } 44 | 45 | @end 46 | -------------------------------------------------------------------------------- /apps/ios/src/RobotCalibration/RobotCalibrationTests/en.lproj/InfoPlist.strings: -------------------------------------------------------------------------------- 1 | /* Localized versions of Info.plist keys */ 2 | 3 | -------------------------------------------------------------------------------- /bots/bitbeambot/calibrate.js: -------------------------------------------------------------------------------- 1 | 2 | var fs = require('fs') 3 | , wdSync = require('wd-sync') 4 | , robot = require('./client').client("127.0.0.1","4242") 5 | , usleep = require('sleep').usleep 6 | , ArgumentParser = require('argparse').ArgumentParser; 7 | 8 | var client = wdSync.remote("127.0.0.1", 4723) 9 | , browser = client.browser 10 | , sync = client.sync; 11 | 12 | var parser = new ArgumentParser({ 13 | version: '0.0.1', 14 | addHelp:true, 15 | description: 'Bitbeambot Calibration Script' 16 | }); 17 | parser.addArgument( 18 | [ '-o', '--output' ], 19 | { 20 | help: 'file to save calibration data to' 21 | } 22 | ); 23 | 24 | var args = parser.parseArgs(); 25 | 26 | sync( function() { 27 | 28 | // Reset Robot to Starting Position 29 | robot.reset(); 30 | 31 | console.log("server status:", browser.status()); 32 | browser.init( { browserName: 'iOS', app: 'Appium.RobotCalibration'} ); 33 | console.log("session capabilities:", browser.sessionCapabilities()); 34 | browser.setWaitTimeout(3000); 35 | console.log(browser.source()); 36 | 37 | var data = []; 38 | 39 | var getCoords = function() { 40 | try { 41 | // disabled due to bug with appium where the label is not updating 42 | // var value = browser.elementByTagName('staticText').text(); 43 | // if (value !== undefined) { 44 | // return eval(value.replace("(","[").replace(")", "]")); 45 | // } 46 | // return value; 47 | 48 | // hack to find value since element.text is not updated 49 | var coordRegex = /label[^\(,]+\((\d+\.*\d*),\s+(\d+\.*\d*)\)/ ; 50 | var pageSource = browser.source(); 51 | if (coordRegex.test(pageSource)) { 52 | var match = coordRegex.exec(pageSource); 53 | return [ parseFloat(match[1]), parseFloat(match[2])]; 54 | } 55 | else { 56 | return undefined; 57 | } 58 | } 59 | catch(err) { 60 | return undefined; 61 | } 62 | }; 63 | 64 | var findPoint = function(deltaX, deltaY) { 65 | 66 | // reset position 67 | robot.reset(); 68 | usleep(500000); 69 | 70 | // move above the point 71 | var newPoint = [contactPoint.position[0] + deltaX, contactPoint.position[1] + deltaY, contactPoint.position[2] *.87]; 72 | robot.setPosition(newPoint[0], newPoint[1], newPoint[2]); 73 | 74 | // touch the device 75 | while (Math.abs(newPoint[2]) < Math.abs(contactPoint.position[2]*1.05)) { 76 | newPoint[2] = newPoint[2]*1.025; 77 | robot.setPosition(newPoint[0], newPoint[1], newPoint[2]); 78 | var location = getCoords(); 79 | if (location !== undefined && location.length >= 2) { 80 | var dataPoint = { position: newPoint, coordinates: location}; 81 | console.log(dataPoint); 82 | data.push(dataPoint); 83 | break; 84 | } 85 | } 86 | }; 87 | 88 | // Find Contact Point 89 | console.log("Finding Contact Point..."); 90 | var startingPosition = robot.position(); 91 | var currentPosition = [startingPosition[0], startingPosition[1], startingPosition[2]]; 92 | var coords = undefined; 93 | while (Math.abs(currentPosition[2] - startingPosition[2]) < 50) { 94 | currentPosition[2] = currentPosition[2]*1.025; 95 | robot.setPosition(currentPosition[0], currentPosition[1], currentPosition[2]); 96 | coords = getCoords(); 97 | if (coords !== undefined && coords.length >= 2) { 98 | break; 99 | } 100 | } 101 | console.log("Contact Point Found!"); 102 | var contactPoint = { position: robot.position(), coordinates: coords }; 103 | console.log(contactPoint); 104 | data.push(contactPoint); 105 | robot.reset(); 106 | usleep(500000); 107 | 108 | var newPoint; 109 | console.log("Determining Directions"); 110 | 111 | console.log("+x direction"); 112 | findPoint(50,0); 113 | console.log("+y direction"); 114 | findPoint(0,50); 115 | console.log("-x direction"); 116 | findPoint(-50,0); 117 | console.log("-y direction"); 118 | findPoint(0,-50); 119 | /* 120 | console.log("x,y corner"); 121 | findPoint(50,50); 122 | console.log("x,-y corner"); 123 | findPoint(50,-50); 124 | console.log("-x,y corner"); 125 | findPoint(-50,50); 126 | console.log("-x,-y corner"); 127 | findPoint(-50,-50); 128 | */ 129 | 130 | console.log("FINAL DATA"); 131 | console.log(data); 132 | 133 | robot.reset(); 134 | browser.quit(); 135 | 136 | fs.writeFile(args.output, JSON.stringify(data, null, 2), function(err) { 137 | if(err) { 138 | console.log(err); 139 | } else { 140 | console.log("Calibration Data Saved!"); 141 | } 142 | }); 143 | 144 | }); 145 | -------------------------------------------------------------------------------- /bots/bitbeambot/client.js: -------------------------------------------------------------------------------- 1 | var request = require('request') 2 | , XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; 3 | 4 | exports.client = function(address, port) { 5 | 6 | return { 7 | address : address, 8 | port : port, 9 | url : function(uri) { 10 | return 'http://' + address + ":" + port + uri; 11 | }, 12 | angles : function() { 13 | var xmlHttp = new XMLHttpRequest(); 14 | xmlHttp.open( "GET", this.url('/angles'), false ); 15 | xmlHttp.send( null ); 16 | return eval(xmlHttp.responseText); 17 | }, 18 | setAngles : function(theta1, theta2, theta3) { 19 | request.post(this.url('/setAngles'), {form:{theta1:theta1, theta2:theta2, theta3:theta3}}); 20 | }, 21 | position : function() { 22 | var xmlHttp = new XMLHttpRequest(); 23 | xmlHttp.open( "GET", this.url('/position'), false ); 24 | xmlHttp.send( null ); 25 | return eval(xmlHttp.responseText); 26 | }, 27 | setPosition : function(x, y, z) { 28 | request.post(this.url('/setPosition'), {form:{x:x, y:y, z:z}}); 29 | }, 30 | positionForCoordinates : function(x,y) { 31 | var xmlHttp = new XMLHttpRequest(); 32 | xmlHttp.open( "GET", this.url('/positionForCoordinates/x/' + x + "/y/" + y), false ); 33 | xmlHttp.send( null ); 34 | return eval(xmlHttp.responseText); 35 | }, 36 | coordinatesForPosition : function(x,y) { 37 | var xmlHttp = new XMLHttpRequest(); 38 | xmlHttp.open( "GET", this.url('/coordinatesForPosition/x/' + x + "/y/" + y), false ); 39 | xmlHttp.send( null ); 40 | return eval(xmlHttp.responseText); 41 | }, 42 | reset : function() { 43 | var xmlHttp = new XMLHttpRequest(); 44 | xmlHttp.open( "GET", this.url('/reset'), false ); 45 | xmlHttp.send( null ); 46 | return eval(xmlHttp.responseText); 47 | }, 48 | tap : function(x,y) { 49 | var xmlHttp = new XMLHttpRequest(); 50 | xmlHttp.open( "GET", this.url('/tap/x/' + x + "/y/" + y), false ); 51 | xmlHttp.send( null ); 52 | return xmlHttp.responseText; 53 | } 54 | }; 55 | 56 | }; -------------------------------------------------------------------------------- /bots/bitbeambot/kinematics.js: -------------------------------------------------------------------------------- 1 | // Original code from 2 | // http://forums.trossenrobotics.com/tutorials/introduction-129/delta-robot-kinematics-3276/ 3 | 4 | // Specific geometry for bitbeambot: 5 | // http://flic.kr/p/cYaQah 6 | var e=26.0; 7 | var f=69.0; 8 | var re=128.0; 9 | var rf=88.0; 10 | 11 | // Trigonometric constants 12 | var s=165*2; 13 | var sqrt3=Math.sqrt(3.0); 14 | var pi=3.141592653; 15 | var sin120=sqrt3/2.0; 16 | var cos120=-0.5; 17 | var tan60=sqrt3; 18 | var sin30=0.5; 19 | var tan30=1.0/sqrt3; 20 | 21 | // Forward kinematics: (theta1, theta2, theta3) -> (x0, y0, z0) 22 | // Returned {error code,theta1,theta2,theta3} 23 | exports.delta_calcForward = function(theta1, theta2, theta3) { 24 | var x0 = 0.0; 25 | var y0 = 0.0; 26 | var z0 = 0.0; 27 | 28 | var t = (f-e)*tan30/2.0; 29 | var dtr = pi/180.0; 30 | 31 | theta1 *= dtr; 32 | theta2 *= dtr; 33 | theta3 *= dtr; 34 | 35 | var y1 = -(t+rf*Math.cos(theta1)); 36 | var z1 = -rf*Math.sin(theta1); 37 | 38 | var y2 = (t+rf*Math.cos(theta2))*sin30; 39 | var x2 = y2*tan60; 40 | var z2 = -rf*Math.sin(theta2); 41 | 42 | var y3 = (t+rf*Math.cos(theta3))*sin30; 43 | var x3 = -y3*tan60; 44 | var z3 = -rf*Math.sin(theta3); 45 | 46 | var dnm = (y2-y1)*x3-(y3-y1)*x2; 47 | 48 | var w1 = y1*y1 + z1*z1; 49 | var w2 = x2*x2 + y2*y2 + z2*z2; 50 | var w3 = x3*x3 + y3*y3 + z3*z3; 51 | 52 | // x = (a1*z + b1)/dnm 53 | var a1 = (z2-z1)*(y3-y1)-(z3-z1)*(y2-y1); 54 | var b1=-((w2-w1)*(y3-y1)-(w3-w1)*(y2-y1))/2.0; 55 | 56 | // y = (a2*z + b2)/dnm; 57 | var a2 = -(z2-z1)*x3+(z3-z1)*x2; 58 | var b2=((w2-w1)*x3-(w3-w1)*x2)/2.0; 59 | 60 | // a*z^2 + b*z + c = 0 61 | var a=a1*a1+a2*a2+dnm*dnm; 62 | var b=2.0*(a1*b1+a2*(b2-y1*dnm)-z1*dnm*dnm); 63 | var c=(b2-y1*dnm)*(b2-y1*dnm)+b1*b1+dnm*dnm*(z1*z1-re*re); 64 | 65 | // discriminant 66 | var d = b*b - 4.0*a*c; 67 | if (d<0.0) return Array(1,0,0,0); // non-existing povar. return error,x,y,z 68 | 69 | z0 = -0.5*(b+Math.sqrt(d))/a; 70 | x0=(a1*z0+b1)/dnm; 71 | y0=(a2*z0+b2)/dnm; 72 | 73 | return Array(0,x0,y0,z0); 74 | } 75 | 76 | // Inverse kinematics 77 | // Helper functions, calculates angle theta1 (for YZ-pane) 78 | delta_calcAngleYZ = function(x0, y0, z0) { 79 | var y1 = -0.5*0.57735*f; // f/2 * tg 30 80 | y0 -= 0.5*0.57735*e; // shift center to edge 81 | // z = a + b*y 82 | var a = (x0*x0+y0*y0+z0*z0+rf*rf-re*re-y1*y1)/(2.0*z0); 83 | var b = (y1-y0)/z0; 84 | 85 | // discriminant 86 | var d = -(a+b*y1)*(a+b*y1)+rf*(b*b*rf+rf); 87 | if (d<0) return Array(1,0); // non-existing povar. return error, theta 88 | 89 | var yj = (y1-a*b-Math.sqrt(d))/(b*b+1); // choosing outer povar 90 | var zj=a+b*yj; 91 | theta=Math.atan(-zj/(y1-yj))*180.0/pi+((yj>y1)?180.0:0.0); 92 | 93 | return Array(0,theta); // return error, theta 94 | } 95 | 96 | exports.delta_calcInverse = function(x0, y0, z0) { 97 | var theta1 = 0; 98 | var theta2 = 0; 99 | var theta3 = 0; 100 | var status = delta_calcAngleYZ(x0,y0,z0); 101 | 102 | if (status[0]==0) { 103 | theta1 = status[1]; 104 | status = delta_calcAngleYZ(x0*cos120+y0*sin120,y0*cos120-x0*sin120,z0,theta2); 105 | } 106 | if (status[0]==0) { 107 | theta2 = status[1]; 108 | status = delta_calcAngleYZ(x0*cos120-y0*sin120,y0*cos120+x0*sin120,z0,theta3); 109 | } 110 | theta3 = status[1]; 111 | 112 | return Array(status[0],theta1,theta2,theta3); 113 | } -------------------------------------------------------------------------------- /bots/bitbeambot/sample-calibration.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "position": [ 4 | 0, 5 | 0, 6 | -169.25108293361723 7 | ], 8 | "coordinates": [ 9 | 377, 10 | 479 11 | ] 12 | }, 13 | { 14 | "position": [ 15 | 50, 16 | 0, 17 | -166.59809678640224 18 | ], 19 | "coordinates": [ 20 | 144, 21 | 479 22 | ] 23 | }, 24 | { 25 | "position": [ 26 | 0, 27 | 50, 28 | -166.59809678640224 29 | ], 30 | "coordinates": [ 31 | 374, 32 | 224 33 | ] 34 | }, 35 | { 36 | "position": [ 37 | -50, 38 | 0, 39 | -166.59809678640224 40 | ], 41 | "coordinates": [ 42 | 626, 43 | 483 44 | ] 45 | }, 46 | { 47 | "position": [ 48 | 0, 49 | -50, 50 | -166.59809678640224 51 | ], 52 | "coordinates": [ 53 | 365, 54 | 704 55 | ] 56 | }, 57 | { 58 | "position": [ 59 | 50, 60 | 50, 61 | -158.57046689960953 62 | ], 63 | "coordinates": [ 64 | 119, 65 | 235 66 | ] 67 | }, 68 | { 69 | "position": [ 70 | 50, 71 | -50, 72 | -162.53472857209977 73 | ], 74 | "coordinates": [ 75 | 162, 76 | 710 77 | ] 78 | }, 79 | { 80 | "position": [ 81 | -50, 82 | 50, 83 | -158.57046689960953 84 | ], 85 | "coordinates": [ 86 | 632, 87 | 238 88 | ] 89 | }, 90 | { 91 | "position": [ 92 | -50, 93 | -50, 94 | -162.53472857209977 95 | ], 96 | "coordinates": [ 97 | 594, 98 | 707 99 | ] 100 | } 101 | ] -------------------------------------------------------------------------------- /bots/bitbeambot/server.js: -------------------------------------------------------------------------------- 1 | // Copyright 2012 2 | // Chris Williams, Jason Huggins 3 | // MIT License 4 | var application_root = __dirname 5 | , express = require("express") 6 | , path = require("path") 7 | , five = require("johnny-five") 8 | , kinematics = require("./kinematics") 9 | , fs = require("fs") 10 | , ArgumentParser = require('argparse').ArgumentParser 11 | , sleep = require('sleep').sleep 12 | , calibration = { isSet: false }; 13 | 14 | // install sylvester 15 | eval(fs.readFileSync(path.resolve(__dirname, "../../lib/sylvester.js"), "utf8")); 16 | 17 | // parse arguments 18 | var parser = new ArgumentParser({ 19 | version: '0.0.1', 20 | addHelp:true, 21 | description: 'Robot Calibration Script' 22 | }); 23 | parser.addArgument( 24 | [ '-c', '--calibration'], { 25 | help: 'file to load calibration data from' 26 | }); 27 | parser.addArgument( 28 | ['-p', '--port'] , { 29 | defaultValue: 4242 30 | , required: false 31 | , type: 'int' 32 | , example: "4242" 33 | , help: 'port to listen on' 34 | }); 35 | /* 36 | parser.addArgument( 37 | ['-a', '--address'], { 38 | defaultValue: '127.0.0.1' 39 | , required: false 40 | , example: "127.0.0.1" 41 | , help: 'IP Address to listen on' 42 | }); 43 | */ 44 | var args = parser.parseArgs(); 45 | 46 | // fire up the robot 47 | var board = new five.Board({ debug: false}); 48 | board.on("ready", function() { 49 | var servo1 = five.Servo({pin: 9}); 50 | var servo2 = five.Servo({pin: 10}); 51 | var servo3 = five.Servo({pin: 11}); 52 | 53 | board.repl.inject({ 54 | servo1: servo1, s1: servo1, 55 | servo2: servo2, s2: servo2, 56 | servo3: servo3, s3: servo3 57 | }); 58 | 59 | var max = 48; 60 | var min = 28; 61 | var range = max - min; 62 | 63 | servo1.move(min); 64 | servo2.move(min); 65 | servo3.move(min); 66 | servo1Angle = min; 67 | servo2Angle = min; 68 | servo3Angle = min; 69 | servo1.on("error", function () { console.log(arguments); }); 70 | servo2.on("error", function () { console.log(arguments); }); 71 | servo3.on("error", function () { console.log(arguments); }); 72 | 73 | // Load Calibration Data 74 | if (fs.existsSync(args.calibration)) { 75 | var calData = eval(fs.readFileSync(args.calibration, "utf8")); 76 | calibration.center = calData[0]; 77 | var x1 = calData[1]; 78 | var y1 = calData[2]; 79 | var xScale = 50.0; 80 | var yScale = 50.0; 81 | calibration.xNormal = [(x1.coordinates[0] - calibration.center.coordinates[0]) / xScale, (x1.coordinates[1] - calibration.center.coordinates[1]) / xScale]; 82 | calibration.yNormal = [(y1.coordinates[0] - calibration.center.coordinates[0]) / yScale, (y1.coordinates[1] - calibration.center.coordinates[1]) / yScale]; 83 | calibration.matrix = $M ([[calibration.xNormal[0], calibration.xNormal[1]],[calibration.yNormal[0], calibration.yNormal[1]]]); 84 | calibration.isSet = true; 85 | } 86 | 87 | getPositionForAngles = function(t1,t2,t3) { 88 | var points = kinematics.delta_calcForward(t1,t2,t3); 89 | return [points[1], points[2], points[3]]; 90 | }; 91 | 92 | getAnglesForPosition = function(x,y,z) { 93 | var angles = kinematics.delta_calcInverse(x,y,z); 94 | return [angles[1], angles[2], angles[3]]; 95 | }; 96 | 97 | setAngles = function(t1,t2,t3) { 98 | console.log("Setting Angles:" + [t1,t2,t3]); 99 | servo1.move(t1); 100 | servo2.move(t2); 101 | servo3.move(t3); 102 | servo1Angle = t1; 103 | servo2Angle = t2; 104 | servo3Angle = t3; 105 | }; 106 | 107 | currentAngles = function() { 108 | return [servo1Angle, servo2Angle, servo3Angle]; 109 | }; 110 | 111 | setPosition = function(x,y,z) { 112 | console.log("Setting Position:" + [x,y,z]); 113 | var angles = kinematics.delta_calcInverse(x,y,z); 114 | setAngles(angles[1], angles[2], angles[3]); 115 | }; 116 | 117 | currentPosition = function() { 118 | return getPositionForAngles(servo1Angle,servo2Angle,servo3Angle); 119 | }; 120 | 121 | convertCoordinatesToPosition = function(x,y) { 122 | var invCalMatrix = calibration.matrix.inverse(); 123 | var deltaX = x - calibration.center.coordinates[0]; 124 | var deltaY = y - calibration.center.coordinates[1]; 125 | var coordMatrix = $M([[deltaX,deltaY]]); 126 | return coordMatrix.multiply(invCalMatrix).elements[0]; 127 | }; 128 | 129 | convertPositionToCoordinates = function(x,y) { 130 | var posMatrix = $M([[x,y]]); 131 | return posMatrix.multiply(calibration.matrix).elements[0]; 132 | }; 133 | 134 | // launch rest server 135 | var app = express(); 136 | 137 | app.configure(function () { 138 | app.use(express.bodyParser()); 139 | app.use(express.methodOverride()); 140 | app.use(app.router); 141 | app.use(express.static(path.join(application_root, "public"))); 142 | app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); 143 | }); 144 | 145 | app.get('/status', function (req, res) { 146 | console.log("GET " + req.url + ": "); 147 | res.send('\"OK\"'); 148 | }); 149 | 150 | app.get('/reset', function (req, res) { 151 | console.log("GET " + req.url + ": "); 152 | setAngles(min, min, min); 153 | res.send(currentAngles()); 154 | }); 155 | 156 | app.post('/setAngles', function (req, res){ 157 | console.log("POST " + req.url + ": "); 158 | console.log(req.body); 159 | var theta1 = parseFloat(req.body.theta1); 160 | var theta2 = parseFloat(req.body.theta2); 161 | var theta3 = parseFloat(req.body.theta3); 162 | setAngles(theta1, theta2, theta3); 163 | return res.send("\"OK\""); 164 | }); 165 | 166 | app.post('/setPosition', function (req, res){ 167 | console.log("POST " + req.url + ": "); 168 | console.log(req.body); 169 | var x = parseFloat(req.body.x); 170 | var y = parseFloat(req.body.y); 171 | var z = parseFloat(req.body.z); 172 | setPosition(x, y, z); 173 | return res.send("\"OK\""); 174 | }); 175 | 176 | app.get('/angles', function (req, res){ 177 | console.log("GET " + req.url + ": "); 178 | return res.send(currentAngles()); 179 | }); 180 | 181 | app.get('/position', function (req, res){ 182 | console.log("GET " + req.url + ": "); 183 | return res.send(currentPosition()); 184 | }); 185 | 186 | app.get('/positionForCoordinates/x/:x/y/:y', function (req, res){ 187 | console.log("GET " + req.url + ": "); 188 | var x = parseFloat(req.params.x); 189 | var y = parseFloat(req.params.y); 190 | return res.send(convertCoordinatesToPosition(x,y)); 191 | }); 192 | 193 | app.get('/coordinatesForPosition/x/:x/y/:y', function (req, res){ 194 | console.log("GET " + req.url + ": "); 195 | var x = parseFloat(req.params.x); 196 | var y = parseFloat(req.params.y); 197 | return res.send(convertPositionToCoordinates(x,y)); 198 | }); 199 | 200 | app.get('/tap/x/:x/y/:y', function (req, res){ 201 | console.log("GET " + req.url + ": "); 202 | var x = parseFloat(req.params.x); 203 | var y = parseFloat(req.params.y); 204 | var pos = convertCoordinatesToPosition(x,y); 205 | setTimeout(function(res) { 206 | setPosition(pos[0], pos[1], calibration.center.position[2]*.8); 207 | setTimeout(function(res) { 208 | setPosition(pos[0], pos[1], calibration.center.position[2]*1.025); 209 | setTimeout(function(res) { 210 | setPosition(pos[0], pos[1], calibration.center.position[2]*.8); 211 | setTimeout(function(res) { 212 | setAngles(min, min, min); 213 | res.send("\"OK\""); 214 | }, 500, res); 215 | }, 500, res); 216 | }, 500, res); 217 | }, 500, res); 218 | }); 219 | 220 | app.listen(args.port); 221 | console.log("Robot listening on port " + args.port); 222 | 223 | }); 224 | -------------------------------------------------------------------------------- /bots/tapster/calibrate.js: -------------------------------------------------------------------------------- 1 | 2 | var fs = require('fs') 3 | , wdSync = require('wd-sync') 4 | , robot = require('./client').client("127.0.0.1","4242") 5 | , sleep = require('sleep').sleep 6 | , ArgumentParser = require('argparse').ArgumentParser; 7 | 8 | var client = wdSync.remote("127.0.0.1", 4723) 9 | , browser = client.browser 10 | , sync = client.sync; 11 | require('sylvester'); 12 | 13 | var parser = new ArgumentParser({ 14 | version: '0.0.1', 15 | addHelp:true, 16 | description: 'Bitbeambot Calibration Script' 17 | }); 18 | 19 | parser.addArgument( 20 | [ '-o', '--output' ], 21 | { 22 | help: 'file to save calibration data to' 23 | } 24 | ); 25 | 26 | var args = parser.parseArgs(); 27 | translationMatrix = $M([0]); 28 | 29 | sync( function() { 30 | 31 | // Reset Robot to Starting Position 32 | robot.reset(); 33 | 34 | console.log("server status:", browser.status()); 35 | browser.init( { browserName: 'iOS', app: 'Appium.RobotCalibration'} ); 36 | console.log("session capabilities:", browser.sessionCapabilities()); 37 | browser.setWaitTimeout(3000); 38 | console.log(browser.source()); 39 | 40 | var data = []; 41 | 42 | var getCoords = function() { 43 | try { 44 | // disabled due to bug with appium where the label is not updating 45 | // var value = browser.elementByTagName('staticText').text(); 46 | // if (value !== undefined) { 47 | // return eval(value.replace("(","[").replace(")", "]")); 48 | // } 49 | // return value; 50 | 51 | // hack to find value since element.text is not updated 52 | var coordRegex = /label[^\(,]+\((\d+\.*\d*),\s+(\d+\.*\d*)\)/ ; 53 | var pageSource = browser.source(); 54 | if (coordRegex.test(pageSource)) { 55 | var match = coordRegex.exec(pageSource); 56 | return [ parseFloat(match[1]), parseFloat(match[2])]; 57 | } 58 | else { 59 | return undefined; 60 | } 61 | } 62 | catch(err) { 63 | return undefined; 64 | } 65 | }; 66 | 67 | var findPoint = function(deltaX, deltaY) { 68 | 69 | // reset position 70 | robot.reset(); 71 | sleep(1); 72 | 73 | // move above the point 74 | var newPoint = [contactPoint.position[0] + deltaX, contactPoint.position[1] + deltaY, contactPoint.position[2] *.87]; 75 | robot.setPosition(newPoint[0], newPoint[1], newPoint[2]); 76 | 77 | // touch the device 78 | while (Math.abs(newPoint[2]) < Math.abs(contactPoint.position[2]*1.05)) { 79 | newPoint[2] = newPoint[2]*1.025; 80 | robot.setPosition(newPoint[0], newPoint[1], newPoint[2]); 81 | var location = getCoords(); 82 | if (location !== undefined && location.length >= 2) { 83 | var dataPoint = { position: newPoint, coordinates: location}; 84 | console.log(dataPoint); 85 | data.push(dataPoint); 86 | break; 87 | } 88 | } 89 | }; 90 | 91 | // Find Contact Point 92 | console.log("Finding Contact Point..."); 93 | var startingPosition = robot.position(); 94 | var currentPosition = [startingPosition[0], startingPosition[1], startingPosition[2]]; 95 | var coords = undefined; 96 | while (Math.abs(currentPosition[2] - startingPosition[2]) < 50) { 97 | currentPosition[2] = currentPosition[2]*1.025; 98 | robot.setPosition(currentPosition[0], currentPosition[1], currentPosition[2]); 99 | coords = getCoords(); 100 | if (coords !== undefined && coords.length >= 2) { 101 | break; 102 | } 103 | } 104 | console.log("Contact Point Found!"); 105 | var contactPoint = { position: robot.position(), coordinates: coords }; 106 | console.log(contactPoint); 107 | data.push(contactPoint); 108 | robot.reset(); 109 | sleep(.5); 110 | 111 | var newPoint; 112 | console.log("Determining Directions"); 113 | 114 | console.log("+x direction"); 115 | findPoint(20,20); 116 | console.log("+y direction"); 117 | findPoint(-10,-15); 118 | data.push(startingPosition[2]); 119 | data.push(contactPoint.position[2]); 120 | //console.log("-x direction"); 121 | //findPoint(-50,0); 122 | //console.log("-y direction"); 123 | //findPoint(0,-50); 124 | /* 125 | console.log("x,y corner"); 126 | findPoint(50,50); 127 | console.log("x,-y corner"); 128 | findPoint(50,-50); 129 | console.log("-x,y corner"); 130 | findPoint(-50,50); 131 | console.log("-x,-y corner"); 132 | findPoint(-50,-50); 133 | */ 134 | console.log("FINAL DATA"); 135 | console.log(data); 136 | 137 | robot.reset(); 138 | browser.quit(); 139 | 140 | fs.writeFile(args.output, JSON.stringify(data, null, 2), function(err) { 141 | if(err) { 142 | console.log(err); 143 | } else { 144 | console.log("Calibration Data Saved!"); 145 | } 146 | }); 147 | 148 | }); -------------------------------------------------------------------------------- /bots/tapster/client.js: -------------------------------------------------------------------------------- 1 | var request = require('request') 2 | , XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; 3 | 4 | exports.client = function(address, port) { 5 | 6 | return { 7 | address : address, 8 | port : port, 9 | url : function(uri) { 10 | return 'http://' + address + ":" + port + uri; 11 | }, 12 | angles : function() { 13 | var xmlHttp = new XMLHttpRequest(); 14 | xmlHttp.open( "GET", this.url('/angles'), false ); 15 | xmlHttp.send( null ); 16 | return eval(xmlHttp.responseText); 17 | }, 18 | setAngles : function(theta1, theta2, theta3) { 19 | request.post(this.url('/setAngles'), {form:{theta1:theta1, theta2:theta2, theta3:theta3}}); 20 | }, 21 | position : function() { 22 | var xmlHttp = new XMLHttpRequest(); 23 | xmlHttp.open( "GET", this.url('/position'), false ); 24 | xmlHttp.send( null ); 25 | return eval(xmlHttp.responseText); 26 | }, 27 | setPosition : function(x, y, z) { 28 | request.post(this.url('/setPosition'), {form:{x:x, y:y, z:z}}); 29 | }, 30 | positionForCoordinates : function(x,y) { 31 | var xmlHttp = new XMLHttpRequest(); 32 | xmlHttp.open( "GET", this.url('/positionForCoordinates/x/' + x + "/y/" + y), false ); 33 | xmlHttp.send( null ); 34 | return eval(xmlHttp.responseText); 35 | }, 36 | coordinatesForPosition : function(x,y) { 37 | var xmlHttp = new XMLHttpRequest(); 38 | xmlHttp.open( "GET", this.url('/coordinatesForPosition/x/' + x + "/y/" + y), false ); 39 | xmlHttp.send( null ); 40 | return eval(xmlHttp.responseText); 41 | }, 42 | reset : function() { 43 | var xmlHttp = new XMLHttpRequest(); 44 | xmlHttp.open( "GET", this.url('/reset'), false ); 45 | xmlHttp.send( null ); 46 | return eval(xmlHttp.responseText); 47 | }, 48 | tap : function(x,y) { 49 | var xmlHttp = new XMLHttpRequest(); 50 | xmlHttp.open( "GET", this.url('/tap/x/' + x + "/y/" + y), false ); 51 | xmlHttp.send( null ); 52 | return xmlHttp.responseText; 53 | } 54 | }; 55 | 56 | }; -------------------------------------------------------------------------------- /bots/tapster/kinematics.js: -------------------------------------------------------------------------------- 1 | // Original code from 2 | // http://forums.trossenrobotics.com/tutorials/introduction-129/delta-robot-kinematics-3276/ 3 | 4 | (function(exports) { 5 | 6 | // Specific geometry for bitbeambot: 7 | // http://flic.kr/p/cYaQah 8 | var e = 83.138; 9 | var f = 103.9; 10 | var re = 8 * 18; 11 | var rf = 8 * 7; 12 | 13 | exports.updateSize = function(parameters) { 14 | e = parameters[0]; 15 | f = parameters[1]; 16 | re = parameters[2]; 17 | rf = parameters[3]; 18 | exports.e = e; 19 | exports.f = f; 20 | exports.re = re; 21 | exports.rf = rf; 22 | }; 23 | 24 | exports.getSize = function() { 25 | return new Array(e, f, re, rf); 26 | }; 27 | 28 | // Trigonometric constants 29 | var s = 165 * 2; 30 | var sqrt3 = Math.sqrt(3.0); 31 | var pi = 3.141592653; 32 | var sin120 = sqrt3 / 2.0; 33 | var cos120 = -0.5; 34 | var tan60 = sqrt3; 35 | var sin30 = 0.5; 36 | var tan30 = 1.0 / sqrt3; 37 | 38 | // Forward kinematics: (theta1, theta2, theta3) -> (x0, y0, z0) 39 | // Returned {error code,theta1,theta2,theta3} 40 | exports.delta_calcForward = function(theta1, theta2, theta3) { 41 | var x0 = 0.0; 42 | var y0 = 0.0; 43 | var z0 = 0.0; 44 | 45 | var t = (f - e) * tan30 / 2.0; 46 | var dtr = pi / 180.0; 47 | 48 | theta1 *= dtr; 49 | theta2 *= dtr; 50 | theta3 *= dtr; 51 | 52 | var y1 = -(t + rf * Math.cos(theta1)); 53 | var z1 = -rf * Math.sin(theta1); 54 | 55 | var y2 = (t + rf * Math.cos(theta2)) * sin30; 56 | var x2 = y2 * tan60; 57 | var z2 = -rf * Math.sin(theta2); 58 | 59 | var y3 = (t + rf * Math.cos(theta3)) * sin30; 60 | var x3 = -y3 * tan60; 61 | var z3 = -rf * Math.sin(theta3); 62 | 63 | var dnm = (y2 - y1) * x3 - (y3 - y1) * x2; 64 | 65 | var w1 = y1 * y1 + z1 * z1; 66 | var w2 = x2 * x2 + y2 * y2 + z2 * z2; 67 | var w3 = x3 * x3 + y3 * y3 + z3 * z3; 68 | 69 | // x = (a1*z + b1)/dnm 70 | var a1 = (z2 - z1) * (y3 - y1) - (z3 - z1) * (y2 - y1); 71 | var b1 = -((w2 - w1) * (y3 - y1) - (w3 - w1) * (y2 - y1)) / 2.0; 72 | 73 | // y = (a2*z + b2)/dnm; 74 | var a2 = -(z2 - z1) * x3 + (z3 - z1) * x2; 75 | var b2 = ((w2 - w1) * x3 - (w3 - w1) * x2) / 2.0; 76 | 77 | // a*z^2 + b*z + c = 0 78 | var a = a1 * a1 + a2 * a2 + dnm * dnm; 79 | var b = 2.0 * (a1 * b1 + a2 * (b2 - y1 * dnm) - z1 * dnm * dnm); 80 | var c = (b2 - y1 * dnm) * (b2 - y1 * dnm) + b1 * b1 + dnm * dnm * (z1 * z1 - re * re); 81 | 82 | // discriminant 83 | var d = b * b - 4.0 * a * c; 84 | if (d < 0.0) { 85 | return new Array(1, 0, 0, 0); // non-existing povar. return error,x,y,z 86 | } 87 | 88 | z0 = -0.5 * (b + Math.sqrt(d)) / a; 89 | x0 = (a1 * z0 + b1) / dnm; 90 | y0 = (a2 * z0 + b2) / dnm; 91 | 92 | return new Array(0, x0, y0, z0); 93 | }; 94 | 95 | // Inverse kinematics 96 | // Helper functions, calculates angle theta1 (for YZ-pane) 97 | var delta_calcAngleYZ = function(x0, y0, z0) { 98 | var y1 = -0.5 * 0.57735 * f; // f/2 * tg 30 99 | y0 -= 0.5 * 0.57735 * e; // shift center to edge 100 | // z = a + b*y 101 | var a = (x0 * x0 + y0 * y0 + z0 * z0 + rf * rf - re * re - y1 * y1) / (2.0 * z0); 102 | var b = (y1 - y0) / z0; 103 | 104 | // discriminant 105 | var d = -(a + b * y1) * (a + b * y1) + rf * (b * b * rf + rf); 106 | if (d < 0) { 107 | return new Array(1, 0); // non-existing povar. return error, theta 108 | } 109 | 110 | var yj = (y1 - a * b - Math.sqrt(d)) / (b * b + 1); // choosing outer povar 111 | var zj = a + b * yj; 112 | var theta = Math.atan(-zj / (y1 - yj)) * 180.0 / pi + ((yj > y1) ? 180.0 : 0.0); 113 | 114 | return new Array(0, theta); // return error, theta 115 | }; 116 | 117 | exports.delta_calcInverse = function(x0, y0, z0) { 118 | var theta1 = 0; 119 | var theta2 = 0; 120 | var theta3 = 0; 121 | var status = delta_calcAngleYZ(x0, y0, z0); 122 | 123 | if (status[0] === 0) { 124 | theta1 = status[1]; 125 | status = delta_calcAngleYZ(x0 * cos120 + y0 * sin120, y0 * cos120 - x0 * sin120, z0, theta2); 126 | } 127 | if (status[0] === 0) { 128 | theta2 = status[1]; 129 | status = delta_calcAngleYZ(x0 * cos120 - y0 * sin120, y0 * cos120 + x0 * sin120, z0, theta3); 130 | } 131 | theta3 = status[1]; 132 | 133 | return new Array(status[0], theta1, theta2, theta3); 134 | }; 135 | }(typeof exports === 'undefined' ? this.ik = {} : exports)); 136 | -------------------------------------------------------------------------------- /bots/tapster/sample-calibration.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "position": [ 4 | 0, 5 | 0, 6 | -169.25108293361723 7 | ], 8 | "coordinates": [ 9 | 377, 10 | 479 11 | ] 12 | }, 13 | { 14 | "position": [ 15 | 50, 16 | 0, 17 | -166.59809678640224 18 | ], 19 | "coordinates": [ 20 | 144, 21 | 479 22 | ] 23 | }, 24 | { 25 | "position": [ 26 | 0, 27 | 50, 28 | -166.59809678640224 29 | ], 30 | "coordinates": [ 31 | 374, 32 | 224 33 | ] 34 | }, 35 | { 36 | "position": [ 37 | -50, 38 | 0, 39 | -166.59809678640224 40 | ], 41 | "coordinates": [ 42 | 626, 43 | 483 44 | ] 45 | }, 46 | { 47 | "position": [ 48 | 0, 49 | -50, 50 | -166.59809678640224 51 | ], 52 | "coordinates": [ 53 | 365, 54 | 704 55 | ] 56 | }, 57 | { 58 | "position": [ 59 | 50, 60 | 50, 61 | -158.57046689960953 62 | ], 63 | "coordinates": [ 64 | 119, 65 | 235 66 | ] 67 | }, 68 | { 69 | "position": [ 70 | 50, 71 | -50, 72 | -162.53472857209977 73 | ], 74 | "coordinates": [ 75 | 162, 76 | 710 77 | ] 78 | }, 79 | { 80 | "position": [ 81 | -50, 82 | 50, 83 | -158.57046689960953 84 | ], 85 | "coordinates": [ 86 | 632, 87 | 238 88 | ] 89 | }, 90 | { 91 | "position": [ 92 | -50, 93 | -50, 94 | -162.53472857209977 95 | ], 96 | "coordinates": [ 97 | 594, 98 | 707 99 | ] 100 | } 101 | ] -------------------------------------------------------------------------------- /bots/tapster/server.js: -------------------------------------------------------------------------------- 1 | // Copyright 2012 2 | // Chris Williams, Jason Huggins 3 | // MIT License 4 | var application_root = __dirname 5 | , express = require("express") 6 | , path = require("path") 7 | , five = require("johnny-five") 8 | , kinematics = require("./kinematics") 9 | , fs = require("fs") 10 | , ArgumentParser = require('argparse').ArgumentParser 11 | , sleep = require('sleep').sleep 12 | , calibration = { isSet: false }; 13 | 14 | // install sylvester 15 | eval(fs.readFileSync(path.resolve(__dirname, "../../lib/sylvester.js"), "utf8")); 16 | 17 | // parse arguments 18 | var parser = new ArgumentParser({ 19 | version: '0.0.1', 20 | addHelp:true, 21 | description: 'Robot Calibration Script' 22 | }); 23 | parser.addArgument( 24 | [ '-c', '--calibration'], { 25 | help: 'file to load calibration data from' 26 | }); 27 | parser.addArgument( 28 | ['-p', '--port'] , { 29 | defaultValue: 4242 30 | , required: false 31 | , type: 'int' 32 | , example: "4242" 33 | , help: 'port to listen on' 34 | }); 35 | /* 36 | parser.addArgument( 37 | ['-a', '--address'], { 38 | defaultValue: '127.0.0.1' 39 | , required: false 40 | , example: "127.0.0.1" 41 | , help: 'IP Address to listen on' 42 | }); 43 | */ 44 | var args = parser.parseArgs(); 45 | 46 | // fire up the robot 47 | var board = new five.Board({ debug: false}); 48 | board.on("ready", function() { 49 | var servo1 = five.Servo({pin: 9}); 50 | var servo2 = five.Servo({pin: 10}); 51 | var servo3 = five.Servo({pin: 11}); 52 | 53 | board.repl.inject({ 54 | servo1: servo1, s1: servo1, 55 | servo2: servo2, s2: servo2, 56 | servo3: servo3, s3: servo3 57 | }); 58 | 59 | var max = 45; 60 | var min = 0; 61 | var range = max - min; 62 | 63 | servo1.move(min); 64 | servo2.move(min); 65 | servo3.move(min); 66 | servo1Angle = min; 67 | servo2Angle = min; 68 | servo3Angle = min; 69 | servo1.on("error", function () { console.log(arguments); }); 70 | servo2.on("error", function () { console.log(arguments); }); 71 | servo3.on("error", function () { console.log(arguments); }); 72 | 73 | // Load Calibration Data 74 | if (fs.existsSync(args.calibration)) { 75 | var data = eval(fs.readFileSync(args.calibration, "utf8")); 76 | var b0x = data[1].position[0], 77 | b0y = data[1].position[1], 78 | b1x = data[2].position[0], 79 | b1y = data[2].position[1]; 80 | var d0x = data[1].coordinates[0], 81 | d0y = data[1].coordinates[1], 82 | d1x = data[2].coordinates[0], 83 | d1y = data[2].coordinates[1]; 84 | 85 | var M = $M([ 86 | [d0x, d0y, 1, 0], 87 | [-d0y, d0x, 0, 1], 88 | [d1x, d1y, 1, 0], 89 | [-d1y, d1x, 0, 1] 90 | ]); 91 | var u = $M([ 92 | [b0x], 93 | [b0y], 94 | [b1x], 95 | [b1y] 96 | ]); 97 | var MI = M.inverse(); 98 | translationMatrix = MI.multiply(u); 99 | offZ = data[3]; 100 | onZ = data[4]; 101 | 102 | } 103 | 104 | getPositionForAngles = function(t1,t2,t3) { 105 | var points = kinematics.delta_calcForward(t1,t2,t3); 106 | return [points[1], points[2], points[3]]; 107 | }; 108 | 109 | getAnglesForPosition = function(x,y,z) { 110 | var angles = kinematics.delta_calcInverse(x,y,z); 111 | return [angles[1], angles[2], angles[3]]; 112 | }; 113 | 114 | setAngles = function(t1,t2,t3) { 115 | console.log("Setting Angles:" + [t1,t2,t3]); 116 | servo1.move(t1); 117 | servo2.move(t2); 118 | servo3.move(t3); 119 | servo1Angle = t1; 120 | servo2Angle = t2; 121 | servo3Angle = t3; 122 | }; 123 | 124 | currentAngles = function() { 125 | return [servo1Angle, servo2Angle, servo3Angle]; 126 | }; 127 | 128 | setPosition = function(x,y,z) { 129 | console.log("Setting Position:" + [x,y,z]); 130 | var angles = kinematics.delta_calcInverse(x,y,z); 131 | setAngles(angles[1], angles[2], angles[3]); 132 | }; 133 | 134 | currentPosition = function() { 135 | return getPositionForAngles(servo1Angle,servo2Angle,servo3Angle); 136 | }; 137 | 138 | convertCoordinatesToPosition = function(x,y) { 139 | var a = translationMatrix.elements[0][0], 140 | b = translationMatrix.elements[1][0], 141 | c = translationMatrix.elements[2][0], 142 | d = translationMatrix.elements[3][0]; 143 | 144 | yprime = a * x + b * y + c; 145 | xprime = b * x - a * y + d; 146 | return [xprime, yprime] 147 | }; 148 | 149 | // launch rest server 150 | var app = express(); 151 | 152 | app.configure(function () { 153 | app.use(express.bodyParser()); 154 | app.use(express.methodOverride()); 155 | app.use(app.router); 156 | app.use(express.static(path.join(application_root, "public"))); 157 | app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); 158 | }); 159 | 160 | app.get('/status', function (req, res) { 161 | console.log("GET " + req.url + ": "); 162 | res.send('\"OK\"'); 163 | }); 164 | 165 | app.get('/reset', function (req, res) { 166 | console.log("GET " + req.url + ": "); 167 | setAngles(min, min, min); 168 | res.send(currentAngles()); 169 | }); 170 | 171 | app.post('/setAngles', function (req, res){ 172 | console.log("POST " + req.url + ": "); 173 | console.log(req.body); 174 | var theta1 = parseFloat(req.body.theta1); 175 | var theta2 = parseFloat(req.body.theta2); 176 | var theta3 = parseFloat(req.body.theta3); 177 | setAngles(theta1, theta2, theta3); 178 | return res.send("\"OK\""); 179 | }); 180 | 181 | app.post('/setPosition', function (req, res){ 182 | console.log("POST " + req.url + ": "); 183 | console.log(req.body); 184 | var x = parseFloat(req.body.x); 185 | var y = parseFloat(req.body.y); 186 | var z = parseFloat(req.body.z); 187 | setPosition(x, y, z); 188 | return res.send("\"OK\""); 189 | }); 190 | 191 | app.get('/angles', function (req, res){ 192 | console.log("GET " + req.url + ": "); 193 | return res.send(currentAngles()); 194 | }); 195 | 196 | app.get('/position', function (req, res){ 197 | console.log("GET " + req.url + ": "); 198 | return res.send(currentPosition()); 199 | }); 200 | 201 | app.get('/positionForCoordinates/x/:x/y/:y', function (req, res){ 202 | console.log("GET " + req.url + ": "); 203 | var x = parseFloat(req.params.x); 204 | var y = parseFloat(req.params.y); 205 | return res.send(convertCoordinatesToPosition(x,y)); 206 | }); 207 | 208 | app.get('/tap/x/:x/y/:y', function (req, res){ 209 | console.log("GET " + req.url + ": "); 210 | var x = parseFloat(req.params.x); 211 | var y = parseFloat(req.params.y); 212 | var pos = convertCoordinatesToPosition(x,y); 213 | setTimeout(function(res) { 214 | setPosition(pos[0], pos[1], offZ); 215 | setTimeout(function(res) { 216 | setPosition(pos[0], pos[1], onZ); 217 | setTimeout(function(res) { 218 | setPosition(pos[0], pos[1], offZ); 219 | setTimeout(function(res) { 220 | setAngles(min, min, min); 221 | res.send("\"OK\""); 222 | }, 500, res); 223 | }, 500, res); 224 | }, 500, res); 225 | }, 500, res); 226 | }); 227 | 228 | app.listen(args.port); 229 | console.log("Robot listening on port " + args.port); 230 | 231 | }); 232 | -------------------------------------------------------------------------------- /lib/sylvester.js: -------------------------------------------------------------------------------- 1 | // === Sylvester === 2 | // Vector and Matrix mathematics modules for JavaScript 3 | // Copyright (c) 2007 James Coglan 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining 6 | // a copy of this software and associated documentation files (the "Software"), 7 | // to deal in the Software without restriction, including without limitation 8 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, 9 | // and/or sell copies of the Software, and to permit persons to whom the 10 | // Software is furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included 13 | // in all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 16 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 21 | // DEALINGS IN THE SOFTWARE. 22 | 23 | var Sylvester = { 24 | version: '0.1.3', 25 | precision: 1e-6 26 | }; 27 | 28 | function Vector() {} 29 | Vector.prototype = { 30 | 31 | // Returns element i of the vector 32 | e: function(i) { 33 | return (i < 1 || i > this.elements.length) ? null : this.elements[i-1]; 34 | }, 35 | 36 | // Returns the number of elements the vector has 37 | dimensions: function() { 38 | return this.elements.length; 39 | }, 40 | 41 | // Returns the modulus ('length') of the vector 42 | modulus: function() { 43 | return Math.sqrt(this.dot(this)); 44 | }, 45 | 46 | // Returns true iff the vector is equal to the argument 47 | eql: function(vector) { 48 | var n = this.elements.length; 49 | var V = vector.elements || vector; 50 | if (n != V.length) { return false; } 51 | do { 52 | if (Math.abs(this.elements[n-1] - V[n-1]) > Sylvester.precision) { return false; } 53 | } while (--n); 54 | return true; 55 | }, 56 | 57 | // Returns a copy of the vector 58 | dup: function() { 59 | return Vector.create(this.elements); 60 | }, 61 | 62 | // Maps the vector to another vector according to the given function 63 | map: function(fn) { 64 | var elements = []; 65 | this.each(function(x, i) { 66 | elements.push(fn(x, i)); 67 | }); 68 | return Vector.create(elements); 69 | }, 70 | 71 | // Calls the iterator for each element of the vector in turn 72 | each: function(fn) { 73 | var n = this.elements.length, k = n, i; 74 | do { i = k - n; 75 | fn(this.elements[i], i+1); 76 | } while (--n); 77 | }, 78 | 79 | // Returns a new vector created by normalizing the receiver 80 | toUnitVector: function() { 81 | var r = this.modulus(); 82 | if (r === 0) { return this.dup(); } 83 | return this.map(function(x) { return x/r; }); 84 | }, 85 | 86 | // Returns the angle between the vector and the argument (also a vector) 87 | angleFrom: function(vector) { 88 | var V = vector.elements || vector; 89 | var n = this.elements.length, k = n, i; 90 | if (n != V.length) { return null; } 91 | var dot = 0, mod1 = 0, mod2 = 0; 92 | // Work things out in parallel to save time 93 | this.each(function(x, i) { 94 | dot += x * V[i-1]; 95 | mod1 += x * x; 96 | mod2 += V[i-1] * V[i-1]; 97 | }); 98 | mod1 = Math.sqrt(mod1); mod2 = Math.sqrt(mod2); 99 | if (mod1*mod2 === 0) { return null; } 100 | var theta = dot / (mod1*mod2); 101 | if (theta < -1) { theta = -1; } 102 | if (theta > 1) { theta = 1; } 103 | return Math.acos(theta); 104 | }, 105 | 106 | // Returns true iff the vector is parallel to the argument 107 | isParallelTo: function(vector) { 108 | var angle = this.angleFrom(vector); 109 | return (angle === null) ? null : (angle <= Sylvester.precision); 110 | }, 111 | 112 | // Returns true iff the vector is antiparallel to the argument 113 | isAntiparallelTo: function(vector) { 114 | var angle = this.angleFrom(vector); 115 | return (angle === null) ? null : (Math.abs(angle - Math.PI) <= Sylvester.precision); 116 | }, 117 | 118 | // Returns true iff the vector is perpendicular to the argument 119 | isPerpendicularTo: function(vector) { 120 | var dot = this.dot(vector); 121 | return (dot === null) ? null : (Math.abs(dot) <= Sylvester.precision); 122 | }, 123 | 124 | // Returns the result of adding the argument to the vector 125 | add: function(vector) { 126 | var V = vector.elements || vector; 127 | if (this.elements.length != V.length) { return null; } 128 | return this.map(function(x, i) { return x + V[i-1]; }); 129 | }, 130 | 131 | // Returns the result of subtracting the argument from the vector 132 | subtract: function(vector) { 133 | var V = vector.elements || vector; 134 | if (this.elements.length != V.length) { return null; } 135 | return this.map(function(x, i) { return x - V[i-1]; }); 136 | }, 137 | 138 | // Returns the result of multiplying the elements of the vector by the argument 139 | multiply: function(k) { 140 | return this.map(function(x) { return x*k; }); 141 | }, 142 | 143 | x: function(k) { return this.multiply(k); }, 144 | 145 | // Returns the scalar product of the vector with the argument 146 | // Both vectors must have equal dimensionality 147 | dot: function(vector) { 148 | var V = vector.elements || vector; 149 | var i, product = 0, n = this.elements.length; 150 | if (n != V.length) { return null; } 151 | do { product += this.elements[n-1] * V[n-1]; } while (--n); 152 | return product; 153 | }, 154 | 155 | // Returns the vector product of the vector with the argument 156 | // Both vectors must have dimensionality 3 157 | cross: function(vector) { 158 | var B = vector.elements || vector; 159 | if (this.elements.length != 3 || B.length != 3) { return null; } 160 | var A = this.elements; 161 | return Vector.create([ 162 | (A[1] * B[2]) - (A[2] * B[1]), 163 | (A[2] * B[0]) - (A[0] * B[2]), 164 | (A[0] * B[1]) - (A[1] * B[0]) 165 | ]); 166 | }, 167 | 168 | // Returns the (absolute) largest element of the vector 169 | max: function() { 170 | var m = 0, n = this.elements.length, k = n, i; 171 | do { i = k - n; 172 | if (Math.abs(this.elements[i]) > Math.abs(m)) { m = this.elements[i]; } 173 | } while (--n); 174 | return m; 175 | }, 176 | 177 | // Returns the index of the first match found 178 | indexOf: function(x) { 179 | var index = null, n = this.elements.length, k = n, i; 180 | do { i = k - n; 181 | if (index === null && this.elements[i] == x) { 182 | index = i + 1; 183 | } 184 | } while (--n); 185 | return index; 186 | }, 187 | 188 | // Returns a diagonal matrix with the vector's elements as its diagonal elements 189 | toDiagonalMatrix: function() { 190 | return Matrix.Diagonal(this.elements); 191 | }, 192 | 193 | // Returns the result of rounding the elements of the vector 194 | round: function() { 195 | return this.map(function(x) { return Math.round(x); }); 196 | }, 197 | 198 | // Returns a copy of the vector with elements set to the given value if they 199 | // differ from it by less than Sylvester.precision 200 | snapTo: function(x) { 201 | return this.map(function(y) { 202 | return (Math.abs(y - x) <= Sylvester.precision) ? x : y; 203 | }); 204 | }, 205 | 206 | // Returns the vector's distance from the argument, when considered as a point in space 207 | distanceFrom: function(obj) { 208 | if (obj.anchor) { return obj.distanceFrom(this); } 209 | var V = obj.elements || obj; 210 | if (V.length != this.elements.length) { return null; } 211 | var sum = 0, part; 212 | this.each(function(x, i) { 213 | part = x - V[i-1]; 214 | sum += part * part; 215 | }); 216 | return Math.sqrt(sum); 217 | }, 218 | 219 | // Returns true if the vector is point on the given line 220 | liesOn: function(line) { 221 | return line.contains(this); 222 | }, 223 | 224 | // Return true iff the vector is a point in the given plane 225 | liesIn: function(plane) { 226 | return plane.contains(this); 227 | }, 228 | 229 | // Rotates the vector about the given object. The object should be a 230 | // point if the vector is 2D, and a line if it is 3D. Be careful with line directions! 231 | rotate: function(t, obj) { 232 | var V, R, x, y, z; 233 | switch (this.elements.length) { 234 | case 2: 235 | V = obj.elements || obj; 236 | if (V.length != 2) { return null; } 237 | R = Matrix.Rotation(t).elements; 238 | x = this.elements[0] - V[0]; 239 | y = this.elements[1] - V[1]; 240 | return Vector.create([ 241 | V[0] + R[0][0] * x + R[0][1] * y, 242 | V[1] + R[1][0] * x + R[1][1] * y 243 | ]); 244 | break; 245 | case 3: 246 | if (!obj.direction) { return null; } 247 | var C = obj.pointClosestTo(this).elements; 248 | R = Matrix.Rotation(t, obj.direction).elements; 249 | x = this.elements[0] - C[0]; 250 | y = this.elements[1] - C[1]; 251 | z = this.elements[2] - C[2]; 252 | return Vector.create([ 253 | C[0] + R[0][0] * x + R[0][1] * y + R[0][2] * z, 254 | C[1] + R[1][0] * x + R[1][1] * y + R[1][2] * z, 255 | C[2] + R[2][0] * x + R[2][1] * y + R[2][2] * z 256 | ]); 257 | break; 258 | default: 259 | return null; 260 | } 261 | }, 262 | 263 | // Returns the result of reflecting the point in the given point, line or plane 264 | reflectionIn: function(obj) { 265 | if (obj.anchor) { 266 | // obj is a plane or line 267 | var P = this.elements.slice(); 268 | var C = obj.pointClosestTo(P).elements; 269 | return Vector.create([C[0] + (C[0] - P[0]), C[1] + (C[1] - P[1]), C[2] + (C[2] - (P[2] || 0))]); 270 | } else { 271 | // obj is a point 272 | var Q = obj.elements || obj; 273 | if (this.elements.length != Q.length) { return null; } 274 | return this.map(function(x, i) { return Q[i-1] + (Q[i-1] - x); }); 275 | } 276 | }, 277 | 278 | // Utility to make sure vectors are 3D. If they are 2D, a zero z-component is added 279 | to3D: function() { 280 | var V = this.dup(); 281 | switch (V.elements.length) { 282 | case 3: break; 283 | case 2: V.elements.push(0); break; 284 | default: return null; 285 | } 286 | return V; 287 | }, 288 | 289 | // Returns a string representation of the vector 290 | inspect: function() { 291 | return '[' + this.elements.join(', ') + ']'; 292 | }, 293 | 294 | // Set vector's elements from an array 295 | setElements: function(els) { 296 | this.elements = (els.elements || els).slice(); 297 | return this; 298 | } 299 | }; 300 | 301 | // Constructor function 302 | Vector.create = function(elements) { 303 | var V = new Vector(); 304 | return V.setElements(elements); 305 | }; 306 | 307 | // i, j, k unit vectors 308 | Vector.i = Vector.create([1,0,0]); 309 | Vector.j = Vector.create([0,1,0]); 310 | Vector.k = Vector.create([0,0,1]); 311 | 312 | // Random vector of size n 313 | Vector.Random = function(n) { 314 | var elements = []; 315 | do { elements.push(Math.random()); 316 | } while (--n); 317 | return Vector.create(elements); 318 | }; 319 | 320 | // Vector filled with zeros 321 | Vector.Zero = function(n) { 322 | var elements = []; 323 | do { elements.push(0); 324 | } while (--n); 325 | return Vector.create(elements); 326 | }; 327 | 328 | 329 | 330 | function Matrix() {} 331 | Matrix.prototype = { 332 | 333 | // Returns element (i,j) of the matrix 334 | e: function(i,j) { 335 | if (i < 1 || i > this.elements.length || j < 1 || j > this.elements[0].length) { return null; } 336 | return this.elements[i-1][j-1]; 337 | }, 338 | 339 | // Returns row k of the matrix as a vector 340 | row: function(i) { 341 | if (i > this.elements.length) { return null; } 342 | return Vector.create(this.elements[i-1]); 343 | }, 344 | 345 | // Returns column k of the matrix as a vector 346 | col: function(j) { 347 | if (j > this.elements[0].length) { return null; } 348 | var col = [], n = this.elements.length, k = n, i; 349 | do { i = k - n; 350 | col.push(this.elements[i][j-1]); 351 | } while (--n); 352 | return Vector.create(col); 353 | }, 354 | 355 | // Returns the number of rows/columns the matrix has 356 | dimensions: function() { 357 | return {rows: this.elements.length, cols: this.elements[0].length}; 358 | }, 359 | 360 | // Returns the number of rows in the matrix 361 | rows: function() { 362 | return this.elements.length; 363 | }, 364 | 365 | // Returns the number of columns in the matrix 366 | cols: function() { 367 | return this.elements[0].length; 368 | }, 369 | 370 | // Returns true iff the matrix is equal to the argument. You can supply 371 | // a vector as the argument, in which case the receiver must be a 372 | // one-column matrix equal to the vector. 373 | eql: function(matrix) { 374 | var M = matrix.elements || matrix; 375 | if (typeof(M[0][0]) == 'undefined') { M = Matrix.create(M).elements; } 376 | if (this.elements.length != M.length || 377 | this.elements[0].length != M[0].length) { return false; } 378 | var ni = this.elements.length, ki = ni, i, nj, kj = this.elements[0].length, j; 379 | do { i = ki - ni; 380 | nj = kj; 381 | do { j = kj - nj; 382 | if (Math.abs(this.elements[i][j] - M[i][j]) > Sylvester.precision) { return false; } 383 | } while (--nj); 384 | } while (--ni); 385 | return true; 386 | }, 387 | 388 | // Returns a copy of the matrix 389 | dup: function() { 390 | return Matrix.create(this.elements); 391 | }, 392 | 393 | // Maps the matrix to another matrix (of the same dimensions) according to the given function 394 | map: function(fn) { 395 | var els = [], ni = this.elements.length, ki = ni, i, nj, kj = this.elements[0].length, j; 396 | do { i = ki - ni; 397 | nj = kj; 398 | els[i] = []; 399 | do { j = kj - nj; 400 | els[i][j] = fn(this.elements[i][j], i + 1, j + 1); 401 | } while (--nj); 402 | } while (--ni); 403 | return Matrix.create(els); 404 | }, 405 | 406 | // Returns true iff the argument has the same dimensions as the matrix 407 | isSameSizeAs: function(matrix) { 408 | var M = matrix.elements || matrix; 409 | if (typeof(M[0][0]) == 'undefined') { M = Matrix.create(M).elements; } 410 | return (this.elements.length == M.length && 411 | this.elements[0].length == M[0].length); 412 | }, 413 | 414 | // Returns the result of adding the argument to the matrix 415 | add: function(matrix) { 416 | var M = matrix.elements || matrix; 417 | if (typeof(M[0][0]) == 'undefined') { M = Matrix.create(M).elements; } 418 | if (!this.isSameSizeAs(M)) { return null; } 419 | return this.map(function(x, i, j) { return x + M[i-1][j-1]; }); 420 | }, 421 | 422 | // Returns the result of subtracting the argument from the matrix 423 | subtract: function(matrix) { 424 | var M = matrix.elements || matrix; 425 | if (typeof(M[0][0]) == 'undefined') { M = Matrix.create(M).elements; } 426 | if (!this.isSameSizeAs(M)) { return null; } 427 | return this.map(function(x, i, j) { return x - M[i-1][j-1]; }); 428 | }, 429 | 430 | // Returns true iff the matrix can multiply the argument from the left 431 | canMultiplyFromLeft: function(matrix) { 432 | var M = matrix.elements || matrix; 433 | if (typeof(M[0][0]) == 'undefined') { M = Matrix.create(M).elements; } 434 | // this.columns should equal matrix.rows 435 | return (this.elements[0].length == M.length); 436 | }, 437 | 438 | // Returns the result of multiplying the matrix from the right by the argument. 439 | // If the argument is a scalar then just multiply all the elements. If the argument is 440 | // a vector, a vector is returned, which saves you having to remember calling 441 | // col(1) on the result. 442 | multiply: function(matrix) { 443 | if (!matrix.elements) { 444 | return this.map(function(x) { return x * matrix; }); 445 | } 446 | var returnVector = matrix.modulus ? true : false; 447 | var M = matrix.elements || matrix; 448 | if (typeof(M[0][0]) == 'undefined') { M = Matrix.create(M).elements; } 449 | if (!this.canMultiplyFromLeft(M)) { return null; } 450 | var ni = this.elements.length, ki = ni, i, nj, kj = M[0].length, j; 451 | var cols = this.elements[0].length, elements = [], sum, nc, c; 452 | do { i = ki - ni; 453 | elements[i] = []; 454 | nj = kj; 455 | do { j = kj - nj; 456 | sum = 0; 457 | nc = cols; 458 | do { c = cols - nc; 459 | sum += this.elements[i][c] * M[c][j]; 460 | } while (--nc); 461 | elements[i][j] = sum; 462 | } while (--nj); 463 | } while (--ni); 464 | var M = Matrix.create(elements); 465 | return returnVector ? M.col(1) : M; 466 | }, 467 | 468 | x: function(matrix) { return this.multiply(matrix); }, 469 | 470 | // Returns a submatrix taken from the matrix 471 | // Argument order is: start row, start col, nrows, ncols 472 | // Element selection wraps if the required index is outside the matrix's bounds, so you could 473 | // use this to perform row/column cycling or copy-augmenting. 474 | minor: function(a, b, c, d) { 475 | var elements = [], ni = c, i, nj, j; 476 | var rows = this.elements.length, cols = this.elements[0].length; 477 | do { i = c - ni; 478 | elements[i] = []; 479 | nj = d; 480 | do { j = d - nj; 481 | elements[i][j] = this.elements[(a+i-1)%rows][(b+j-1)%cols]; 482 | } while (--nj); 483 | } while (--ni); 484 | return Matrix.create(elements); 485 | }, 486 | 487 | // Returns the transpose of the matrix 488 | transpose: function() { 489 | var rows = this.elements.length, cols = this.elements[0].length; 490 | var elements = [], ni = cols, i, nj, j; 491 | do { i = cols - ni; 492 | elements[i] = []; 493 | nj = rows; 494 | do { j = rows - nj; 495 | elements[i][j] = this.elements[j][i]; 496 | } while (--nj); 497 | } while (--ni); 498 | return Matrix.create(elements); 499 | }, 500 | 501 | // Returns true iff the matrix is square 502 | isSquare: function() { 503 | return (this.elements.length == this.elements[0].length); 504 | }, 505 | 506 | // Returns the (absolute) largest element of the matrix 507 | max: function() { 508 | var m = 0, ni = this.elements.length, ki = ni, i, nj, kj = this.elements[0].length, j; 509 | do { i = ki - ni; 510 | nj = kj; 511 | do { j = kj - nj; 512 | if (Math.abs(this.elements[i][j]) > Math.abs(m)) { m = this.elements[i][j]; } 513 | } while (--nj); 514 | } while (--ni); 515 | return m; 516 | }, 517 | 518 | // Returns the indeces of the first match found by reading row-by-row from left to right 519 | indexOf: function(x) { 520 | var index = null, ni = this.elements.length, ki = ni, i, nj, kj = this.elements[0].length, j; 521 | do { i = ki - ni; 522 | nj = kj; 523 | do { j = kj - nj; 524 | if (this.elements[i][j] == x) { return {i: i+1, j: j+1}; } 525 | } while (--nj); 526 | } while (--ni); 527 | return null; 528 | }, 529 | 530 | // If the matrix is square, returns the diagonal elements as a vector. 531 | // Otherwise, returns null. 532 | diagonal: function() { 533 | if (!this.isSquare) { return null; } 534 | var els = [], n = this.elements.length, k = n, i; 535 | do { i = k - n; 536 | els.push(this.elements[i][i]); 537 | } while (--n); 538 | return Vector.create(els); 539 | }, 540 | 541 | // Make the matrix upper (right) triangular by Gaussian elimination. 542 | // This method only adds multiples of rows to other rows. No rows are 543 | // scaled up or switched, and the determinant is preserved. 544 | toRightTriangular: function() { 545 | var M = this.dup(), els; 546 | var n = this.elements.length, k = n, i, np, kp = this.elements[0].length, p; 547 | do { i = k - n; 548 | if (M.elements[i][i] == 0) { 549 | for (j = i + 1; j < k; j++) { 550 | if (M.elements[j][i] != 0) { 551 | els = []; np = kp; 552 | do { p = kp - np; 553 | els.push(M.elements[i][p] + M.elements[j][p]); 554 | } while (--np); 555 | M.elements[i] = els; 556 | break; 557 | } 558 | } 559 | } 560 | if (M.elements[i][i] != 0) { 561 | for (j = i + 1; j < k; j++) { 562 | var multiplier = M.elements[j][i] / M.elements[i][i]; 563 | els = []; np = kp; 564 | do { p = kp - np; 565 | // Elements with column numbers up to an including the number 566 | // of the row that we're subtracting can safely be set straight to 567 | // zero, since that's the point of this routine and it avoids having 568 | // to loop over and correct rounding errors later 569 | els.push(p <= i ? 0 : M.elements[j][p] - M.elements[i][p] * multiplier); 570 | } while (--np); 571 | M.elements[j] = els; 572 | } 573 | } 574 | } while (--n); 575 | return M; 576 | }, 577 | 578 | toUpperTriangular: function() { return this.toRightTriangular(); }, 579 | 580 | // Returns the determinant for square matrices 581 | determinant: function() { 582 | if (!this.isSquare()) { return null; } 583 | var M = this.toRightTriangular(); 584 | var det = M.elements[0][0], n = M.elements.length - 1, k = n, i; 585 | do { i = k - n + 1; 586 | det = det * M.elements[i][i]; 587 | } while (--n); 588 | return det; 589 | }, 590 | 591 | det: function() { return this.determinant(); }, 592 | 593 | // Returns true iff the matrix is singular 594 | isSingular: function() { 595 | return (this.isSquare() && this.determinant() === 0); 596 | }, 597 | 598 | // Returns the trace for square matrices 599 | trace: function() { 600 | if (!this.isSquare()) { return null; } 601 | var tr = this.elements[0][0], n = this.elements.length - 1, k = n, i; 602 | do { i = k - n + 1; 603 | tr += this.elements[i][i]; 604 | } while (--n); 605 | return tr; 606 | }, 607 | 608 | tr: function() { return this.trace(); }, 609 | 610 | // Returns the rank of the matrix 611 | rank: function() { 612 | var M = this.toRightTriangular(), rank = 0; 613 | var ni = this.elements.length, ki = ni, i, nj, kj = this.elements[0].length, j; 614 | do { i = ki - ni; 615 | nj = kj; 616 | do { j = kj - nj; 617 | if (Math.abs(M.elements[i][j]) > Sylvester.precision) { rank++; break; } 618 | } while (--nj); 619 | } while (--ni); 620 | return rank; 621 | }, 622 | 623 | rk: function() { return this.rank(); }, 624 | 625 | // Returns the result of attaching the given argument to the right-hand side of the matrix 626 | augment: function(matrix) { 627 | var M = matrix.elements || matrix; 628 | if (typeof(M[0][0]) == 'undefined') { M = Matrix.create(M).elements; } 629 | var T = this.dup(), cols = T.elements[0].length; 630 | var ni = T.elements.length, ki = ni, i, nj, kj = M[0].length, j; 631 | if (ni != M.length) { return null; } 632 | do { i = ki - ni; 633 | nj = kj; 634 | do { j = kj - nj; 635 | T.elements[i][cols + j] = M[i][j]; 636 | } while (--nj); 637 | } while (--ni); 638 | return T; 639 | }, 640 | 641 | // Returns the inverse (if one exists) using Gauss-Jordan 642 | inverse: function() { 643 | if (!this.isSquare() || this.isSingular()) { return null; } 644 | var ni = this.elements.length, ki = ni, i, j; 645 | var M = this.augment(Matrix.I(ni)).toRightTriangular(); 646 | var np, kp = M.elements[0].length, p, els, divisor; 647 | var inverse_elements = [], new_element; 648 | // Matrix is non-singular so there will be no zeros on the diagonal 649 | // Cycle through rows from last to first 650 | do { i = ni - 1; 651 | // First, normalise diagonal elements to 1 652 | els = []; np = kp; 653 | inverse_elements[i] = []; 654 | divisor = M.elements[i][i]; 655 | do { p = kp - np; 656 | new_element = M.elements[i][p] / divisor; 657 | els.push(new_element); 658 | // Shuffle of the current row of the right hand side into the results 659 | // array as it will not be modified by later runs through this loop 660 | if (p >= ki) { inverse_elements[i].push(new_element); } 661 | } while (--np); 662 | M.elements[i] = els; 663 | // Then, subtract this row from those above it to 664 | // give the identity matrix on the left hand side 665 | for (j = 0; j < i; j++) { 666 | els = []; np = kp; 667 | do { p = kp - np; 668 | els.push(M.elements[j][p] - M.elements[i][p] * M.elements[j][i]); 669 | } while (--np); 670 | M.elements[j] = els; 671 | } 672 | } while (--ni); 673 | return Matrix.create(inverse_elements); 674 | }, 675 | 676 | inv: function() { return this.inverse(); }, 677 | 678 | // Returns the result of rounding all the elements 679 | round: function() { 680 | return this.map(function(x) { return Math.round(x); }); 681 | }, 682 | 683 | // Returns a copy of the matrix with elements set to the given value if they 684 | // differ from it by less than Sylvester.precision 685 | snapTo: function(x) { 686 | return this.map(function(p) { 687 | return (Math.abs(p - x) <= Sylvester.precision) ? x : p; 688 | }); 689 | }, 690 | 691 | // Returns a string representation of the matrix 692 | inspect: function() { 693 | var matrix_rows = []; 694 | var n = this.elements.length, k = n, i; 695 | do { i = k - n; 696 | matrix_rows.push(Vector.create(this.elements[i]).inspect()); 697 | } while (--n); 698 | return matrix_rows.join('\n'); 699 | }, 700 | 701 | // Set the matrix's elements from an array. If the argument passed 702 | // is a vector, the resulting matrix will be a single column. 703 | setElements: function(els) { 704 | var i, elements = els.elements || els; 705 | if (typeof(elements[0][0]) != 'undefined') { 706 | var ni = elements.length, ki = ni, nj, kj, j; 707 | this.elements = []; 708 | do { i = ki - ni; 709 | nj = elements[i].length; kj = nj; 710 | this.elements[i] = []; 711 | do { j = kj - nj; 712 | this.elements[i][j] = elements[i][j]; 713 | } while (--nj); 714 | } while(--ni); 715 | return this; 716 | } 717 | var n = elements.length, k = n; 718 | this.elements = []; 719 | do { i = k - n; 720 | this.elements.push([elements[i]]); 721 | } while (--n); 722 | return this; 723 | } 724 | }; 725 | 726 | // Constructor function 727 | Matrix.create = function(elements) { 728 | var M = new Matrix(); 729 | return M.setElements(elements); 730 | }; 731 | 732 | // Identity matrix of size n 733 | Matrix.I = function(n) { 734 | var els = [], k = n, i, nj, j; 735 | do { i = k - n; 736 | els[i] = []; nj = k; 737 | do { j = k - nj; 738 | els[i][j] = (i == j) ? 1 : 0; 739 | } while (--nj); 740 | } while (--n); 741 | return Matrix.create(els); 742 | }; 743 | 744 | // Diagonal matrix - all off-diagonal elements are zero 745 | Matrix.Diagonal = function(elements) { 746 | var n = elements.length, k = n, i; 747 | var M = Matrix.I(n); 748 | do { i = k - n; 749 | M.elements[i][i] = elements[i]; 750 | } while (--n); 751 | return M; 752 | }; 753 | 754 | // Rotation matrix about some axis. If no axis is 755 | // supplied, assume we're after a 2D transform 756 | Matrix.Rotation = function(theta, a) { 757 | if (!a) { 758 | return Matrix.create([ 759 | [Math.cos(theta), -Math.sin(theta)], 760 | [Math.sin(theta), Math.cos(theta)] 761 | ]); 762 | } 763 | var axis = a.dup(); 764 | if (axis.elements.length != 3) { return null; } 765 | var mod = axis.modulus(); 766 | var x = axis.elements[0]/mod, y = axis.elements[1]/mod, z = axis.elements[2]/mod; 767 | var s = Math.sin(theta), c = Math.cos(theta), t = 1 - c; 768 | // Formula derived here: http://www.gamedev.net/reference/articles/article1199.asp 769 | // That proof rotates the co-ordinate system so theta 770 | // becomes -theta and sin becomes -sin here. 771 | return Matrix.create([ 772 | [ t*x*x + c, t*x*y - s*z, t*x*z + s*y ], 773 | [ t*x*y + s*z, t*y*y + c, t*y*z - s*x ], 774 | [ t*x*z - s*y, t*y*z + s*x, t*z*z + c ] 775 | ]); 776 | }; 777 | 778 | // Special case rotations 779 | Matrix.RotationX = function(t) { 780 | var c = Math.cos(t), s = Math.sin(t); 781 | return Matrix.create([ 782 | [ 1, 0, 0 ], 783 | [ 0, c, -s ], 784 | [ 0, s, c ] 785 | ]); 786 | }; 787 | Matrix.RotationY = function(t) { 788 | var c = Math.cos(t), s = Math.sin(t); 789 | return Matrix.create([ 790 | [ c, 0, s ], 791 | [ 0, 1, 0 ], 792 | [ -s, 0, c ] 793 | ]); 794 | }; 795 | Matrix.RotationZ = function(t) { 796 | var c = Math.cos(t), s = Math.sin(t); 797 | return Matrix.create([ 798 | [ c, -s, 0 ], 799 | [ s, c, 0 ], 800 | [ 0, 0, 1 ] 801 | ]); 802 | }; 803 | 804 | // Random matrix of n rows, m columns 805 | Matrix.Random = function(n, m) { 806 | return Matrix.Zero(n, m).map( 807 | function() { return Math.random(); } 808 | ); 809 | }; 810 | 811 | // Matrix filled with zeros 812 | Matrix.Zero = function(n, m) { 813 | var els = [], ni = n, i, nj, j; 814 | do { i = n - ni; 815 | els[i] = []; 816 | nj = m; 817 | do { j = m - nj; 818 | els[i][j] = 0; 819 | } while (--nj); 820 | } while (--ni); 821 | return Matrix.create(els); 822 | }; 823 | 824 | 825 | 826 | function Line() {} 827 | Line.prototype = { 828 | 829 | // Returns true if the argument occupies the same space as the line 830 | eql: function(line) { 831 | return (this.isParallelTo(line) && this.contains(line.anchor)); 832 | }, 833 | 834 | // Returns a copy of the line 835 | dup: function() { 836 | return Line.create(this.anchor, this.direction); 837 | }, 838 | 839 | // Returns the result of translating the line by the given vector/array 840 | translate: function(vector) { 841 | var V = vector.elements || vector; 842 | return Line.create([ 843 | this.anchor.elements[0] + V[0], 844 | this.anchor.elements[1] + V[1], 845 | this.anchor.elements[2] + (V[2] || 0) 846 | ], this.direction); 847 | }, 848 | 849 | // Returns true if the line is parallel to the argument. Here, 'parallel to' 850 | // means that the argument's direction is either parallel or antiparallel to 851 | // the line's own direction. A line is parallel to a plane if the two do not 852 | // have a unique intersection. 853 | isParallelTo: function(obj) { 854 | if (obj.normal) { return obj.isParallelTo(this); } 855 | var theta = this.direction.angleFrom(obj.direction); 856 | return (Math.abs(theta) <= Sylvester.precision || Math.abs(theta - Math.PI) <= Sylvester.precision); 857 | }, 858 | 859 | // Returns the line's perpendicular distance from the argument, 860 | // which can be a point, a line or a plane 861 | distanceFrom: function(obj) { 862 | if (obj.normal) { return obj.distanceFrom(this); } 863 | if (obj.direction) { 864 | // obj is a line 865 | if (this.isParallelTo(obj)) { return this.distanceFrom(obj.anchor); } 866 | var N = this.direction.cross(obj.direction).toUnitVector().elements; 867 | var A = this.anchor.elements, B = obj.anchor.elements; 868 | return Math.abs((A[0] - B[0]) * N[0] + (A[1] - B[1]) * N[1] + (A[2] - B[2]) * N[2]); 869 | } else { 870 | // obj is a point 871 | var P = obj.elements || obj; 872 | var A = this.anchor.elements, D = this.direction.elements; 873 | var PA1 = P[0] - A[0], PA2 = P[1] - A[1], PA3 = (P[2] || 0) - A[2]; 874 | var modPA = Math.sqrt(PA1*PA1 + PA2*PA2 + PA3*PA3); 875 | if (modPA === 0) return 0; 876 | // Assumes direction vector is normalized 877 | var cosTheta = (PA1 * D[0] + PA2 * D[1] + PA3 * D[2]) / modPA; 878 | var sin2 = 1 - cosTheta*cosTheta; 879 | return Math.abs(modPA * Math.sqrt(sin2 < 0 ? 0 : sin2)); 880 | } 881 | }, 882 | 883 | // Returns true iff the argument is a point on the line 884 | contains: function(point) { 885 | var dist = this.distanceFrom(point); 886 | return (dist !== null && dist <= Sylvester.precision); 887 | }, 888 | 889 | // Returns true iff the line lies in the given plane 890 | liesIn: function(plane) { 891 | return plane.contains(this); 892 | }, 893 | 894 | // Returns true iff the line has a unique point of intersection with the argument 895 | intersects: function(obj) { 896 | if (obj.normal) { return obj.intersects(this); } 897 | return (!this.isParallelTo(obj) && this.distanceFrom(obj) <= Sylvester.precision); 898 | }, 899 | 900 | // Returns the unique intersection point with the argument, if one exists 901 | intersectionWith: function(obj) { 902 | if (obj.normal) { return obj.intersectionWith(this); } 903 | if (!this.intersects(obj)) { return null; } 904 | var P = this.anchor.elements, X = this.direction.elements, 905 | Q = obj.anchor.elements, Y = obj.direction.elements; 906 | var X1 = X[0], X2 = X[1], X3 = X[2], Y1 = Y[0], Y2 = Y[1], Y3 = Y[2]; 907 | var PsubQ1 = P[0] - Q[0], PsubQ2 = P[1] - Q[1], PsubQ3 = P[2] - Q[2]; 908 | var XdotQsubP = - X1*PsubQ1 - X2*PsubQ2 - X3*PsubQ3; 909 | var YdotPsubQ = Y1*PsubQ1 + Y2*PsubQ2 + Y3*PsubQ3; 910 | var XdotX = X1*X1 + X2*X2 + X3*X3; 911 | var YdotY = Y1*Y1 + Y2*Y2 + Y3*Y3; 912 | var XdotY = X1*Y1 + X2*Y2 + X3*Y3; 913 | var k = (XdotQsubP * YdotY / XdotX + XdotY * YdotPsubQ) / (YdotY - XdotY * XdotY); 914 | return Vector.create([P[0] + k*X1, P[1] + k*X2, P[2] + k*X3]); 915 | }, 916 | 917 | // Returns the point on the line that is closest to the given point or line 918 | pointClosestTo: function(obj) { 919 | if (obj.direction) { 920 | // obj is a line 921 | if (this.intersects(obj)) { return this.intersectionWith(obj); } 922 | if (this.isParallelTo(obj)) { return null; } 923 | var D = this.direction.elements, E = obj.direction.elements; 924 | var D1 = D[0], D2 = D[1], D3 = D[2], E1 = E[0], E2 = E[1], E3 = E[2]; 925 | // Create plane containing obj and the shared normal and intersect this with it 926 | // Thank you: http://www.cgafaq.info/wiki/Line-line_distance 927 | var x = (D3 * E1 - D1 * E3), y = (D1 * E2 - D2 * E1), z = (D2 * E3 - D3 * E2); 928 | var N = Vector.create([x * E3 - y * E2, y * E1 - z * E3, z * E2 - x * E1]); 929 | var P = Plane.create(obj.anchor, N); 930 | return P.intersectionWith(this); 931 | } else { 932 | // obj is a point 933 | var P = obj.elements || obj; 934 | if (this.contains(P)) { return Vector.create(P); } 935 | var A = this.anchor.elements, D = this.direction.elements; 936 | var D1 = D[0], D2 = D[1], D3 = D[2], A1 = A[0], A2 = A[1], A3 = A[2]; 937 | var x = D1 * (P[1]-A2) - D2 * (P[0]-A1), y = D2 * ((P[2] || 0) - A3) - D3 * (P[1]-A2), 938 | z = D3 * (P[0]-A1) - D1 * ((P[2] || 0) - A3); 939 | var V = Vector.create([D2 * x - D3 * z, D3 * y - D1 * x, D1 * z - D2 * y]); 940 | var k = this.distanceFrom(P) / V.modulus(); 941 | return Vector.create([ 942 | P[0] + V.elements[0] * k, 943 | P[1] + V.elements[1] * k, 944 | (P[2] || 0) + V.elements[2] * k 945 | ]); 946 | } 947 | }, 948 | 949 | // Returns a copy of the line rotated by t radians about the given line. Works by 950 | // finding the argument's closest point to this line's anchor point (call this C) and 951 | // rotating the anchor about C. Also rotates the line's direction about the argument's. 952 | // Be careful with this - the rotation axis' direction affects the outcome! 953 | rotate: function(t, line) { 954 | // If we're working in 2D 955 | if (typeof(line.direction) == 'undefined') { line = Line.create(line.to3D(), Vector.k); } 956 | var R = Matrix.Rotation(t, line.direction).elements; 957 | var C = line.pointClosestTo(this.anchor).elements; 958 | var A = this.anchor.elements, D = this.direction.elements; 959 | var C1 = C[0], C2 = C[1], C3 = C[2], A1 = A[0], A2 = A[1], A3 = A[2]; 960 | var x = A1 - C1, y = A2 - C2, z = A3 - C3; 961 | return Line.create([ 962 | C1 + R[0][0] * x + R[0][1] * y + R[0][2] * z, 963 | C2 + R[1][0] * x + R[1][1] * y + R[1][2] * z, 964 | C3 + R[2][0] * x + R[2][1] * y + R[2][2] * z 965 | ], [ 966 | R[0][0] * D[0] + R[0][1] * D[1] + R[0][2] * D[2], 967 | R[1][0] * D[0] + R[1][1] * D[1] + R[1][2] * D[2], 968 | R[2][0] * D[0] + R[2][1] * D[1] + R[2][2] * D[2] 969 | ]); 970 | }, 971 | 972 | // Returns the line's reflection in the given point or line 973 | reflectionIn: function(obj) { 974 | if (obj.normal) { 975 | // obj is a plane 976 | var A = this.anchor.elements, D = this.direction.elements; 977 | var A1 = A[0], A2 = A[1], A3 = A[2], D1 = D[0], D2 = D[1], D3 = D[2]; 978 | var newA = this.anchor.reflectionIn(obj).elements; 979 | // Add the line's direction vector to its anchor, then mirror that in the plane 980 | var AD1 = A1 + D1, AD2 = A2 + D2, AD3 = A3 + D3; 981 | var Q = obj.pointClosestTo([AD1, AD2, AD3]).elements; 982 | var newD = [Q[0] + (Q[0] - AD1) - newA[0], Q[1] + (Q[1] - AD2) - newA[1], Q[2] + (Q[2] - AD3) - newA[2]]; 983 | return Line.create(newA, newD); 984 | } else if (obj.direction) { 985 | // obj is a line - reflection obtained by rotating PI radians about obj 986 | return this.rotate(Math.PI, obj); 987 | } else { 988 | // obj is a point - just reflect the line's anchor in it 989 | var P = obj.elements || obj; 990 | return Line.create(this.anchor.reflectionIn([P[0], P[1], (P[2] || 0)]), this.direction); 991 | } 992 | }, 993 | 994 | // Set the line's anchor point and direction. 995 | setVectors: function(anchor, direction) { 996 | // Need to do this so that line's properties are not 997 | // references to the arguments passed in 998 | anchor = Vector.create(anchor); 999 | direction = Vector.create(direction); 1000 | if (anchor.elements.length == 2) {anchor.elements.push(0); } 1001 | if (direction.elements.length == 2) { direction.elements.push(0); } 1002 | if (anchor.elements.length > 3 || direction.elements.length > 3) { return null; } 1003 | var mod = direction.modulus(); 1004 | if (mod === 0) { return null; } 1005 | this.anchor = anchor; 1006 | this.direction = Vector.create([ 1007 | direction.elements[0] / mod, 1008 | direction.elements[1] / mod, 1009 | direction.elements[2] / mod 1010 | ]); 1011 | return this; 1012 | } 1013 | }; 1014 | 1015 | 1016 | // Constructor function 1017 | Line.create = function(anchor, direction) { 1018 | var L = new Line(); 1019 | return L.setVectors(anchor, direction); 1020 | }; 1021 | 1022 | // Axes 1023 | Line.X = Line.create(Vector.Zero(3), Vector.i); 1024 | Line.Y = Line.create(Vector.Zero(3), Vector.j); 1025 | Line.Z = Line.create(Vector.Zero(3), Vector.k); 1026 | 1027 | 1028 | 1029 | function Plane() {} 1030 | Plane.prototype = { 1031 | 1032 | // Returns true iff the plane occupies the same space as the argument 1033 | eql: function(plane) { 1034 | return (this.contains(plane.anchor) && this.isParallelTo(plane)); 1035 | }, 1036 | 1037 | // Returns a copy of the plane 1038 | dup: function() { 1039 | return Plane.create(this.anchor, this.normal); 1040 | }, 1041 | 1042 | // Returns the result of translating the plane by the given vector 1043 | translate: function(vector) { 1044 | var V = vector.elements || vector; 1045 | return Plane.create([ 1046 | this.anchor.elements[0] + V[0], 1047 | this.anchor.elements[1] + V[1], 1048 | this.anchor.elements[2] + (V[2] || 0) 1049 | ], this.normal); 1050 | }, 1051 | 1052 | // Returns true iff the plane is parallel to the argument. Will return true 1053 | // if the planes are equal, or if you give a line and it lies in the plane. 1054 | isParallelTo: function(obj) { 1055 | var theta; 1056 | if (obj.normal) { 1057 | // obj is a plane 1058 | theta = this.normal.angleFrom(obj.normal); 1059 | return (Math.abs(theta) <= Sylvester.precision || Math.abs(Math.PI - theta) <= Sylvester.precision); 1060 | } else if (obj.direction) { 1061 | // obj is a line 1062 | return this.normal.isPerpendicularTo(obj.direction); 1063 | } 1064 | return null; 1065 | }, 1066 | 1067 | // Returns true iff the receiver is perpendicular to the argument 1068 | isPerpendicularTo: function(plane) { 1069 | var theta = this.normal.angleFrom(plane.normal); 1070 | return (Math.abs(Math.PI/2 - theta) <= Sylvester.precision); 1071 | }, 1072 | 1073 | // Returns the plane's distance from the given object (point, line or plane) 1074 | distanceFrom: function(obj) { 1075 | if (this.intersects(obj) || this.contains(obj)) { return 0; } 1076 | if (obj.anchor) { 1077 | // obj is a plane or line 1078 | var A = this.anchor.elements, B = obj.anchor.elements, N = this.normal.elements; 1079 | return Math.abs((A[0] - B[0]) * N[0] + (A[1] - B[1]) * N[1] + (A[2] - B[2]) * N[2]); 1080 | } else { 1081 | // obj is a point 1082 | var P = obj.elements || obj; 1083 | var A = this.anchor.elements, N = this.normal.elements; 1084 | return Math.abs((A[0] - P[0]) * N[0] + (A[1] - P[1]) * N[1] + (A[2] - (P[2] || 0)) * N[2]); 1085 | } 1086 | }, 1087 | 1088 | // Returns true iff the plane contains the given point or line 1089 | contains: function(obj) { 1090 | if (obj.normal) { return null; } 1091 | if (obj.direction) { 1092 | return (this.contains(obj.anchor) && this.contains(obj.anchor.add(obj.direction))); 1093 | } else { 1094 | var P = obj.elements || obj; 1095 | var A = this.anchor.elements, N = this.normal.elements; 1096 | var diff = Math.abs(N[0]*(A[0] - P[0]) + N[1]*(A[1] - P[1]) + N[2]*(A[2] - (P[2] || 0))); 1097 | return (diff <= Sylvester.precision); 1098 | } 1099 | }, 1100 | 1101 | // Returns true iff the plane has a unique point/line of intersection with the argument 1102 | intersects: function(obj) { 1103 | if (typeof(obj.direction) == 'undefined' && typeof(obj.normal) == 'undefined') { return null; } 1104 | return !this.isParallelTo(obj); 1105 | }, 1106 | 1107 | // Returns the unique intersection with the argument, if one exists. The result 1108 | // will be a vector if a line is supplied, and a line if a plane is supplied. 1109 | intersectionWith: function(obj) { 1110 | if (!this.intersects(obj)) { return null; } 1111 | if (obj.direction) { 1112 | // obj is a line 1113 | var A = obj.anchor.elements, D = obj.direction.elements, 1114 | P = this.anchor.elements, N = this.normal.elements; 1115 | var multiplier = (N[0]*(P[0]-A[0]) + N[1]*(P[1]-A[1]) + N[2]*(P[2]-A[2])) / (N[0]*D[0] + N[1]*D[1] + N[2]*D[2]); 1116 | return Vector.create([A[0] + D[0]*multiplier, A[1] + D[1]*multiplier, A[2] + D[2]*multiplier]); 1117 | } else if (obj.normal) { 1118 | // obj is a plane 1119 | var direction = this.normal.cross(obj.normal).toUnitVector(); 1120 | // To find an anchor point, we find one co-ordinate that has a value 1121 | // of zero somewhere on the intersection, and remember which one we picked 1122 | var N = this.normal.elements, A = this.anchor.elements, 1123 | O = obj.normal.elements, B = obj.anchor.elements; 1124 | var solver = Matrix.Zero(2,2), i = 0; 1125 | while (solver.isSingular()) { 1126 | i++; 1127 | solver = Matrix.create([ 1128 | [ N[i%3], N[(i+1)%3] ], 1129 | [ O[i%3], O[(i+1)%3] ] 1130 | ]); 1131 | } 1132 | // Then we solve the simultaneous equations in the remaining dimensions 1133 | var inverse = solver.inverse().elements; 1134 | var x = N[0]*A[0] + N[1]*A[1] + N[2]*A[2]; 1135 | var y = O[0]*B[0] + O[1]*B[1] + O[2]*B[2]; 1136 | var intersection = [ 1137 | inverse[0][0] * x + inverse[0][1] * y, 1138 | inverse[1][0] * x + inverse[1][1] * y 1139 | ]; 1140 | var anchor = []; 1141 | for (var j = 1; j <= 3; j++) { 1142 | // This formula picks the right element from intersection by 1143 | // cycling depending on which element we set to zero above 1144 | anchor.push((i == j) ? 0 : intersection[(j + (5 - i)%3)%3]); 1145 | } 1146 | return Line.create(anchor, direction); 1147 | } 1148 | }, 1149 | 1150 | // Returns the point in the plane closest to the given point 1151 | pointClosestTo: function(point) { 1152 | var P = point.elements || point; 1153 | var A = this.anchor.elements, N = this.normal.elements; 1154 | var dot = (A[0] - P[0]) * N[0] + (A[1] - P[1]) * N[1] + (A[2] - (P[2] || 0)) * N[2]; 1155 | return Vector.create([P[0] + N[0] * dot, P[1] + N[1] * dot, (P[2] || 0) + N[2] * dot]); 1156 | }, 1157 | 1158 | // Returns a copy of the plane, rotated by t radians about the given line 1159 | // See notes on Line#rotate. 1160 | rotate: function(t, line) { 1161 | var R = Matrix.Rotation(t, line.direction).elements; 1162 | var C = line.pointClosestTo(this.anchor).elements; 1163 | var A = this.anchor.elements, N = this.normal.elements; 1164 | var C1 = C[0], C2 = C[1], C3 = C[2], A1 = A[0], A2 = A[1], A3 = A[2]; 1165 | var x = A1 - C1, y = A2 - C2, z = A3 - C3; 1166 | return Plane.create([ 1167 | C1 + R[0][0] * x + R[0][1] * y + R[0][2] * z, 1168 | C2 + R[1][0] * x + R[1][1] * y + R[1][2] * z, 1169 | C3 + R[2][0] * x + R[2][1] * y + R[2][2] * z 1170 | ], [ 1171 | R[0][0] * N[0] + R[0][1] * N[1] + R[0][2] * N[2], 1172 | R[1][0] * N[0] + R[1][1] * N[1] + R[1][2] * N[2], 1173 | R[2][0] * N[0] + R[2][1] * N[1] + R[2][2] * N[2] 1174 | ]); 1175 | }, 1176 | 1177 | // Returns the reflection of the plane in the given point, line or plane. 1178 | reflectionIn: function(obj) { 1179 | if (obj.normal) { 1180 | // obj is a plane 1181 | var A = this.anchor.elements, N = this.normal.elements; 1182 | var A1 = A[0], A2 = A[1], A3 = A[2], N1 = N[0], N2 = N[1], N3 = N[2]; 1183 | var newA = this.anchor.reflectionIn(obj).elements; 1184 | // Add the plane's normal to its anchor, then mirror that in the other plane 1185 | var AN1 = A1 + N1, AN2 = A2 + N2, AN3 = A3 + N3; 1186 | var Q = obj.pointClosestTo([AN1, AN2, AN3]).elements; 1187 | var newN = [Q[0] + (Q[0] - AN1) - newA[0], Q[1] + (Q[1] - AN2) - newA[1], Q[2] + (Q[2] - AN3) - newA[2]]; 1188 | return Plane.create(newA, newN); 1189 | } else if (obj.direction) { 1190 | // obj is a line 1191 | return this.rotate(Math.PI, obj); 1192 | } else { 1193 | // obj is a point 1194 | var P = obj.elements || obj; 1195 | return Plane.create(this.anchor.reflectionIn([P[0], P[1], (P[2] || 0)]), this.normal); 1196 | } 1197 | }, 1198 | 1199 | // Sets the anchor point and normal to the plane. If three arguments are specified, 1200 | // the normal is calculated by assuming the three points should lie in the same plane. 1201 | // If only two are sepcified, the second is taken to be the normal. Normal vector is 1202 | // normalised before storage. 1203 | setVectors: function(anchor, v1, v2) { 1204 | anchor = Vector.create(anchor); 1205 | anchor = anchor.to3D(); if (anchor === null) { return null; } 1206 | v1 = Vector.create(v1); 1207 | v1 = v1.to3D(); if (v1 === null) { return null; } 1208 | if (typeof(v2) == 'undefined') { 1209 | v2 = null; 1210 | } else { 1211 | v2 = Vector.create(v2); 1212 | v2 = v2.to3D(); if (v2 === null) { return null; } 1213 | } 1214 | var A1 = anchor.elements[0], A2 = anchor.elements[1], A3 = anchor.elements[2]; 1215 | var v11 = v1.elements[0], v12 = v1.elements[1], v13 = v1.elements[2]; 1216 | var normal, mod; 1217 | if (v2 !== null) { 1218 | var v21 = v2.elements[0], v22 = v2.elements[1], v23 = v2.elements[2]; 1219 | normal = Vector.create([ 1220 | (v12 - A2) * (v23 - A3) - (v13 - A3) * (v22 - A2), 1221 | (v13 - A3) * (v21 - A1) - (v11 - A1) * (v23 - A3), 1222 | (v11 - A1) * (v22 - A2) - (v12 - A2) * (v21 - A1) 1223 | ]); 1224 | mod = normal.modulus(); 1225 | if (mod === 0) { return null; } 1226 | normal = Vector.create([normal.elements[0] / mod, normal.elements[1] / mod, normal.elements[2] / mod]); 1227 | } else { 1228 | mod = Math.sqrt(v11*v11 + v12*v12 + v13*v13); 1229 | if (mod === 0) { return null; } 1230 | normal = Vector.create([v1.elements[0] / mod, v1.elements[1] / mod, v1.elements[2] / mod]); 1231 | } 1232 | this.anchor = anchor; 1233 | this.normal = normal; 1234 | return this; 1235 | } 1236 | }; 1237 | 1238 | // Constructor function 1239 | Plane.create = function(anchor, v1, v2) { 1240 | var P = new Plane(); 1241 | return P.setVectors(anchor, v1, v2); 1242 | }; 1243 | 1244 | // X-Y-Z planes 1245 | Plane.XY = Plane.create(Vector.Zero(3), Vector.k); 1246 | Plane.YZ = Plane.create(Vector.Zero(3), Vector.i); 1247 | Plane.ZX = Plane.create(Vector.Zero(3), Vector.j); 1248 | Plane.YX = Plane.XY; Plane.ZY = Plane.YZ; Plane.XZ = Plane.ZX; 1249 | 1250 | // Utility functions 1251 | var $V = Vector.create; 1252 | var $M = Matrix.create; 1253 | var $L = Line.create; 1254 | var $P = Plane.create; 1255 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "appium-robots", 3 | "description": "Robots for Appium.", 4 | "tags": [ 5 | "robots", 6 | "automation", 7 | "javascript" 8 | ], 9 | "version": "0.0.1", 10 | "author": "appium-discuss@googlegroups.com", 11 | "repository": { 12 | "type": "git", 13 | "url": "https://github.com/appium/robots.git" 14 | }, 15 | "bugs": { 16 | "url": "https://github.com/appium/robots/issues" 17 | }, 18 | "engines": [ 19 | "node" 20 | ], 21 | "main": "./server.js", 22 | "dependencies": { 23 | "express": "~3.0.6", 24 | "grunt": "~0.4.0", 25 | "grunt-cli": "~0.1.6", 26 | "argparse": "~0.1.10", 27 | "path": "~0.4.9", 28 | "temp": "~0.5.0", 29 | "request": "~2.12.0", 30 | "win-spawn" : "~1.1.1", 31 | "johnny-five": "git://github.com/rwaldron/johnny-five.git", 32 | "wd-sync" : "~1.0.1", 33 | "xmlhttprequest" : "~1.5.0", 34 | "sylvester":"0.0.21", 35 | "sleep":"~1.1.2" 36 | } 37 | } 38 | --------------------------------------------------------------------------------