• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Scala-Robotics-Simulator / PPS-22-srs / #424

28 Aug 2025 02:16PM UTC coverage: 47.411% (+0.5%) from 46.919%
#424

Pull #67

github

sceredi
chore: update default robot size and sensor range
Pull Request #67: feat(obstacleAvoidance): implement obstacle avoidance behavior in policy

67 of 93 new or added lines in 10 files covered. (72.04%)

2 existing lines in 1 file now uncovered.

1282 of 2704 relevant lines covered (47.41%)

6.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.39
/src/main/scala/io/github/srs/model/entity/dynamicentity/sensor/Sensor.scala
1
package io.github.srs.model.entity.dynamicentity.sensor
2

3
import cats.Monad
4
import cats.syntax.all.*
5
import io.github.srs.model.entity.dynamicentity.{ DynamicEntity, Robot }
6
import io.github.srs.model.entity.{ Orientation, Point2D, ShapeType }
7
import io.github.srs.model.environment.Environment
8
import io.github.srs.model.illumination.model.ScaleFactor
9
import io.github.srs.utils.Ray.intersectRay
10
import io.github.srs.utils.SimulationDefaults.DynamicEntity.Sensor.ProximitySensor as ProximitySensorDefaults
11

12
/**
13
 * Represents the range of a sensor.
14
 */
15
type Range = Double
16

×
17
/**
18
 * Represents a sensor that can sense the environment for a dynamic entity.
19
 *
20
 * @tparam Entity
21
 *   the type of dynamic entity that the sensor can act upon.
22
 * @tparam Env
23
 *   the type of environment in which the sensor operates.
24
 */
25
trait Sensor[-Entity <: DynamicEntity, -Env <: Environment]:
26
  /**
8✔
27
   * The type of data that the sensor returns. This type can vary based on the specific sensor implementation.
28
   */
29
  type Data
30

31
  /**
32
   * The offset orientation of the sensor relative to the entity's orientation.
33
   *
34
   * @return
35
   *   the orientation offset of the sensor.
36
   */
37
  def offset: Orientation
38

39
  /**
40
   * Senses the environment for the given entity and returns the data collected by the sensor.
41
   *
42
   * @param entity
43
   *   the dynamic entity that the sensor is attached to.
44
   * @param env
45
   *   the environment in which the sensor operates.
46
   * @tparam F
47
   *   the effect type in which the sensing operation is performed.
48
   * @return
49
   *   a monadic effect containing the data sensed by the sensor.
50
   */
51
  def sense[F[_]: Monad](entity: Entity, env: Env): F[Data]
52

53
  private[sensor] def direction(entity: Entity): Point2D =
54
    val globalOrientation = entity.orientation.toRadians + offset.toRadians
4✔
55
    (math.cos(globalOrientation), math.sin(globalOrientation))
24✔
56

19✔
57
  private[sensor] def origin(entity: Entity): Point2D =
58
    import Point2D.*
4✔
59
    val distance = entity.shape match
60
      case ShapeType.Circle(radius) => radius
7✔
61
      case ShapeType.Rectangle(width, height) => math.min(width, height)
19✔
62
    val origin = entity.position + direction(entity) * distance
1✔
63
    origin
33✔
64
end Sensor
2✔
65

66
/**
67
 * Represents a reading from a sensor. This case class encapsulates the sensor and the value it has sensed.
68
 *
69
 * @param sensor
70
 *   the sensor that has taken the reading.
71
 * @param value
72
 *   the value sensed by the sensor.
73
 * @tparam S
74
 *   the type of sensor, which is a subtype of [[Sensor]].
75
 * @tparam A
76
 *   the type of data sensed by the sensor.
77
 */
78
final case class SensorReading[S <: Sensor[?, ?], A](sensor: S, value: A)
79

68✔
80
/**
81
 * A collection of sensor readings. This type is used to represent multiple sensor readings from a dynamic entity. It is
82
 * a vector of [[SensorReading]] instances, allowing for efficient access and manipulation of sensor data.
83
 */
84
type SensorReadings = Vector[SensorReading[? <: Sensor[?, ?], ?]]
85

86
/**
87
 * A collection of proximity sensor readings. This type is a specialized version of [[SensorReadings]], specifically for
88
 * proximity sensors. It is a vector of [[SensorReading]] instances where the sensor is a [[ProximitySensor]] and the
89
 * data is of type `Double`.
90
 */
