Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
|
Forum Index : Microcontroller and PC projects : PicoMite: Serial port weirdness.
Author | Message | ||||
Gizmo Admin Group Joined: 05/06/2004 Location: AustraliaPosts: 5078 |
Making progress on using the PicoMite for a rover project. So far its reading compass, distance, RC and GPS data, plus sending the telemetry data. On com1 I'm getting corrupt data, most of the time. A few CtrC's and RUN's, and its back to normal, but if I stop the program and run it again, its bad data, so I keep trying stops and starts until its good again. The data is at 38.4kb, NMEA data from a GPS module. Its almost as though the com port is starting to read bytes mid byte and getting out of sync. Am I missing something, is there a best practice to reset the com port in a program, clear the buffer if the data is bad? Code below. 'initiate I2C SetPin GP0,GP1,I2C I2C OPEN 100, 1000 'initiate Serial 1 for RTK GPS input SetPin GP12, GP13, COM1 ' initiate Serial 2 for Telemetry setpin GP9, GP8, COM2 'initilise the motor drive pins setpin GP2, DOUT setpin GP3, PWM[1] setpin GP4, DOUT setpin GP5, PWM[2] 'initilise the batt voltage analogue input SETPIN GP28, AIN 'Initiage compass I2C WRITE &H1E, 0,2,&H02, &H00 Dim MagData(6) 'Open the com ports Open "COM1:38400" As GPS Open "COM2:57600" As #2 'initilise pins for reading PPM data SETPIN GP6, DIN DIM RCChan(8) Do ' Compass I2C WRITE &H1E,0,1,&H03 I2C READ &H1E, 0, 6, MagData() X=(MagData(0)*256)+MagData(1) Y=(MagData(2)*256)+MagData(3) Z=(MagData(4)*256)+MagData(5) If X>32768 Then X=X-65536 If Y>32768 Then Y=Y-65536 If Z>32768 Then Z=Z-65536 ' Added the +190 And +127 to calibrate the sensor for my location CompassInRad = Atan2(Z+190, X+127) + DeclinationAngle CompassInDeg=Int(CompassInRad * 180/Pi) Print "Compas ";CompassInDeg;" degrees" Print #2, "<COM>";CompassInDeg;"</COM>]" ' Distance D=Distance(24,25) Print "Distance to object ";D;"cm" Print #2, "<DIS>";D;"</DIS>" ' GPS Print "GPS Valid ";GPS(VALID) Print "Lat ";GPS(LATITUDE) Print "Lng ";GPS(LONGITUDE) Print "DOP ";GPS(DOP) Print "FIX ";GPS(FIX) print #2, "<LAT>";GPS(Latitude);"</LAT>" print #2, "<LNG>";GPS(LONGITUDE);"</LNG>" print #2, "<FIX>";GPS(FIX);"<DOP>" ' RC Receiver, using PPM ' https://github.com/rc-hacks/mpx-ppm-spec c=0 hp=0 lp=pulsin(GP6,0,50000)' get low pulse duration. do while hp<4000 and c<10 ' Wait for a sync pulse hp=pulsin(GP6,1,50000) c=c+1 loop if hp>0 then for t=1 to 4 ' only need the first 4 channels RCChan(t) = pulsin(GP6,1)+lp next print "RC Channel Data "; for t=1 to 4 print str$(RCChan(t))+" "; print #2, "<CH1>";str$(RCChan(t));"</CH1>" print #2, "<CH2>";str$(RCChan(t));"</CH2>" print #2, "<CH3>";str$(RCChan(t));"</CH3>" print #2, "<CH4>";str$(RCChan(t));"</CH4>" next else Print "No PPM signal" endif ' Battery Voltage ' 10:1 resisitor diver, calibrated in software BattV=Pin(GP28) print "Batt voltage ";BattV*12.51 Pause (500) Loop Glenn The best time to plant a tree was twenty years ago, the second best time is right now. JAQ |
||||
phil99 Guru Joined: 11/02/2018 Location: AustraliaPosts: 2135 |
Have had something similar, though at higher speed with a long cable so may not be relevant here. Fixed by arranging the ground wire to be between Tx and Rx lines and adding a 3.3kΩ pullup at the Rx pin at each end. |
||||
Gizmo Admin Group Joined: 05/06/2004 Location: AustraliaPosts: 5078 |
I might try a pull down resistor on the Rx line of the Pico. Its only a short run of a couple inches, and there is a level shifter in there, the GPS is outputting 5v. I also need to receive data on com2, will try that later, but tis sending data just fine. I wonder if I issue close and open com port commands in code if the GPS data is bad, might be a solution. The best time to plant a tree was twenty years ago, the second best time is right now. JAQ |
||||
phil99 Guru Joined: 11/02/2018 Location: AustraliaPosts: 2135 |
Rather than a level-shifter it may be better to use a voltage divider. 1kΩ between GPS Tx and Pico Rx plus 2.2kΩ from Pico Rx to ⏚. The Pico digital inputs can tolerate up to 5.0V (the datasheet is conservative) so just the 1kΩ between GPS Tx and Pico Rx should also be ok. |
||||
Turbo46 Guru Joined: 24/12/2017 Location: AustraliaPosts: 1611 |
I don't really know if it would help but I like to assemble a complete message in a string variable then send the string PRINT #2, Txd$ rather than assemble the message as you are transmitting it. Doing that may cause inter character gaps that may cause loss of sync. Bill Keep safe. Live long and prosper. |
||||
Grogster Admin Group Joined: 31/12/2012 Location: New ZealandPosts: 9306 |
Not sure if it matters, but I have had good success using CAT5/CAT6 network cable. Connect all traces(GRN/WHI, ORN/WHI, BRN/WHI, BLU/WHI) to ground, and have your TXD/RXD on one pair of the cable. This has resulted in good outcomes when I have wired up this way. Keep the UART port speed SLOW. Definitely no faster then 2400/4800 baud. It looks like you are using standard 4-core security cable - get rid of that. Although what I mentioned is still an "Unbalanced" arrangement, having a ground-wire twisted with each of the "Hot" data lines, does indeed seem to produce good results. Try hooking up a length of CAT5(CAT6 is even better, as the twist is tighter), and slow the UART to no more then 4800, and it has always worked for me with no corrupt characters. I've got one link running over about 50M of CAT6, and it works just fine.(@ 2400 baud) YMMV. Smoke makes things work. When the smoke gets out, it stops! |
||||
Gizmo Admin Group Joined: 05/06/2004 Location: AustraliaPosts: 5078 |
A crude fix, but it works for now. If it receives 5 GPS(VALID)=0 in a row, it closes and reopens the comport. On about the 3rd or 4th com restart it worked and is fine after that. I'll get rid of the level shifter and go for the resistor divider, I may need that level shifter port for another job. if GPS(VALID)=0 then BadGPSData=BadGPSData+1 if BadGPSData>5 then close GPS SetPin GP12, GP13, COM1 Open "COM1:38400" As GPS BadGPSData=0 end if Hey G, the run from the GPS module to the Pico is only a couple inches. The other devices on the 4 wire cables are all working ok, but if they play up I'll be upgrading the cables. When this mower is 200 meters away from teh base station it will need all the clean signals it can get. 4800bd is way to slow for what I need unfortunately, especially from the GPS module. Glenn The best time to plant a tree was twenty years ago, the second best time is right now. JAQ |
||||
Mixtel90 Guru Joined: 05/10/2019 Location: United KingdomPosts: 6783 |
If you are running any distance then it might be an idea to change to use a differential signal like RS485 over twisted pair. It's easy enough now. With CAT5 / CAT6 you could use RX and TX pairs so there's no direction switching to bother about. Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
phil99 Guru Joined: 11/02/2018 Location: AustraliaPosts: 2135 |
I think Glenn is using RF comms. Otherwise the mower may mow the cable :) |
||||
Mixtel90 Guru Joined: 05/10/2019 Location: United KingdomPosts: 6783 |
This is true. :) At RF high data rate = high bandwidth. The data rate limit is how much bandwidth you are allowed to use. That's the *total* data rate, including start & stop bits, headers, any obligatory idling time and actual useful data bits. To get a high speed data link (over 1Mbps) it's often necessary to use GHz band RF.433MHz etc. may be as low as 3kbps. Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
Gizmo Admin Group Joined: 05/06/2004 Location: AustraliaPosts: 5078 |
Its using a pair of HC-12 modules to receive data from a fixed GPS RTK base station. The base station is located where at most the mower would be 200 meters away. For telemetry it uses a paid of ESP32's acting as a serial bridge. I've fitted external aerials and using the 802.11n protocol. I haven't found their range limit yet. Lastly it also uses a RC transmitter and receiver. This is used to arm the rover, select a driving mode ( auto or manual ), etc. Its also a safety kill switch, no RC signal means full stop. The best time to plant a tree was twenty years ago, the second best time is right now. JAQ |
||||
Mixtel90 Guru Joined: 05/10/2019 Location: United KingdomPosts: 6783 |
Apparently the HC-12 can manage up to 57600 bps or 115200 bps from the serial port (236000 bps OTA in both cases), but the receiver sensitivity has dropped by then. Linky Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
Print this page |