Mintoris Forum

Author Topic: Robotics via WiFi  (Read 10234 times)

Graham

  • Full Member
  • ***
  • Posts: 11
Robotics via WiFi
« on: Mar 09, 2013, 02:51 PM »
Some of you might be aware of the iOS and Android WiFi receiver from Hobbyking.
http://www.hobbyking.com/hobbyking/store/uh_viewItem.asp?idProduct=21430
It acts as a WiFi hotspot, and apps are available for iOS (appstore) and for android (from Hobbyking site…. see purple "Files" tab towards the bottom of the page to which the link above will take you). These apps assume you want to use it to control a 4 channel radio-controlled car or aeroplane, using your smart-phone or tablet.  There is also a downloadable folder ("Android project source", containing folder "library") that contains material that would no doubt mean something to many of you……‥ but not to me…… (I program only in basic, and probably rather badly).

I want to use this thing for a quite different project, and I would love to be able to program it in Mintoris Basic. Does anyone have any thoughts?  I know there has been interest in using Mintoris basic for robotics (discussed in the Bluetooth forum)…‥ would this cheap and simple WiFi device be a good starting point???  (It seems to work well. I  connected it to some cheap servos, and then my Sony Ericsson XPERIA arc S controlled all four of them beautifully).

Any thoughts would be valuable to someone as ignorant as me…… even the opinion that it is likely to be impossible, or beyond my capabilities‼
Graham

Chuck

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1899
Re: Robotics via WiFi
« Reply #1 on: Mar 09, 2013, 07:12 PM »
Well I don't have the receiver to test, but I downloaded the android app source code, analyzed it and wrote this little test program that just might work.

I wrote this without testing it so it may have bugs and might not work at all. You will have to experiment with it and let me know what happens.

I also attached the .bas file below so you can download it.

-Chuck

Look for updated program in a later post.
« Last Edit: Mar 13, 2013, 03:13 AM by Chuck »

Chuck

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1899
Re: Robotics via WiFi
« Reply #2 on: Mar 09, 2013, 11:27 PM »
Well, did it work???  ;D
« Last Edit: Mar 10, 2013, 05:24 AM by Chuck »

Graham

  • Full Member
  • ***
  • Posts: 11
Re: Robotics via WiFi
« Reply #3 on: Mar 10, 2013, 10:12 AM »
Thanks‼ That will get me started.  I have only spent 5 mins playing with it…. no time last night.  But I guess that "for i=0 to 9" should be "for i=1 to 10" since character 0 of a string seems wrong… and that line comes up as an error before it has had time to print "Connected", though I assume it was connected. When changed to "for i=1 to 10" it prints "Connected", but the servos do not twitch‼ Presumably I also need to make it loop so that a continuous stream of values is sent to the receiver?
Thanks again‼ Any further thoughts will be welcome.

Graham

  • Full Member
  • ***
  • Posts: 11
Re: Robotics via WiFi
« Reply #4 on: Mar 10, 2013, 11:54 AM »
IT WORKS ‼‼ (with the "i=0 to 9" typo corrected to "i=1 to 10") But I discovered that the channel values need to be between 0 and 100.  If you go up to 255 odd things happen, and it stops working. So I added do-until loops that alternate between sending channel values of 0 for a few seconds, and then sending channel values of 100 for a few seconds, and then back to 0.  This seems to cause a full range of movement of the servos…‥ the same as is provided by the apps that can be downloaded. So from now on it is easy‼  I will write touch-screen based routines to change the channel values, and add a nice background image. Thanks so much for your help‼  And no doubt others will find this an absurdly easy way to use Mintoris basic for radio control. Would you like me to put this onto the Hobbyking website discussion forum, or do you want to do it?

Chuck

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1899
Re: Robotics via WiFi
« Reply #5 on: Mar 10, 2013, 06:51 PM »
Outstanding, you debugged it perfectly. Yes, please post your results on the HobbyKing site. We will also need a working demo posted here. In your loop you should continuously check if TcpGetState(1) = 1. If it is not 1 you should immediately do a TcpClose 1 and then try to reconnect with a TcpOpen.


Chuck

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1899
Re: Robotics via WiFi
« Reply #6 on: Mar 10, 2013, 07:49 PM »
Here is my second attempt at a demo program. I defined null$, and header$ as global constants for efficiency. I also put the code in a double loop to stream the data to the receiver and recover from loss of signal. I set the wait delay to 25 ms. We need to find the longest delay that will still work just so people will know. Let me know if this works. I'm going to have to get one of those receivers.