91
type ProximityReadings = Vector[SensorReading[ProximitySensor[?, ?], ProximitySensor[?, ?]#Data]]
92

93
/**
94
 * A collection of light sensor readings. This type is a specialized version of [[SensorReadings]], specifically for
95
 * light sensors. It is a vector of [[SensorReading]] instances where the sensor is a [[LightSensor]] and the data is of
96
 * type `Double`.
97
 */
98
type LightReadings = Vector[SensorReading[LightSensor[?, ?], LightSensor[?, ?]#Data]]
99

100
object SensorReadings:
101

3✔
102
  extension (readings: SensorReadings)
103

5✔
104
    /**
105
     * Pretty prints the sensor readings in a human-readable format. Each reading is formatted based on the type of
106
     * sensor
107
     *
108
     * @return
109
     *   a vector of strings representing the pretty-printed sensor readings.
110
     */
111
    def prettyPrint: Vector[String] =
112
      readings
×
113
        .map(r =>
×
114
          r.sensor match
×
115
            case ProximitySensor(offset, range) =>
116
              s"Proximity (offset: ${offset.degrees}°, range: $range m) -> ${r.value}"
117
            case LightSensor(offset) => s"Light (offset: ${offset.degrees}°) -> ${r.value}"
118
            case other => s"${other.getClass.getSimpleName} -> ${r.value}",
119
        )
120

×
121
    /**
122
     * Filters the sensor readings to include only those from proximity sensors.
123
     *
124
     * @return
125
     *   a vector of sensor readings from proximity sensors.
126
     */
127
    def proximityReadings: ProximityReadings =
128
      readings.collect { case _ @SensorReading(s: ProximitySensor[?, ?], v: Double) =>
4✔
129
        SensorReading(s, v)
33✔
130
      }
15✔
131

3✔
132
    /**
133
     * Filters the sensor readings to include only those from light sensors.
134
     *
135
     * @return
136
     *   a vector of sensor readings from light sensors.
137
     */
138
    def lightReadings: LightReadings =
NEW
139
      readings.collect { case _ @SensorReading(s: LightSensor[?, ?], v: Double) =>
×
NEW
140
        SensorReading(s, v)
×
NEW
141
      }
×
NEW
142
  end extension
×
143

144
end SensorReadings
145

146
/**
147
 * A proximity sensor that can sense the distance to other entities in the environment. It calculates the distance to
148
 * the nearest entity within its range and returns a normalized value. The value is normalized to a range between 0.0
149
 * (closest) and 1.0 (farthest).
150
 *
151
 * @param offset
152
 *   the offset orientation of the sensor relative to the entity's orientation.
153
 * @param range
154
 *   the range of the sensor, which defines how far it can sense.
155
 * @tparam Entity
156
 *   the type of dynamic entity that the sensor can act upon.
157
 * @tparam Env
158
 *   the type of environment in which the sensor operates.
159
 */
160
final case class ProximitySensor[Entity <: DynamicEntity, Env <: Environment](
161
    override val offset: Orientation = Orientation(ProximitySensorDefaults.DefaultOffset),
78✔
162
    range: Range = ProximitySensorDefaults.DefaultRange,
21✔
163
) extends Sensor[Entity, Env]:
13✔
164

165
  override type Data = Double
166

167
  private def rayEnd(entity: Entity): Point2D =
168
    import Point2D.*
4✔
169
    origin(entity) + direction(entity) * range
170

35✔
171
  override def sense[F[_]: Monad](entity: Entity, env: Env): F[Data] =
172
    Monad[F].pure:
4✔
173
      val o = origin(entity)
10✔
174
      val end = rayEnd(entity)
12✔
175

8✔
176
      val distances = env.entities
177
        .filter(!_.equals(entity))
5✔
178
        .flatMap(intersectRay(_, o, end))
7✔
179
        .filter(_ <= range)
8✔
180

8✔
181
      distances.minOption.map(_ / range).getOrElse(1.0)
182

29✔
183
end ProximitySensor
184

185
/**
186
 * A light sensor that senses the light intensity in the environment.
187
 *
188
 * @param offset
189
 *   the offset orientation of the sensor relative to the entity's orientation.
190
 * @tparam Entity
191
 *   the type of dynamic entity that the sensor can act upon.
192
 * @tparam Env
193
 *   the type of environment in which the sensor operates.
194
 */
195
final case class LightSensor[Entity <: DynamicEntity, Env <: Environment](
196
    offset: Orientation = Orientation(ProximitySensorDefaults.DefaultOffset),
58✔
197
) extends Sensor[Entity, Env]:
6✔
198

199
  override type Data = Double
200

201
  /**
202
   * Senses the light intensity at the position of the sensor in the environment. It uses a cached light map to compute
203
   * the field of light and samples it at the sensor's position.
204
   *
205
   * @param entity
206
   *   the dynamic entity that the sensor is attached to.
207
   * @param env
208
   *   the environment in which the sensor operates.
209
   * @return
210
   *   a monadic effect containing the light intensity sensed by the sensor.
211
   */
212
  override def sense[F[_]: Monad](entity: Entity, env: Env): F[Data] =
213
    Monad[F].pure:
4✔
214
      val o = origin(entity)
10✔
215
      env.lightField.illuminationAt(o)(using ScaleFactor.default)
12✔
216

20✔
217
end LightSensor
218

219
object Sensor:
220

×
221
  extension (r: Robot)
222

5✔
223
    /**
224
     * Senses all sensors of the robot in the given environment.
225
     *
226
     * @param env
227
     *   the environment in which to sense.
228
     * @return
229
     *   a vector of sensor readings.
230
     */
231
    def senseAll[F[_]: Monad](env: Environment): F[SensorReadings] =
232
      r.sensors.traverse: sensor =>
4✔
233
        sensor.sense(r, env).map(reading => SensorReading(sensor, reading))
17✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc