Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implemented serial driver to facilitate communication with PSLab Devices #2488

Merged
merged 4 commits into from
Jun 23, 2024

Conversation

AsCress
Copy link
Contributor

@AsCress AsCress commented Jun 17, 2024

Resolves #2472
Implements the usb-serial-for-android library to facilitate communication with the PSLab device, as discussed in our weekly GSoC meet.

Changes

  • This PR reconfigures the CommunicationHandler.java class to implement the usb-serial-for-android library, in order to facilitate communication with the new V6 device, while also having backwards compatibility with the V5.

Screenshots / Recordings

N/A

Checklist:

  • No hard coding: I have used resources from strings.xml, dimens.xml and colors.xml without hard coding any value.
  • No end of file edits: No modifications done at end of resource files strings.xml, dimens.xml or colors.xml.
  • Code reformatting: I have reformatted code and fixed indentation in every file included in this pull request.
  • No extra space: My code does not contain any extra lines or extra spaces than the ones that are necessary.

Note: -
I have only been able to test this new driver with the V6 device.
@marcnause Could you please help me with testing this on the V5 device and confirm if all the features work fine ?

Copy link

github-actions bot commented Jun 17, 2024

@AsCress AsCress added the Feature New addition to the existing app label Jun 17, 2024
@marcnause
Copy link
Contributor

marcnause commented Jun 17, 2024

@AsCress I tested with the APK I downloaded via the link that was provided by the pipeline. Thumbs up for PR #2487! 😉

I tested the app on my Motorola Moto X Play and the PSLab v5 device.

During testing I used the Wave Generator, the Oscilloscope, the Power Supply and the Multimeter which worked in general, but I noticed a few issues.

As expected, the app recognizes the PSLab v5 device when it is connected to the phone before the app is started.

The phone will ask if I want to allow access to the device and if I want the app to open every time the PSLab v5 device is connected. The settings seem to be not persisted, I get asked the same question on every startup.

The app notices when the PSLab v5 device is disconnected, but it does not recognize if the the PSLab v5 device is connected again.

The Oscilloscope shows glitches in the waveforms for sine waves and digital waves created with the Wave Generator. The glitches don't show up all of the time, but they are very noticeable.

Screenshot_20240617-203841
Screenshot_20240617-204131

Please let me know if there is anything I can do to help you debugging these issues.

Copy link
Collaborator

@CloudyPadmal CloudyPadmal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@AsCress
Copy link
Contributor Author

AsCress commented Jun 18, 2024

@marcnause Thank you very much for testing on the V5 device.
From all the information that you provided and by observing the waveforms, the best issue I can guess here is that the device is having communication problems with the app (since I was facing similar issues with the V6 initially), as we are using the CDC ACM driver for the V5 device, and I couldn't test it, since I don't have the V5 device.
As far as the debugging is concerned, it would be great if you could provide me the logcat files generated while running the app when you encounter these issues.
It took me a couple of hours to debug the V6 device and make it error free, but it was possible only because I had that one with me.
I'll try my best to do whatever I can by just looking at the logs. 🙂

@AsCress
Copy link
Contributor Author

AsCress commented Jun 18, 2024

@marcnause I've fixed the permission issues. Should be retaining permissions now. Can you please confirm that as well ?

@marcnause
Copy link
Contributor

@AsCress Retaining permission works now. When I connect the PSLab v5 to my phone the app opens. The device is also recognized after re connection now.

I noticed one problem though. If I wait approximately 10 seconds until I disconnect the PSLab v5, everything works fine. If I wait only a few seconds (I counted from 1 to 5), the app crashes when I disconnect the device.

Here is a log file where I connect he device, wait for approximately 10 seconds, re-connect the device and then remove it after approximately 5 seconds.

crash.txt

I also created a sine wave in the Wave Generator, but now there is no waveform visible in the Oscilloscope. I have captured the log and it is visible that the data of teh wave contains only zeros.

pslab_sine.txt

I did the same with a digital wave and the waveform is a horizontal line with spikes instead of squares.

pslab_digital.txt

Screenshot_20240618-222332

@AsCress
Copy link
Contributor Author

AsCress commented Jun 19, 2024

@marcnause Thank you very much for testing and the logs. They helped me very much. I've added some instructions in there to start communication with the v5 device. Can you please test if the communication works now ?

@marcnause
Copy link
Contributor

@AsCress I was able to crash the app by disconnecting after a few seconds, but I had to find the sweet spot.

crash.txt

I still get weird waveforms in the Oscilloscope. I created a sine wave with the Wave Generator and got this result. 🤔

sine.txt

Screenshot_20240619-180915

If there is anything in particular I can do to support you, please let me know.

@AsCress
Copy link
Contributor Author

AsCress commented Jun 19, 2024

@marcnause Just trying a quick fix to check if this fixes the issues. Could please test this again ? Thanks.

@marcnause
Copy link
Contributor