Global null$, header$
null$ = chr$(0)
header$ = chr$(85) + chr$(0) + chr$(11) + chr$(0)

do

  Print "Attempting to connect…"
  wait 1

  TcpOpen 1, "10.10.100.254", 8899, "c", 12000, 1000

  if TcpGetState(1) = 1 then

    Print "Connected"
    wait 1

    do while TcpGetState(1) = 1

      ' set your channel values here.
      sendRawChannels(0, 25, 50, 100)
      wait 50

    loop

  endif

  Print "Not Connected"
  Print TcpGetError$(1)
  TcpClose 1

loop

end


Sub sendRawChannels(ch1, ch2, ch3, ch4)

  ' Add header
  packet$ = header$

  ' Add channel values
  packet$ = packet$ + chr$(ch1 & 255)  
  packet$ = packet$ + chr$(ch2 & 255)  
  packet$ = packet$ + chr$(ch3 & 255)  
  packet$ = packet$ + chr$(ch4 & 255)  

  ' Two bytes not used
  packet$ = packet$ + null$  
  packet$ = packet$ + null$  

  sum = 0
  for i = 1 to 10
    sum = sum + ascii(mid$(packet$, i, 1))
  next

  ' Add checksum byte
  packet$ = packet$ + chr$(sum % 256)

  TcpWrite 1, packet$

End Sub
« Last Edit: Mar 13, 2013, 03:16 AM by Chuck »

Graham

  • Full Member
  • ***
  • Posts: 11
Re: Robotics via WiFi
« Reply #7 on: Mar 10, 2013, 10:05 PM »
I can now control all 4 channels by moving 4 circles up and down the screen using the touch function. It works perfectly with a 10 millisecond delay (i.e. Touch x,y,10). If the delay is too long the servos give up and return to their resting positions. I have not yet worked out the optimum value for this delay, or the maximum permissible delay before the gizmo reverts to standby…. but I think it can be quite long so there is plenty of time for other bits of programming to happen.
Incidentally, basic keeps trying to update Reference while logged in to the Hobbyking WiFi gizmo, which is annoying. (It is already up-to-date in my Android phone). Then it states that there is no internet connection. This does not seem to matter because it does not interrupt the link to the receiver, but it would be nice to be able to stop this from happening. ??
I will put something on the Hobbyking site. Your new version will certainly work. I will try putting my "Touch" routine into it, and report back, and upload a version that works at least on my phone.

Chuck

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1899
Re: Robotics via WiFi
« Reply #8 on: Mar 10, 2013, 10:11 PM »
hmm, I'll fix that reference update problem. I didn't think of the situation where you have wifi but don't have internet.

Chuck

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1899
Re: Robotics via WiFi
« Reply #9 on: Mar 10, 2013, 11:21 PM »
I fixed the reference update progress bar and messages. They will no longer appear if there is no internet connection and the reference is up to date. This change will be in the next release.

Graham

  • Full Member
  • ***
  • Posts: 11
Re: Robotics via WiFi
« Reply #10 on: Mar 12, 2013, 09:33 AM »
It works beautifully using the last structure that you posted…‥many thanks for your help‼  I have changed it to use graphics mode (landscape) so I can add an image (though I have not done so), and the servo channel values (0-100) are represented on screen by red circles that can be moved up and down the landscape screen. If I get far enough from the receiver for the connection to be lost,  the error code goes to the clipboard, and the phone reconnects perfectly when I approach the receiver again, (except when the phone has switched to another WiFi network‼  I assume there is no way round that, so it is best to make the phone forget favourite networks that are in range).  At present you tap the screen to move the red circles and change the servo channel values…‥they move to the tapped point. But I assume that one could program a real dragging of the circles which would be better for anyone wanting to control a radio-controlled aeroplane‼ Preferably one ought to be able to drag two channels at once with two thumbs.
I hope this will inspire some more sales of your wonderful basic……. this must be the easiest way to program radio control.
I attach the program that works on my Sony Ericsson Xperia arc S. I apologise for the fact that it assumes the 480x854 screen resolution…‥ it needs to be rewritten in a device-independent way.

