Serial Encoding with Print

8 years 5 months ago #29530470 by Confluence
Serial Encoding with Print was created by Confluence
Just posting this in case others need similar functionality.

For our particular use case, we needed the encoder counts to be sent from our FDC32XX controller at 50 Hz. The print command currently can only print string outputs. Since the encoders are int32's they are bounded by +- ~2 billion. Printing three of these values over a serial line is very slow and not efficent (2 billion is 10 chars, + 1 for negative sign, so possibly 33 characters, not counting headers or delimiters).

Our soultion uses byte encoding to represent each byte of a number as an ASCII char and sent over the serial port. The biggest issue is that the current print command can not print variables as ASCII chars. So the attached code masks each byte to get the value of each bit and then does 8 if/then/else comparisons to print the correct ASCII symbol. Using this solution we are able to encode numbers into compact byte values for serial communication.
' Encoder Packet Printer

	' Packet format:
			' [1:3]		Az: 	header				(uint8)x3
			' [4:5]		enc1 	encoder1			(uint16)
			' [6:7]		enc2 	encoder2			(uint16)
			' [8:9]		enc3 	encoder3			(uint16)
			' [10]		csum 	checksum byte 		(uint8)  (sum of encoders mod 256)

	' << init >>
	wait(500)								' pause before script execution
	
	' << main loop >>
	while 1
		enc1 = getvalue(_ABCNTR, 1)  		' get encoder values
		enc2 = getvalue(_ABCNTR, 2)
		enc3 = getvalue(_ABCNTR, 3)
				
		gosub encmod 						' bound encoder values to less than 2-byte values
		   
		gosub serialize 					' split into single byte chunks
		
		gosub chksum						' calculate checksum
		
		gosub packetprinter					' print 10-byte packet over serial

	end while

	' << bound encoder counts to [0,64000] >> 
	encmod: 
		val1 = enc1 mod 64000							' encoder values are +/-2,000,000,000
		val2 = enc2 mod 64000   						' 64000 is the largest 2-byte value
		val3 = enc3 mod 64000							' to cleanly mod with 2,000,000,000
	return

	' << serialize 2 byte values (~uint16) into 1 byte chunks >> 
	serialize: 
		' encoder 1
		byte1 = (val1 and 0b1111111100000000) >> 8	' mask, shift
		byte2 = (val1 and 0b0000000011111111) 		' mask
		' encoder 2
		byte3 = (val2 and 0b1111111100000000) >> 8
		byte4 = (val2 and 0b0000000011111111) 
		' encoder 3
		byte5 = (val2 and 0b1111111100000000) >> 8
		byte6 = (val3 and 0b0000000011111111)
	return		
	
	' << calculate checksum byte >> 
	chksum: 
		csum = (byte1 + byte2 + byte3 + byte4 + byte5 + byte6) mod 256
	return

	' << output packet over serial >>
	packetprinter: 
		print("Az4") 			' [1:3]		header		(uint8)x3
		
		val = byte1				' [4:5]		enc1 		(uint16)
		gosub twiddlebit
		val = byte2
		gosub twiddlebit
		
		val = byte3				' [6:7]		enc2 		(uint16)
		gosub twiddlebit
		val = byte4
		gosub twiddlebit
		
		val = byte5				' [8:9]		enc3 		(uint16)
		gosub twiddlebit
		val = byte6
		gosub twiddlebit
		
		val = csum				' [10]		checksum	(uint8)
		gosub twiddlebit
	return

	' << print val as uint8 char via serial >>
	twiddlebit:												' convert to binary for minimum/consistent runtime
		bit1 = (val and 0b00000001)   						' LSB
		if (val and 0b00000010)>0 then bit2=1 else bit2=0
		if (val and 0b00000100)>0 then bit3=1 else bit3=0	' Speedfix: changed division to comparison
		if (val and 0b00001000)>0 then bit4=1 else bit4=0
		if (val and 0b00010000)>0 then bit5=1 else bit5=0
		if (val and 0b00100000)>0 then bit6=1 else bit6=0
		if (val and 0b01000000)>0 then bit7=1 else bit7=0
		if (val and 0b10000000)>0 then bit8=1 else bit8=0 	' MSB
		
		if bit8 then										' begin /binary/ tree
			if bit7 then
				if bit6 then
					if bit5 then
						if bit4 then						'byte value
							if bit3 then
								if bit2 then
									if bit1 then
										print("\x0FF") 		'255
									else
										print("\x0FE") 		'254
									end if
								else
									if bit1 then
										print("\x0FD") 		'253
									else
										print("\x0FC") 		'252
									end if
								end if
							else
								if bit2 then
									if bit1 then
										print("\x0FB") 		'251
									else
										print("\x0FA") 		'250
									end if
								else
									if bit1 then
										print("\x0F9") 		'249
									else
										print("\x0F8") 		'248
									end if
								end if
							end if
						else
							if bit3 then
								if bit2 then
									if bit1 then
										print("\x0F7") 		'247
									else
										print("\x0F6") 		'246
									end if
								else
									if bit1 then
										print("\x0F5") 		'245
									else
										print("\x0F4") 		'244
									end if
								end if
							else
								if bit2 then
									if bit1 then
										print("\x0F3") 		'243
									else
										print("\x0F2") 		'242
									end if
								else
									if bit1 then
										print("\x0F1") 		'241
									else
										print("\x0F0") 		'240
									end if
								end if
							end if
						end if
					else
						if bit4 then
							if bit3 then
								if bit2 then
									if bit1 then
										print("\x0EF") 		'239
									else
										print("\x0EE") 		'238
									end if
								else
									if bit1 then
										print("\x0ED") 		'237
									else
										print("\x0EC") 		'236
									end if
								end if
							else
								if bit2 then
									if bit1 then
										print("\x0EB") 		'235
									else
										print("\x0EA") 		'234
									end if
								else
									if bit1 then
										print("\x0E9") 		'232
									else
										print("\x0E8") 		'231
									end if
								end if
							end if
						else
							if bit3 then
								if bit2 then
									if bit1 then
										print("\x0E7") 		'230
									else
										print("\x0E6") 		'229
									end if
								else
									if bit1 then
										print("\x0E5") 		'228
									else
										print("\x0E4") 		'227
									end if
								end if
							else
								if bit2 then
									if bit1 then
										print("\x0E3") 		'226
									else
										print("\x0E2") 		'225
									end if
								else
									if bit1 then
										print("\x0E1") 		'224
									else
										print("\x0E0") 		'223
									end if
								end if
							end if
						end if
					end if
				else
					if bit5 then
						if bit4 then
							if bit3 then
								if bit2 then
									if bit1 then
										print("\x0DF") 		'222
									else
										print("\x0DE") 		'221
									end if
								else
									if bit1 then
										print("\x0DD") 		'220
									else
										print("\x0DC") 		'219
									end if
								end if
							else
								if bit2 then
									if bit1 then
										print("\x0DB") 		'218
									else
										print("\x0DA") 		'217
									end if
								else
									if bit1 then
										print("\x0D9") 		'216
									else
										print("\x0D8") 		'215
									end if
								end if
							end if
						else
							if bit3 then
								if bit2 then
									if bit1 then
										print("\x0D7") 		'214
									else
										print("\x0D6") 		'213
									end if
								else
									if bit1 then
										print("\x0D5") 		'212
									else
										print("\x0D4") 		'211
									end if
								end if
							else
								if bit2 then
									if bit1 then
										print("\x0D3") 		'210
									else
										print("\x0D2") 		'209
									end if
								else
									if bit1 then
										print("\x0D1") 		'208
									else
										print("\x0D0") 		'207
									end if
								end if
							end if
						end if
					else
						if bit4 then
							if bit3 then
								if bit2 then
									if bit1 then
										print("\x0CF") 		'206
									else
										print("\x0CE") 		'205
									end if
								else
									if bit1 then
										print("\x0CD") 		'204
									else
										print("\x0CC") 		'203
									end if
								end if
							else
								if bit2 then
									if bit1 then
										print("\x0CB") 		'202
									else
										print("\x0CA") 		'201
									end if
								else
									if bit1 then
										print("\x0C9") 		'200
									else
										print("\x0C8") 		'199
									end if
								end if
							end if
						else
							if bit3 then
								if bit2 then
									if bit1 then
										print("\x0C7") 		'198
									else
										print("\x0C6") 		'197
									end if
								else
									if bit1 then
										print("\x0C5") 		'196
									else
										print("\x0C4") 		'195
									end if
								end if
							else
								if bit2 then
									if bit1 then
										print("\x0C3") 		'194
									else
										print("\x0C2") 		'193
									end if
								else
									if bit1 then
										print("\x0C1") 		'192
									else
										print("\x0C0") 		'191
									end if
								end if
							end if
						end if
					end if
				end if
			else
				if bit6 then
					if bit5 then
						if bit4 then
							if bit3 then
								if bit2 then
									if bit1 then
										print("\x0BF") 		'190
									else
										print("\x0BE") 		'189
									end if
								else
									if bit1 then
										print("\x0BD") 		'188
									else
										print("\x0BC") 		'187
									end if
								end if
							else
								if bit2 then
									if bit1 then
										print("\x0BB") 		'186
									else
										print("\x0BA") 		'185
									end if
								else
									if bit1 then
										print("\x0B9") 		'184
									else
										print("\x0B8") 		'183
									end if
								end if
							end if
						else
							if bit3 then
								if bit2 then
									if bit1 then
										print("\x0B7") 		'182
									else
										print("\x0B6") 		'181
									end if
								else
									if bit1 then
										print("\x0B5") 		'180
									else
										print("\x0B4") 		'179
									end if
								end if
							else
								if bit2 then
									if bit1 then
										print("\x0B3") 		'178
									else
										print("\x0B2") 		'177
									end if
								else
									if bit1 then
										print("\x0B1") 		'176
									else
										print("\x0B0") 		'175
									end if
								end if
							end if
						end if
					else
						if bit4 then
							if bit3 then
								if bit2 then
									if bit1 then
										print("\x0AF") 		'174
									else
										print("\x0AE") 		'173
									end if
								else
									if bit1 then
										print("\x0AD") 		'172
									else
										print("\x0AC") 		'171
									end if
								end if
							else
								if bit2 then
									if bit1 then
										print("\x0AB") 		'170
									else
										print("\x0AA") 		'169
									end if
								else
									if bit1 then
										print("\x0A9") 		'168
									else
										print("\x0A8") 		'167
									end if
								end if
							end if
						else
							if bit3 then
								if bit2 then
									if bit1 then
										print("\x0A7") 		'166
									else
										print("\x0A6") 		'165
									end if
								else
									if bit1 then
										print("\x0A5") 		'164
									else
										print("\x0A4") 		'163
									end if
								end if
							else
								if bit2 then
									if bit1 then
										print("\x0A3") 		'162
									else
										print("\x0A2") 		'161
									end if
								else
									if bit1 then
										print("\x0A1") 		'160
									else
										print("\x0A0") 		'159
									end if
								end if
							end if
						end if
					end if
				else
					if bit5 then
						if bit4 then
							if bit3 then
								if bit2 then
									if bit1 then
										print("\x09F") 		'158
									else
										print("\x09E") 		'157
									end if
								else
									if bit1 then
										print("\x09D") 		'156
									else
										print("\x09C") 		'155
									end if
								end if
							else
								if bit2 then
									if bit1 then
										print("\x09B") 		'154
									else
										print("\x09A") 		'153
									end if
								else
									if bit1 then
										print("\x099") 		'152
									else
										print("\x098") 		'151
									end if
								end if
							end if
						else
							if bit3 then
								if bit2 then
									if bit1 then
										print("\x097") 		'150
									else
										print("\x096") 		'149
									end if
								else
									if bit1 then
										print("\x095") 		'148
									else
										print("\x094") 		'147
									end if
								end if
							else
								if bit2 then
									if bit1 then
										print("\x093") 		'146
									else
										print("\x092") 		'145
									end if
								else
									if bit1 then
										print("\x091") 		'144
									else
										print("\x090") 		'143
									end if
								end if
							end if
						end if
					else
						if bit4 then
							if bit3 then
								if bit2 then
									if bit1 then
										print("\x08F") 		'142
									else
										print("\x08E") 		'141
									end if
								else
									if bit1 then
										print("\x08D") 		'140
									else
										print("\x08C") 		'139
									end if
								end if
							else
								if bit2 then
									if bit1 then
										print("\x08B") 		'138
									else
										print("\x08A") 		'137
									end if
								else
									if bit1 then
										print("\x089") 		'136
									else
										print("\x088") 		'135
									end if
								end if
							end if
						else
							if bit3 then
								if bit2 then
									if bit1 then
										print("\x087") 		'134
									else
										print("\x086") 		'133
									end if
								else
									if bit1 then
										print("\x085") 		'132
									else
										print("\x084") 		'131
									end if
								end if
							else
								if bit2 then
									if bit1 then
										print("\x083") 		'130
									else
										print("\x082") 		'129
									end if
								else
									if bit1 then
										print("\x081") 		'128
									else
										print("\x080") 		'128
									end if
								end if
							end if
						end if
					end if
				end if
			end if
		else
			if bit7 then
				if bit6 then
					if bit5 then
						if bit4 then
							if bit3 then
								if bit2 then
									if bit1 then
										print("\x07F") 		'127
									else
										print("\x07E") 		'126
									end if
								else
									if bit1 then
										print("\x07D") 		'125
									else
										print("\x07C") 		'124
									end if
								end if
							else
								if bit2 then
									if bit1 then
										print("\x07B") 		'123
									else
										print("\x07A") 		'122
									end if
								else
									if bit1 then
										print("\x079") 		'121
									else
										print("\x078") 		'120
									end if
								end if
							end if
						else
							if bit3 then
								if bit2 then
									if bit1 then
										print("\x077") 		'119
									else
										print("\x076") 		'118
									end if
								else
									if bit1 then
										print("\x075") 		'117
									else
										print("\x074") 		'116
									end if
								end if
							else
								if bit2 then
									if bit1 then
										print("\x073") 		'115
									else
										print("\x072") 		'114
									end if
								else
									if bit1 then
										print("\x071") 		'113
									else
										print("\x070") 		'112
									end if
								end if
							end if
						end if
					else
						if bit4 then
							if bit3 then
								if bit2 then
									if bit1 then
										print("\x06F") 		'111
									else
										print("\x06E") 		'110
									end if
								else
									if bit1 then
										print("\x06D") 		'109
									else
										print("\x06C") 		'108
									end if
								end if
							else
								if bit2 then
									if bit1 then
										print("\x06B") 		'107
									else
										print("\x06A") 		'106
									end if
								else
									if bit1 then
										print("\x069") 		'105
									else
										print("\x068") 		'104
									end if
								end if
							end if
						else
							if bit3 then
								if bit2 then
									if bit1 then
										print("\x067") 		'103
									else
										print("\x066") 		'102
									end if
								else
									if bit1 then
										print("\x065") 		'101
									else
										print("\x064") 		'100
									end if
								end if
							else
								if bit2 then
									if bit1 then
										print("\x063") 		' 99
									else
										print("\x062") 		' 98
									end if
								else
									if bit1 then
										print("\x061") 		' 97
									else
										print("\x060") 		' 96
									end if
								end if
							end if
						end if
					end if
				else
					if bit5 then
						if bit4 then
							if bit3 then
								if bit2 then
									if bit1 then
										print("\x05F") 		' 95
									else
										print("\x05E") 		' 94
									end if
								else
									if bit1 then
										print("\x05D") 		' 93
									else
										print("\x05C") 		' 92
									end if
								end if
							else
								if bit2 then
									if bit1 then
										print("\x05B") 		' 91
									else
										print("\x05A") 		' 90
									end if
								else
									if bit1 then
										print("\x059") 		' 89
									else
										print("\x058") 		' 88
									end if
								end if
							end if
						else
							if bit3 then
								if bit2 then
									if bit1 then
										print("\x057") 		' 87
									else
										print("\x056") 		' 86
									end if
								else
									if bit1 then
										print("\x055") 		' 85
									else
										print("\x054") 		' 84
									end if
								end if
							else
								if bit2 then
									if bit1 then
										print("\x053") 		' 83
									else
										print("\x052") 		' 82
									end if
								else
									if bit1 then
										print("\x051") 		' 81
									else
										print("\x050") 		' 80
									end if
								end if
							end if
						end if
					else
						if bit4 then
							if bit3 then
								if bit2 then
									if bit1 then
										print("\x04F") 		' 79
									else
										print("\x04E") 		' 78
									end if
								else
									if bit1 then
										print("\x04D") 		' 77
									else
										print("\x04C") 		' 76
									end if
								end if
							else
								if bit2 then
									if bit1 then
										print("\x04B") 		' 75
									else
										print("\x04A") 		' 74
									end if
								else
									if bit1 then
										print("\x049") 		' 73
									else
										print("\x048") 		' 72
									end if
								end if
							end if
						else
							if bit3 then
								if bit2 then
									if bit1 then
										print("\x047") 		' 71
									else
										print("\x046") 		' 70
									end if
								else
									if bit1 then
										print("\x045") 		' 69
									else
										print("\x044") 		' 68
									end if
								end if
							else
								if bit2 then
									if bit1 then
										print("\x043") 		' 67
									else
										print("\x042") 		' 66
									end if
								else
									if bit1 then
										print("\x041") 		' 65
									else
										print("\x040") 		' 64
									end if
								end if
							end if
						end if
					end if
				end if
			else
				if bit6 then
					if bit5 then
						if bit4 then
							if bit3 then
								if bit2 then
									if bit1 then
										print("\x03F") 		' 63
									else
										print("\x03E") 		' 62
									end if
								else
									if bit1 then
										print("\x03D") 		' 61
									else
										print("\x03C") 		' 60
									end if
								end if
							else
								if bit2 then
									if bit1 then
										print("\x03B") 		' 59
									else
										print("\x03A") 		' 58
									end if
								else
									if bit1 then
										print("\x039") 		' 57
									else
										print("\x038") 		' 56
									end if
								end if
							end if
						else
							if bit3 then
								if bit2 then
									if bit1 then
										print("\x037") 		' 55
									else
										print("\x036") 		' 54
									end if
								else
									if bit1 then
										print("\x035") 		' 53
									else
										print("\x034") 		' 52
									end if
								end if
							else
								if bit2 then
									if bit1 then
										print("\x033") 		' 51
									else
										print("\x032") 		' 50
									end if
								else
									if bit1 then
										print("\x031") 		' 49
									else
										print("\x030") 		' 48
									end if
								end if
							end if
						end if
					else
						if bit4 then
							if bit3 then
								if bit2 then
									if bit1 then
										print("\x02F") 		' 47
									else
										print("\x02E") 		' 46
									end if
								else
									if bit1 then
										print("\x02D") 		' 45
									else
										print("\x02C") 		' 44
									end if
								end if
							else
								if bit2 then
									if bit1 then
										print("\x02B") 		' 43
									else
										print("\x02A") 		' 42
									end if
								else
									if bit1 then
										print("\x029") 		' 41
									else
										print("\x028") 		' 40
									end if
								end if
							end if
						else
							if bit3 then
								if bit2 then
									if bit1 then
										print("\x027") 		' 39
									else
										print("\x026") 		' 38
									end if
								else
									if bit1 then
										print("\x025") 		' 37
									else
										print("\x024") 		' 36
									end if
								end if
							else
								if bit2 then
									if bit1 then
										print("\x023") 		' 35
									else
										print("\x022") 		' 34
									end if
								else
									if bit1 then
										print("\x021") 		' 33
									else
										print("\x020") 		' 32
									end if
								end if
							end if
						end if
					end if
				else
					if bit5 then
						if bit4 then
							if bit3 then
								if bit2 then
									if bit1 then
										print("\x01F") 		' 31
									else
										print("\x01E") 		' 30
									end if
								else
									if bit1 then
										print("\x01D") 		' 29
									else
										print("\x01C") 		' 28
									end if
								end if
							else
								if bit2 then
									if bit1 then
										print("\x01B") 		' 27
									else
										print("\x01A") 		' 26
									end if
								else
									if bit1 then
										print("\x019") 		' 25
									else
										print("\x018") 		' 24
									end if
								end if
							end if
						else
							if bit3 then
								if bit2 then
									if bit1 then
										print("\x017") 		' 23
									else
										print("\x016") 		' 22
									end if
								else
									if bit1 then
										print("\x015") 		' 21
									else
										print("\x014") 		' 20
									end if
								end if
							else
								if bit2 then
									if bit1 then
										print("\x013") 		' 19
									else
										print("\x012") 		' 18
									end if
								else
									if bit1 then
										print("\x011") 		' 17
									else
										print("\x010") 		' 16
									end if
								end if
							end if
						end if
					else
						if bit4 then
							if bit3 then
								if bit2 then
									if bit1 then
										print("\x00F") 		' 15
									else
										print("\x00E") 		' 14
									end if
								else
									if bit1 then
										print("\x00D") 		' 13
									else
										print("\x00C") 		' 12
									end if
								end if
							else
								if bit2 then
									if bit1 then
										print("\x00B") 		' 11
									else
										print("\x00A") 		' 10
									end if
								else
									if bit1 then
										print("\x009") 		'  9
									else
										print("\x008") 		'  8
									end if
								end if
							end if
						else
							if bit3 then
								if bit2 then
									if bit1 then
										print("\x007") 		'  7
									else
										print("\x006") 		'  6
									end if
								else
									if bit1 then
										print("\x005") 		'  5
									else
										print("\x004") 		'  4
									end if
								end if
							else
								if bit2 then
									if bit1 then
										print("\x003") 		'  3
									else
										print("\x002") 		'  2
									end if
								else
									if bit1 then
										print("\x001") 		'  1
									else
										print("\x000") 		'  0
									end if
								end if
							end if
						end if
					end if
				end if
			end if
		end if
	return

Please Log in or Create an account to join the conversation.

Time to create page: 0.053 seconds