@AsCress I tested the latest version, but as far as I can tell, the behavior visible to the user did not change.

What I did:

  1. Open app
  2. Connect PSLab v5
  3. Set frequency for sine wave for CH1 in Wave Generator
  4. Start Oscilloscope from Wave Generator
  5. Choose CH1 in Oscilloscope
  6. Look at waveform
  7. Disconnect PSLab v5
  8. Tap back button until app closes

Here is the log:

log.txt

@AsCress
Copy link
Contributor Author

AsCress commented Jun 20, 2024

@marcnause Thanks for the testing. I've just pushed in a new version. Tested this today alongwith @bessman using an old android phone and a v5 device, remotely. The communication worked (there were no read errors as such in the logs) however, there were spikes as you had mentioned before and the amplitude of noise on any unconnected channel was too high. Can you please once test this version with your setup and check if the same issue persists ? It'd be great if you could provide logs as well. Thanks !!

Edit:-
The latest version I'm pushing now should solve the crash issue as well.

@marcnause
Copy link
Contributor

I still see errors in the log of my phone: log.txt

And I was still able to provoke crashes: crash.txt

I just remembered that @bessman mentioned that there are at least 2 versions of the firmware for the PSLab v5. I got my device in 2019 and never updated the firmware. I don't know if it makes any difference.

@bessman
Copy link
Contributor

bessman commented Jun 20, 2024

there are at least 2 versions of the firmware for the PSLab v5.

I don't think I've said that. As far as I know all v5 boards shipped with firmware version 2.0. Perhaps you misheard "firmware version 2" as "two firmware versions"?

@marcnause
Copy link
Contributor

Perhaps you misheard "firmware version 2" as "two firmware versions"?

Oh, OK! That was a misunderstanding on my part.

@AsCress
Copy link
Contributor Author

AsCress commented Jun 21, 2024

@marcnause @bessman This is quite strange though 🤔🤔. An implementation which works on one V5 device doesn't work at all on another. Any inputs as to what could be causing it ? Nonetheless, I plan to take on this topic in detail in our weekly meet.

@marcnause
Copy link
Contributor

So far I only tested with one phone. Maybe it would be a good idea to try other devices to rule out an issue with the phone. I will do that later today and post the results here.

@AsCress
Copy link
Contributor Author

AsCress commented Jun 21, 2024

@marcnause That would be great !!

@marcnause
Copy link
Contributor

@AsCress I tested again with different Android devices, I downloaded the APK from Github again and also built an APK from your branch, but always with the same result described above already.

Do you still have any uncommitted changes? Do you still have the APK you used for testing with the PSLab v5 (just to rule out any mistakes on my side)? Maybe we can discuss the setup you used for testing in our weekly meeting. Is it possible that it had an impact on the communication (e.g. timing)?

@marcnause
Copy link
Contributor

I tested again today with different cables and different versions of the app. Turns out that the problem was the way I had connected outputs and inputs of my PSLab. I had connected PV1 to VOL and there is a bug which prevents the board to boot properly if this connection is present during startup (fossasia/pslab-firmware#142). The stupid thing is, that I reported the bug myself last year. 🤦 @AsCress, I have to apologize to you for wasting your time.

Once I removed the cable, I was able to observe a waveform in the oscilloscope (with occasional glitches as described in #2488 (comment)).

I noticed the "PV1/VOL-bug" when I used the PSLab Python library. The Python library reports "Device not found". Would it be possible to detect that the PSLab is in the weird state in the Android app and warn the user about the bug? If yes, we should create another ticket for that.

@AsCress
Copy link
Contributor Author

AsCress commented Jun 23, 2024

@marcnause Not an issue at all !! This gave me an opportunity to research different implementations of the CDC ACM driver 😂. We now have a direction along which we can work to solve the issue ! I've just pushed a new version. Could you test if this fixes those oscassional glitches in the waveform ?

@marcnause
Copy link
Contributor

I tested with the latest version and I got an IndexOutOfBoundsExeption in the line you changed in 84a7dce.

Turns out, that interface has only one endpoint on my test device. Changing the the index of getEndpoint to 0 fixed the crash, but then I did not receive any data. I dug a little bit deeper and I found out that my device seems to have 3 USB interfaces, even though it has only one USB jack. I have yet to find the combination of interface- and endpoint-index that works for my test device.

@marcnause
Copy link
Contributor

marcnause commented Jun 23, 2024

I tested with the right indices for my device (1 and 1). I got a correct waveform I did not see any glitches anymore. 👍

@AsCress
Copy link
Contributor Author

AsCress commented Jun 23, 2024

@marcnause Thanks for confirming that it works fine. I've pushed a final new version which integrates this fix into our code. This PR is now ready to be merged if everything works fine.

app/build.gradle.kts Show resolved Hide resolved
@marcnause marcnause merged commit 7fed2ad into fossasia:development Jun 23, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature New addition to the existing app
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PSLab v6 can't communicate with the Android device
4 participants