Chuck

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1899
Re: Robotics via WiFi
« Reply #11 on: Mar 12, 2013, 04:57 PM »
I checked the HobbyKing source code and they send out channel packets every 50ms. Test the program with a Touch x,y, 50. I think 10ms is too fast, you don't want to overload the receiver. After you test it, we can post it in the Programs->Demos section of the forum so people can find it easier.

All you need to do to make your program device-independent is to add the SetScale command to tell Basic what size screen you are designing the program to use. Put it right after the Graphics On command.

SetScale 480, 854

Oh, I see you've done that. Your program should work on any screen size.

The TouchEvent command might work better than the Touch command. Check out the Multi-Touch Demo program in the Programs->Demos section. They do some special tricks in the HobbyKing code to support multiple drag points. I would have to add something like a touch hotspot where you define a rectangle that would report special, localized touch events.

Great work‼
« Last Edit: Mar 12, 2013, 05:42 PM by Chuck »

Graham

  • Full Member
  • ***
  • Posts: 11
Re: Robotics via WiFi
« Reply #12 on: Mar 12, 2013, 05:48 PM »
Thanks for the comments‼  In fact you can increase the "touch" delay right up to 800 ms, without any problems. But at 1000 the servos occasionally twitch, as they try to go back to their resting positions. At 1200 ms they twitch all the time. So I will change it to 50 ms.
I had already put this onto the Hobbyking site, so you might want to add a comment there to explain that 50ms would be preferable.
I had looked at your TouchEvent demo, and I will try using it when I am sure that I understand it‼

Chuck

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1899
Re: Robotics via WiFi
« Reply #13 on: Mar 12, 2013, 09:58 PM »
Those two unused bytes in the channel packet are interesting. It's as if they left room for two more channels. I hope they improve this product. It would also be nice to get some info back from the receiver.

Graham

  • Full Member
  • ***
  • Posts: 11
Re: Robotics via WiFi
« Reply #14 on: Mar 13, 2013, 02:43 PM »
I really must stop playing with this‼ Here is a version that uses TouchEvent instead of Touch. It is much better because you can now drag the buttons smoothly. In the version using Touch you were obliged to guess at the required setting for the servos and tap hopefully in the corresponding position on the screen. With this version I think a radio control freak really would be able to fly a plane.

Graham

  • Full Member
  • ***
  • Posts: 11
Re: Robotics via WiFi
« Reply #15 on: Mar 15, 2013, 05:11 PM »
So what about this device :-  http://www.wirc.dension.com/
It has more channels, and also it can stream live video from a USB webcam to the android or iOS device. I do not have this receiver, but if you think that it could be controlled by Mintoris Basic, just like the HobbyKing receiver, then I will order one. 
An app  for it is available on the Google Play store (search for "WiRC"), but it is 3.7 MB so I cannot upload it here. (I have the  apk file and could email if I knew where to send it).

Thanks for any help you can offer.

Chuck

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1899
Re: Robotics via WiFi
« Reply #16 on: Mar 15, 2013, 05:27 PM »
The only reason I was able to control the Hobbyking device is that the provided the source code to their app. I've sent an email Denison and asked for the information I need. The control will probably be easy but, the video will be more difficult.

Graham

  • Full Member
  • ***
  • Posts: 11
Re: Robotics via WiFi
« Reply #17 on: Mar 15, 2013, 06:52 PM »
OK‼ Forgive my ignorance. I thought maybe someone with your skills could unzip the apk and browse through its components, or decompile it using "apktool" ……‥ whatever that is……. which I have seen mentioned elsewhere ‼ 
Incidentally, was the way I used TouchEvent for the HobbyKing receiver OK? If so I guess it could be uploaded to the program section…… attributed 99% to you‼

Chuck

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1899
Re: Robotics via WiFi
« Reply #18 on: Mar 15, 2013, 06:57 PM »
Yes, it looks fine. You could probably change:

If idx=0 and action =2  then

to

If idx=0 then

So that you catch the touch down and touch up events also.

Chuck

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1899
Re: Robotics via WiFi
« Reply #19 on: Mar 18, 2013, 01:45 PM »
I contacted the people at http://www.wirc.dension.com/ about getting the info needed to program their WiFi RC Receiver. They wrote back and wanted me to sign a nondisclosure agreement. I wrote back saying I couldn't do that since my intention was to reveal how to program their device in Mintoris Basic.

-Chuck