The middle pin of variable resistor is attached to A0 pin of the arduino and other two pins are attached to ground and +5v. This is creating a voltage divider and the measurement ranges from 0 to 1024. The measurement of 512 is received when the steering position is straight ahead.
The code
Here is arduino code that reads analog input from A0 and send the value to serial port. The code is adapted from Arduino IDE Analog sketch demo with minor changes.int sensorPin = A0; // select the input pin for the potentiometer int sensorValue = 0; // variable to store the value coming from the sensor int newSensorValue = 0; void setup() { Serial.begin(9600); } void loop() { // read the value from the sensor: newSensorValue = analogRead(sensorPin); if(abs(newSensorValue - sensorValue) > 5){ sensorValue = newSensorValue; Serial.println(sensorValue); } delay(10); }Here is python code that reads the data from arduino serial port and update angle variable. This angle is that is being sent to the simulator:
import serial from time import sleep import threading port = "/dev/ttyACM1" ser = serial.Serial(port, 9600) angle = 0.0 def read_steering_angle(): print("Monitoring steering angle") global angle while True: data = ser.readline() if len(data) > 0: #print('Got serial data:', data) angle = float(int(data) - 512) / 50.0 return angle t = threading.Thread(target=read_steering_angle) t.daemon = True t.start() while True: print(angle) sleep(1)
Demo
Here is how I am driving using the steering.Here is screen capture of driving with the steering.
No comments:
Post a Comment